O'Reilly logo

Stay ahead with the world's most comprehensive technology and business learning platform.

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, tutorials, and more.

Start Free Trial

No credit card required

Kotlin for Enterprise Applications using Java EE

Book Description

Build Java Enterprise Applications and learn how Kotlin makes it easier to code them using components like JSF 2.3, Enterprise JavaBeans (EJB) 3.2, Contexts and Dependency Injection (CDI) 2.0, the Java API for WebSockets, JAX-RS 2.1, Servlet 4.0.

Key Features

  • An in-depth guide updated with all the latest features of Kotlin 1.2 and Java EE 8
  • Build microservices in Java EE with the help of Kotlin use cases
  • Explore coroutines, garbage collection, multithreading, memory management and more

Book Description

Kotlin was developed with a view to solving programmers' difficulties and operational challenges. This book guides you in making Kotlin and Java EE work in unison to build enterprise-grade applications. Together, they can be used to create services of any size with just a few lines of code and let you focus on the business logic.

Kotlin for Enterprise Applications using Java EE begins with a brief tour of Kotlin and helps you understand what makes it a popular and reasonable choice of programming language for application development, followed by its incorporation in the Java EE platform. We will then learn how to build applications using the Java Persistence API (JPA) and Enterprise JavaBeans (EJB), as well as develop RESTful web services and MicroServices. As we work our way through the chapters, we'll use various performance improvement and monitoring tools for your application and see how they optimize real-world applications. At each step along the way, we will see how easy it is to develop enterprise applications in Kotlin. By the end of this book, we will have learned design patterns and how to implement them using Kotlin.

What you will learn

  • Understand Kotlin syntax and appreciate why it's gaining in popularity
  • Explore the Java EE ecosystem and the APIs in Java EE
  • Implement applications using Kotlin
  • Overcome the challenges of developing the Java EE system using Kotlin
  • Gain insights into Java Message Services (JMS)
  • Build RESTful MicroServices and secure applications
  • Optimize applications with performance and monitoring tools
  • Understand design patterns and implement them

Who this book is for

Kotlin for Enterprise Applications using Java EE is for Java EE developers who want to build their enterprise project or application with Kotlin or migrate from Java to Kotlin. Basic knowledge of programming is necessary to understand the key concepts covered in this book.

Downloading the example code for this book You can download the example code files for all Packt books you have purchased from your account at http://www.PacktPub.com. If you purchased this book elsewhere, you can visit http://www.PacktPub.com/support and register to have the files e-mailed directly to you.

Table of Contents

  1. Title Page
  2. Copyright and Credits
    1. Kotlin for Enterprise Applications using Java EE
  3. Dedication
  4. Foreword
  5. Contributors
    1. About the author
    2. About the reviewers
  6. Acknowledgements
  7. About Packt
    1. Why subscribe?
    2. Packt.com
  8. Preface
    1. Who this book is for
    2. What this book covers
    3. To get the most out of this book
      1. Download the example code files
      2. Conventions used in the book
    4. Get in touch
      1. Reviews
  9. Kotlin – A First look
    1. Technical requirements
    2. Introduction to Kotlin
      1. The history of Kotlin
      2. Features of Kotlin
      3. Getting started with Kotlin
        1. Installing Kotlin
          1. Installing Kotlin on Linux 
          2. Installing Kotlin on Windows
          3. Installing Kotlin on Mac
          4. Compiling and running
    3. A quick tour of Kotlin
      1. Declaring variables
      2. Data types in Kotlin
        1. Warnings
        2. Type inference
        3. String templates
          1. Multi-line String Literals
      3. Expressions over statements
        1. Functions
        2. Default arguments
        3. Named arguments
        4. varargs and spread
      4. The for loop
      5. Iterating over a list
      6. When clause
      7. The nullable type
    4. Lambda expressions in Kotlin
    5. Extension functions
    6. Classes in Kotlin
      1. Constructors 
        1. Static functions
    7. Kotlin programming style and Syntax
    8. Summary
  10. Kotlin – The Game Changer
    1. Technical requirements
    2. Why Kotlin is the game changer
      1. Interoperability with Java
      2. Functional paradigms
      3. Immutability
      4. Null safety
    3. Kotlin versus Java
      1. Null safety issue
      2. Arrays in Kotlin are invariant
      3. Extension functions
      4. No checked exceptions
        1. Functional paradigms
        2. Concise code
    4. Configuring Kotlin in the project
      1. Creating a simple Kotlin project
      2. Maven project
      3. Configuring Kotlin in Eclipse
    5. Working with coroutines
      1. Coroutines in Kotlin
    6. Using Kotlin with Java
      1. Kotlin in an existing Java project
      2. Java in a Kotlin project
    7. Summary
  11. An Overview of Java EE and Kotlin
    1. Technical requirements
    2. Overview of Java EE
      1. New features and enhancements in Java EE 8
      2. Introduction to the Java EE architecture
      3. Integrating Kotlin plugins
        1. The all-open compiler plugin
          1. Using the all-open plugin in Maven
          2. Using the all-open plugin in Gradle
        2. No-arg compiler plugin
          1. Using the no-org plugin in Maven
          2. Using the no-org plugin in Gradle
        3. The kotlin-spring compiler plugin
          1. Using the kotlin-spring plugin in Maven
          2. Using the kotlin-spring plugin in Gradle
          3. Using the kotlin-spring plugin in CLI
        4. JPA plugin
          1. Using the kotlin-jpa plugin in Maven
          2. Using the kotlin-jpa plugin in Gradle
          3. Using the kotlin-jpa plugin in CLI
        5. The SAM-with-receiver compiler plugin
          1. Using the SAM-with-receiver plugin in Maven
          2. Using the SAM-with-receiver plugin in Gradle
          3. Using the SAM-with-receiver plugin in CLI
        6. The Jackson plugin
          1. Using the Jackson plugin in Maven
          2. Using the Jackson plugin in Gradle
      4. Kotlin and Servlets
        1. Kotlin for server-side development
        2. Servlets
          1. The life cycle of a servlet
        3. Creating a simple servlet application
      5. Kotlin and EJB
        1. An overview of EJBs
        2. Advantages of EJBs
          1. The EJB component model
        3. Bean validation
    3. Summary
  12. Kotlin with JSF and CDI
    1. Technical requirements
    2. Introduction to JSF
    3. JSF architecture
      1. The Benefits of using JSF
    4. Developing our first JSF application
    5. Kotlin and CDI
      1. The Difference between EJB and CDI-managed beans
        1. Injecting the beans
    6. Producers
    7. Qualifiers
      1. Scopes
      2. CDI and domain events
        1. Defining an event
        2. Listening to the event
        3. Firing the event
    8. Interceptors
      1. Interceptor
        1. Implementing the interceptor
      2. Building custom interceptors
        1. Defining a custom interceptor
        2. Enhancing the auditable interceptor
        3. Activating the interceptor
    9. Summary
  13. Kotlin with JPA and EJB
    1. Technical requirements
    2. Kotlin Data Classes
    3. Java Persistence API
      1. JPA architecture
        1. Bootstrapping the JPA
        2. Mapping domain entities using JPA
      2. Queries
    4. Modeling JPA entities
      1. Mapping entities
        1. OneToMany mapping
        2. Cascading
        3. Fetching strategy
        4. Naming a join column
        5. ManyToOne mapping
      2. Data sources
        1. Persistence units
        2. Persistence context
    5. Transactions
    6. Exception handling
    7. Using JPA in an application
    8. Summary
  14. Enterprise Messaging with Kotlin
    1. Technical requirements
    2. Understanding the messaging domains
      1. Messaging architecture
      2. Messaging domains
        1. Point-to-point messaging model
        2. Publish-subscribe messaging model
    3. Messaging queues and topics
      1. Java messaging System
        1. Installing GlassFish
        2. Configuring GlassFish
    4. Building a messaging service using Kotlin
      1. Point-to-point messaging
        1. Writing a producer class
        2. A quick comparison to Java code
        3. Writing a Consumer class
        4. Writing a test case for the Point-to-point messaging model
      2. Publish-subscribe model
        1. Writing a Publisher class
        2. Writing a Subscriber class
        3. Writing a test case for the publish-subscribe model
    5. Message acknowledgement
    6. Transactions
      1. Enabling transactions in the point-to-point messaging model
      2. Enabling transactions in the publish-subscribe messaging model
    7. Summary
  15. Developing RESTful Services with JAX-RS
    1. Technical requirements
    2. Web services
      1. Working model of the web service
      2. RESTful web services
    3. Understanding REST verbs 
      1. Resources
        1. HTTP status codes
          1. The 100 series
          2. The 200 series
          3. The 300 series
          4. The 400 series
          5. The 500 series
    4. Introduction to JAX-RS
      1. JAX-RS annotations
    5. Implementing a RESTful service using Jersey
    6. Implementing Real-World RESTful Web Services
      1. Defining the layers
        1. Writing a POST function for a create operation
        2. Invoking the create organization API using cURL
        3. Writing a GET function for a read operation 
        4. Invoking the get organization API using cURL
    7. Summary
  16. Securing JAVA EE Applications with Kotlin
    1. Technical requirements
    2. Introduction to security API
    3. The IdentityStore mechanism
      1. EmbeddedIdentityStoreDefinition
      2. DatabaseIdentityStoreDefinition
      3. LdapIdentityStoreDefinition
    4. HttpAuthenticationMechanism
      1. BasicAuthenticationMechanismDefinition
      2. FormAuthenticationMechanismDefinition
      3. Custom form-based HTTP authentication
      4. The SecurityContext API
    5. Implementing security API using Kotlin
    6. Securing JAX-RS APIs with JWT
      1. The structure of JWT
        1. Header
        2. Payload
        3. Signature
      2. Implementing JWT
    7. Summary
  17. Implementing Microservices with Kotlin
    1. Technical requirements
    2. Introduction to microservices
      1. Advantages of microservices
    3. Breaking the monolith into microservices
      1. Microservices architecture (MSA)
    4. Developing real-world microservices
      1. Developing the microservices
        1. Developing the authentication service
        2. Implementing the /authorize/jwt/token API 
        3. Implementing the /authorize/jwt/verify-token API 
        4. Developing the identity service
          1. The flow diagram
      2. Exception handling
    5. Writing test code for microservices
      1. Unit testing
        1. Writing the skeleton for the test class
        2. Setting up the test data
        3. Mocking the response
        4. Writing the assertions
      2. Integration testing
        1. Writing a REST client for the POST /authorize/jwt/token API
        2. Writing a REST client for the /identity/organization/{orgId} API
        3. Writing the assertions
        4. Refactoring the integration test cases
    6. Summary
  18. Performance Monitoring and Logging
    1. Technical requirements
    2. Finding Memory Leaks
    3. Application monitoring 
      1. Java Mission Control
      2. Java VisualVM
    4. Garbage collection
      1. Memory model
      2. Types of garbage collector
        1. Serial and parallel collectors
        2. Concurrent collectors
        3. Tuning the GC
          1. High throughput and low latency
          2. High throughput and low footprint
          3. Low pause time and small footprint
    5. Profiling
      1. Profiling with JProfiler
      2. Offline profiling
    6. Getting our real-world application production-ready
    7. Summary
  19. Design Patterns with Kotlin
    1. Technical requirements
    2. Design patterns
      1. Advantages of design patterns
    3. Categorizing design patterns
      1. Creational patterns
      2. Structural patterns
      3. Behavioral patterns
    4. Implementing the singleton pattern
      1. Writing a singleton class in Java
      2. Writing the test case for a singleton
      3. Lazy initialization
      4. Singleton implementation in comparison to Kotlin
    5. Implementing the factory pattern
    6. Implementing the builder pattern
    7. Implementing the decorator pattern
      1. Writing the code for the decorator pattern
      2. Adding decorators
    8. Implementing the observer pattern
      1. Illustrating the observer pattern
    9. Implementing the chain of responsibility pattern
      1. Illustrating the chain-of-responsibility pattern
      2. Defining the contracts
      3. Writing the implementation
      4. Defining the concrete handlers
      5. Verifying the handler implementation
    10. Selecting design patterns
    11. Summary
  20. Other Books You May Enjoy
    1. Leave a review - let other readers know what you think