30

Table of Contents · Introduction: Java EE, Jakarta EE and Microprofile The Java Enterprise Edition (Java EE) is a superset of Java Standard Edition (Java SE) that dictates the basic

  • Upload
    others

  • View
    7

  • Download
    1

Embed Size (px)

Citation preview

Page 1: Table of Contents · Introduction: Java EE, Jakarta EE and Microprofile The Java Enterprise Edition (Java EE) is a superset of Java Standard Edition (Java SE) that dictates the basic
Page 2: Table of Contents · Introduction: Java EE, Jakarta EE and Microprofile The Java Enterprise Edition (Java EE) is a superset of Java Standard Edition (Java SE) that dictates the basic

Table of ContentsPractical Enterprise Application Development . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .  1

Preface . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .  1

The Author of the Book . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .  2

The reviewers . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .  2

Who this book is for . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .  3

How to Contact Us . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .  3

Piracy . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .  3

Book Version . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .  3

Conventions used in this book . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .  3

Source code. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .  4

Introduction: Java EE, Jakarta EE and Microprofile . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .  5

Java EE 7 new API . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .  6

Java EE 7 enhancements . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .  6

Java EE 8 enhancements . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .  7

From Java EE to Jakarta EE 8 and beyond . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .  7

MicroProfile and Java EE . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .  9

1. Getting started with Jakarta EE Development . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .  10

1.1. Installing WildFly and Development tools. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .  10

1.1.1. Testing the installed application server . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .  11

1.1.1.1. Stopping WildFly. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .  12

1.2. Installing Maven. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .  13

1.3. Installing a Development Environment . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .  14

1.3.1. A bird’s eye view of IntelliJ Idea . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .  14

1.3.2. Importing the book example code into your IDE . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .  15

1.4. Debugging WildFly with IntelliJ Idea . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .  17

2. Configuring Maven to build Enterprise projects . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .  19

2.1. Using Maven archetypes for your projects . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .  19

2.1.1. Using the webapp-javaee7 archetype . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .  20

2.1.2. Using the ejb-javaee7 archetype . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .  21

2.1.3. Using the appclient-javaee7 archetype . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .  22

2.1.4. Using the ear-javaee7 archetype. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .  23

2.2. Creating Maven Projects from IntelliJ idea . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .  24

2.3. Configuring the Project object module (pom.xml) . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .  25

2.3.1. Project coordinates . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .  25

2.4. Packaging . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .  25

2.4.1. Properties . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .  25

2.4.2. Project Dependencies . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .  26

2.4.2.1. Using Jakarta EE dependency . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .  26

Page 3: Table of Contents · Introduction: Java EE, Jakarta EE and Microprofile The Java Enterprise Edition (Java EE) is a superset of Java Standard Edition (Java SE) that dictates the basic

2.4.2.2. Using WildFly BOM and dependencies . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .  26

2.4.2.2.1. Adding Project dependencies to your pom.xml . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .  27

2.5. The build section of your pom.xml . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .  28

2.5.1. Managing application deployment . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .  30

3. Programming Servlets . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .  31

3.1. Defining Servlets . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .  31

3.1.1. Registering a Servlet Dynamically . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .  33

3.1.2. Using init Parameters in the your Servlets . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .  34

3.1.3. Using Filters in your Servlets. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .  34

3.1.4. Using Servlet Listeners in your applications . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .  35

3.1.4.1. Listening to your ServletContext . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .  36

3.1.4.2. Listening to your ServletContext attribute changes . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .  36

3.1.4.3. Listening to your HttpSession creation and disposal . . . . . . . . . . . . . . . . . . . . . . . . . . . . .  36

3.1.4.4. Listening to changes in HttpSession attributes . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .  37

3.1.4.5. Listening to changes in HttpSession activation and passivation . . . . . . . . . . . . . . . . . .  37

3.1.4.6. Listening to bindings in HttpSession . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .  38

3.1.4.7. Listening to Http Request creation and disposal. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .  39

3.2. Asynchronous support . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .  39

3.2.1. Coding an asynchronous Servlet . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .  39

3.3. Non-Blocking I/O in Servlets . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .  42

3.4. Using Server Push . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .  45

3.4.1. Using the PushBuilder Interface . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .  45

3.4.1.1. Using the Push Strategy with Servlet Filters . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .  46

3.5. Compiling applications using Servlets . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .  47

4. Developing Enterprise Java Beans. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .  49

4.1. Overview of Enterprise Java Beans . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .  49

4.2. Coding a Stateless Session Bean . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .  50

4.3. Coding a Stateful Session Bean . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .  50

4.3.1. Compiling and deploying the project . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .  51

4.3.2. Controlling Passivation for Stateful EJBs . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .  53

4.4. Coding EJB Clients . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .  54

4.4.1. Coding the remote EJB client . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .  55

4.4.1.1. Configuring your Remote EJB Client . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .  57

4.4.2. Configuring your Client’s Maven dependencies . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .  58

4.4.3. Running the client class . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .  59

4.4.3.1. Using jboss-ejb-client.properties configuration file . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .  60

4.5. The other type of Beans . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .  60

4.5.1. Coding Singleton EJBs . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .  61

4.5.1.1. Controlling Singleton Concurrency . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .  62

4.6. Using Bean Managed Concurrency . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .  62

4.7. Coding Asynchronous EJBs . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .  62

Page 4: Table of Contents · Introduction: Java EE, Jakarta EE and Microprofile The Java Enterprise Edition (Java EE) is a superset of Java Standard Edition (Java SE) that dictates the basic

4.8. Coding EJB Timers . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .  64

4.8.1. Defining programmatic timers using @Timeout annotation . . . . . . . . . . . . . . . . . . . . . . . . .  65

4.8.2. Defining Automatic timers using the @Schedule annotation . . . . . . . . . . . . . . . . . . . . . . . . .  66

4.8.3. Getting a list of active timers . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .  66

4.8.4. Timers and Persistence . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .  67

4.9. Configuring Transactions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .  67

4.9.1. Container Managed Transactions. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .  68

4.9.2. Container Managed Transactions and Exceptions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .  70

4.9.3. Bean Managed Transactions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .  70

4.9.3.1. Compiling EJBs with BMT . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .  71

5. Context Dependency Injection . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .  72

5.1. Overview of CDI and Dependency Injection . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .  72

5.1.1. Learning CDI Scopes . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .  73

5.1.2. Naming your Beans . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .  74

5.1.2.1. Using the Model annotation . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .  75

5.2. Managing the Bean configuration . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .  75

5.2.1. Using filter expressions in your beans.xml . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .  76

5.2.2. Vetoed Beans . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .  77

5.3. Beans Producers and Disposers . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .  78

5.4. Combining pieces to create a simple example . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .  79

5.4.1. Coding the Java Bean class . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .  80

5.4.2. Including a Qualifier . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .  80

5.4.3. Creating a Producer for our Bean . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .  80

5.4.4. Coding the SessionScoped CDI Bean . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .  81

5.4.5. Coding the User Interface . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .  82

5.4.6. Building and Running the example . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .  83

5.5. Adding Qualifiers members . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .  83

5.6. Adding Interceptors on Producers (CDI 2.0) . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .  84

5.6.1. Implementing Interceptor Beans . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .  87

5.6.2. Multiple interceptors . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .  88

5.6.3. Inheriting Interceptors . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .  89

5.6.4. Compiling classes with Interceptors . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .  89

5.7. Implementing Decorator Beans . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .  90

5.8. Implementing Alternative Beans . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .  91

5.9. Using Conversation Scope in your Beans . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .  93

5.10. Managing Events with CDI. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .  93

5.10.1. Firing events asynchronously . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .  94

5.10.2. Adding Qualifiers to your Observers . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .  95

5.11. Transactions and CDI . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .  96

5.12. Running CDI outside of WildFly . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .  99

6. Java Server Faces . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .  102

Page 5: Table of Contents · Introduction: Java EE, Jakarta EE and Microprofile The Java Enterprise Edition (Java EE) is a superset of Java Standard Edition (Java SE) that dictates the basic

6.1. Introduction to JSF. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .  102

6.2. Creating your first Java Server Faces application . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .  102

6.3. Building the application Model . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .  103

6.3.1. Building the view . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .  105

6.3.2. Compiling and deploying your application . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .  107

6.3.3. A bit of style . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .  108

6.4. JSF survival kit . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .  108

6.4.1. Using JSF facets to simplify JSF development . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .  108

6.4.2. Showing messages in your JSF applications . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .  109

6.4.3. Showing debugging information in your pages . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .  110

6.5. Validating your data . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .  111

6.6. Bean Validation 2.0 . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .  112

6.6.1. Constraints in Parameterized types . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .  113

6.6.2. Cascaded Validation of Containers . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .  114

6.7. Enhancing your application with Facelets . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .  114

6.7.1. Creating Composite components . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .  116

6.7.2. Caching Facelets . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .  118

6.8. Navigation using Java Server Faces . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .  119

6.8.1. Standard JSF navigation . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .  119

6.8.2. Implicit JSF Navigation . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .  119

6.8.3. JSF Conditional Navigation . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .  120

6.9. JSF 2.2 goodies . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .  121

6.9.1. Using Faces Flow in your JSF application . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .  121

6.9.1.1. Mixing multiple flows . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .  124

6.9.2. Using Resource Library contracts . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .  128

6.9.2.1. Reusing library contracts in your applications . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .  131

6.9.3. HTML 5 and JSF . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .  131

6.9.4. Passing attributes from the server side . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .  132

6.9.5. Pass-through Elements . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .  133

6.9.6. Uploading files with JSF . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .  133

6.10. JSF 2.3 goodies . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .  136

6.10.1. Using Command Script . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .  137

6.10.2. Networking and WebSockets updates . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .  139

6.10.2.1. WebSocket Usage: Client Side . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .  139

6.10.2.2. WebSocket Usage: Server Side . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .  139

6.10.3. Validation and conversion enhancements . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .  141

6.10.4. Map Iteration in UIData and UIRepeat . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .  142

6.10.5. Deprecation of Managed Beans . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .  144

6.10.6. JSF Events after view is rendered . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .  146

6.11. Class level bean validation . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .  149

6.11.1. Using Server Push with JSF . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .  152

Page 6: Table of Contents · Introduction: Java EE, Jakarta EE and Microprofile The Java Enterprise Edition (Java EE) is a superset of Java Standard Edition (Java SE) that dictates the basic

7. Learning Java Persistence API . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .  153

7.1. Introducing JPA . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .  153

7.2. Working with JPA . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .  153

7.2.1. Setting up a database connection . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .  154

7.2.1.1. Configuring the Datasource using the CLI . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .  154

7.3. A sample JPA Project . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .  155

7.3.1. The JPA Configuration file . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .  157

7.3.2. Adding an Entity Manager to our application . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .  159

7.3.3. Compiling a JPA Project . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .  160

7.3.4. Coding a Test class for our JPA project . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .  161

7.4. Focus on Schema generation . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .  161

7.5. Using Named queries . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .  162

7.6. Other ways to perform CRUD operations . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .  163

7.6.1. Using Criteria Queries . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .  163

7.6.1.1. Performing Bulk Updates and Deletes using Criteria Queries. . . . . . . . . . . . . . . . . . . .  164

7.6.2. Executing Native SQL queries . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .  165

7.7. Adding Validation to your Entities . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .  166

7.7.1. Using built-in constraints . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .  167

7.7.2. Available built-in constraints . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .  167

7.7.3. Creating Custom Constraints . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .  170

7.8. Calling Stored Procedures . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .  171

7.9. Caching data . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .  173

7.9.1. Enabling Hibernate Cache . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .  174

7.10. Managing JPA Persistence context . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .  175

7.11. Using Listeners in your Entities . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .  176

8. Testing applications using Arquillian . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .  178

8.1. Moving from unit tests to Arquillian. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .  178

8.2. Introducing Arquillian . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .  181

8.3. Setting up a Maven project for Arquillian . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .  182

8.3.1. Compiling your project . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .  184

8.3.2. Changing the defaults of remote environment . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .  187

8.3.3. Running a test in a managed environment . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .  187

9. Developing applications with WebSockets . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .  190

9.1. Overview of WebSockets . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .  190

9.1.1. Into the WebSocket protocol . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .  190

9.2. Running your First WebSocket application . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .  191

9.2.1. Coding a simple Javascript client . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .  192

9.2.2. Compiling and running the example. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .  194

9.3. Websockets messaging styles . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .  194

9.4. Creating a Java WebSocket Endpoint . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .  195

9.5. Using Encoders and Decoders to send custom Java types . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .  196

Page 7: Table of Contents · Introduction: Java EE, Jakarta EE and Microprofile The Java Enterprise Edition (Java EE) is a superset of Java Standard Edition (Java SE) that dictates the basic

9.6. Sending data asynchronously . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .  200

9.6.1. Option 1: the J2SE Future object approach . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .  200

9.6.2. Option 2: Implementing an Handler . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .  201

9.7. Sending binary content . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .  201

9.8. Maintaining Client State and other advanced features . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .  202

9.8.1. Using Path parameters in Endpoints . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .  203

9.9. Defining an Endpoint Configurator Class . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .  204

10. Developing SOAP Based Web services . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .  206

10.1. Developing SOAP-based Web services . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .  206

10.2. Strategies for building SOAP web services . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .  207

10.3. A basic Web Service Project . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .  207

10.3.1. Developing the Implementation class . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .  209

10.3.2. Deploying the Web service . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .  210

10.3.3. Creating a WS Test client . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .  211

10.3.4. Creating a native Apache CXF Client . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .  213

10.4. Customizing the Web Service Properties . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .  214

10.5. Using Handlers in your Web services . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .  215

10.5.1. Coding SOAP Handlers . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .  217

10.5.2. Coding Logical Handlers . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .  219

11. Developing RESTful Web services . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .  221

11.1. Entering RESTFul Web services . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .  221

11.2. Coding RESTFul Web services . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .  221

11.2.1. Packaging and deploying your REST Web service . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .  223

11.3. Using parameters in your REST services . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .  224

11.4. RESTful Web services client API . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .  226

11.4.1. Compiling and Running the Test example . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .  228

11.5. Asynchronous JAX-RS Server API . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .  229

11.6. Asynchronous JAX-RS Client API . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .  230

11.7. Filters and Interceptors . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .  232

11.7.1. Server Side Filters . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .  233

11.7.2. Client Side Filters . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .  235

11.7.3. Reader and Writer Interceptors . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .  236

11.8. JAX-RS support for Server Side Events (JAX-RS 2.1) . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .  238

11.8.1. Coding a full Server-Client SSE example . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .  239

11.8.2. Coding a Java Client for Server Side Events . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .  241

11.8.3. SSE Broadcasting . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .  243

12. Developing applications with JMS. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .  245

12.1. Messaging styles . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .  245

12.2. Coding a simple JMS Producer . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .  246

12.2.1. Setting message delivery options . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .  248

12.2.2. Controlling the JMS Session . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .  249

Page 8: Table of Contents · Introduction: Java EE, Jakarta EE and Microprofile The Java Enterprise Edition (Java EE) is a superset of Java Standard Edition (Java SE) that dictates the basic

12.3. Consuming JMS messages . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .  249

12.3.1. Specifying a selector on your MDB . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .  250

12.4. Packaging and compiling your JMS server project . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .  251

12.5. Coding a Standalone JMS Consumer . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .  251

12.6. JMS Application scoped resources . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .  252

12.7. Securing your JMS destinations . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .  253

12.8. Coding JMS remote clients . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .  254

12.8.1. Compiling your JMS client projects . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .  256

13. JSON Processing . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .  257

13.1. JSON Structure . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .  257

13.2. Producing and Consuming JSON data. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .  258

13.2.1. Producing JSON using the Object Model API . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .  258

13.2.1.1. Parsing JSON using the Object Model API . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .  260

13.2.2. Producing JSON using the Streaming API . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .  260

13.2.2.1. Consuming JSON using the Streaming API . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .  261

13.3. Using JSON-B for mapping Java objects to/from JSON . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .  264

13.3.1. Mapping a collection . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .  266

13.3.2. Customized mapping. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .  267

13.4. Compiling your JSON projects . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .  268

14. Batch Processing . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .  269

14.1. Batch processing core elements . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .  269

14.2. Coding a Chunk Steps application . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .  271

14.2.1. Creating a Batch Client . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .  275

14.3. Packaging and deploying your application . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .  277

14.3.1. Defining a checkpoint policy . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .  278

14.4. Coding your first Batchlet . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .  280

14.4.1. Grouping steps in a flow. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .  281

14.4.2. Executing flows in parallel . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .  282

14.4.3. Using Decisions to drive flows . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .  283

14.4.4. Using Listeners in Batch Applications. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .  285

14.4.5. Handling exceptions in Jobs . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .  287

14.4.6. Using Retry Listeners . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .  290

14.4.7. Partitioning Jobs . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .  291

14.4.8. Restarting Jobs . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .  292

15. Jakarta EE Concurrency . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .  294

15.1. Overview of Concurrency Utilities . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .  294

15.2. Using the ManagedExecutorService to submit tasks . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .  294

15.3. Coding a simple Asynchronous Task . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .  295

15.3.1. Retrieving the result from the Asynchronous Task . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .  296

15.3.2. Monitoring the state of a Future Task . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .  298

15.4. Using Transaction in asynchronous Tasks . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .  299

Page 9: Table of Contents · Introduction: Java EE, Jakarta EE and Microprofile The Java Enterprise Edition (Java EE) is a superset of Java Standard Edition (Java SE) that dictates the basic

15.5. Scheduling tasks with the ManagedScheduledExecutorService . . . . . . . . . . . . . . . . . . . . . . . .  300

15.5.1. Submitting a simple ScheduledTask . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .  301

15.5.2. Capturing the result of a Scheduled execution . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .  301

15.6. Creating Managed Threads using the ManagedThreadFactory . . . . . . . . . . . . . . . . . . . . . . . . .  302

15.6.1. Creating Managed Threads from a Factory . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .  302

15.7. Using a Managed Executor Service . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .  303

15.8. Using Dynamic Contextual objects . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .  305

15.8.1. Executing Contextual Tasks . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .  306

15.9. Building examples using Concurrency API . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .  307

16. Securing WildFly applications . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .  307

16.1. Elytron building blocks . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .  308

16.1.1. Default Security Domain and Security Realms . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .  309

16.2. How to enable Elytron for Authentication . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .  310

16.3. Approaching Java Security API . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .  311

16.4. Securing Enterprise applications with Elytron . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .  313

16.4.1. Configuring a File System Security Realm . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .  314

16.4.2. Configuring a JDBC Realm . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .  316

16.5. Securing a Web application . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .  317

16.6. Securing an EJB application . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .  319

16.6.1. Coding a secured EJB application Server . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .  321

16.6.1.1. Coding the EJB Client . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .  322

16.6.1.2. Testing the project . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .  323

16.6.2. Securing EJBs a using declarative approach . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .  323

16.7. Securing the communication layer . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .  324

16.8. Enabling the Secure Socket Layer on WildFly . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .  326

16.8.1. Creating Server and Client certificates . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .  327

16.8.2. Configuring SSL / HTTPS on WildFly . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .  328

16.8.2.1. Encrypting the communication of Web applications . . . . . . . . . . . . . . . . . . . . . . . . . .  330

16.8.2.2. Encrypting the EJB communication . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .  331

16.8.2.3. Updating your client configuration . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .  333

17. Extending Jakarta EE with MicroProfile API . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .  335

17.1. Using the Microprofile Config API with WildFly . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .  337

17.1.1. Using ConfigSource from Class . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .  340

17.2. Using the Microprofile Health Check API with WildFly . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .  342

17.2.1. Readiness health checks . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .  344

17.2.2. Liveness health checks . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .  346

17.3. Using the Microprofile OpenAPI with WildFly . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .  348

17.4. Using the Microprofile OpenTracing API with WildFly . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .  352

17.5. Using the Microprofile Metrics API with WildFly . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .  356

17.6. Using the Microprofile Fault Tolerance API with WildFly . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .  361

17.6.1. Timeout Fault tolerance . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .  361

Page 10: Table of Contents · Introduction: Java EE, Jakarta EE and Microprofile The Java Enterprise Edition (Java EE) is a superset of Java Standard Edition (Java SE) that dictates the basic

17.6.2. Retry Fault tolerance . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .  362

17.6.3. Circuit Breaker Fault Tolerance . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .  363

17.7. Using the Microprofile JWT API with WildFly . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .  364

17.7.1. Using Keycloak as OAuth2 Authorization Server . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .  366

17.7.2. Deploying JWT applications in WildFly . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .  370

17.7.3. Testing the application . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .  373

17.8. Using the Microprofile REST API with WildFly . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .  375

17.8.1. Creating the REST Server Endpoint . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .  375

17.8.2. Creating the REST Client Endpoint . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .  377

17.8.2.1. Configuring the REST client base URL/URI dynamically . . . . . . . . . . . . . . . . . . . . . . . .  379

18. Building Microservices using Thorntail . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .  380

18.1. Getting started with Thorntail . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .  380

18.1.1. Changing the runtime properties. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .  384

18.2. Coding Enterprise applications as Microservices . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .  385

18.3. Building MicroServices using Servlets . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .  387

18.3.1. Creating Hollow Jars from your applications . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .  390

18.4. Building EJB based Microservices . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .  391

18.4.1. Coding Remote EJB Client for Microservices . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .  393

18.5. Building Microservices using CDI . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .  394

18.6. Adding JSF to Microservices . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .  396

18.7. Building Microservices using JPA . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .  397

18.7.1. Configuring Datasource connectivity . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .  400

18.8. Testing Microservices with Arquillian . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .  401

18.9. Building Microservices using Websockets . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .  404

18.10. Building JAX-WS and JAX-RS Microservices . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .  405

18.10.1. Building RESTful Microservices . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .  405

18.10.1.1. Creating a MicroService REST Client . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .  408

18.11. Building SOAP Microservices . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .  409

18.12. Developing Event Driven Microservices . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .  411

18.13. Using JSON in your Microservices . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .  414

18.14. Running Batch Jobs with Microservices . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .  415

18.15. Running parallel tasks in your Microservices . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .  417

18.16. Securing Microservices . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .  418

18.16.1. Keycloak Authentication . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .  418

18.16.1.1. Building the MicroService application . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .  422

18.16.2. JAAS authentication with Thorntail . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .  424

18.16.3. Using Enterprise Security in Thorntail . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .  427

18.16.4. HTTPS Support in Microservices . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .  428

18.17. Using MicroProfile API with Thornail . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .  429

19. Appendix . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .  431

19.1. Sending mails using WildFly. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .  431

Page 11: Table of Contents · Introduction: Java EE, Jakarta EE and Microprofile The Java Enterprise Edition (Java EE) is a superset of Java Standard Edition (Java SE) that dictates the basic
Page 12: Table of Contents · Introduction: Java EE, Jakarta EE and Microprofile The Java Enterprise Edition (Java EE) is a superset of Java Standard Edition (Java SE) that dictates the basic
Page 13: Table of Contents · Introduction: Java EE, Jakarta EE and Microprofile The Java Enterprise Edition (Java EE) is a superset of Java Standard Edition (Java SE) that dictates the basic

Practical Enterprise ApplicationDevelopmentAuthor : Francesco Marchioni

© ItBuzzPress 2020

PrefaceAfter almost 10 years, Oracle gave up on Java Enterprise Edition (JEE) and started spinning Java’sstill-popular enterprise middleware platform to the Eclipse Foundation. Now, under the aegis of theEclipse Foundation, JEE has been renamed to Jakarta EE. The Java EE platform, with its fullecosystem, still provides flexible ways to run applications and is easily extensible for any currenttrends, as well as any future trends to come.

We cannot, however, ignore the new opportunities available in the IT industry that is going throughan epic modernization strategy. In particular, new scenarios are emerging where traditionalmonolithic applications are not the only option but Microservices architecture are proving to be amore agile and productive alternative. The good news is that you can reuse the same skills we havelearned for Java Enterprise Applications in our Microservices, thus reducing substantially thelearning curve for these new architectures.

The first part of the book covers everything from the foundation components (EJB, Servlets, CDI,JPA) to the new technology stack defined in Java Enterprise Edition (now Jakarta EE 8), includingthe new Batch API, JSON-P API, the Concurrency API,Web Sockets, the JMS 2.0 API, the core Webservices stack (JAX-WS, JAX-RS). The testing area with Arquillian framework and the Security API isalso fully covered in this part.

Although the Java Enterprise API is a great set of API for building applications yet it historicallylacks some features which are needed if you develop Microservice-style and cloud-readyapplications. For example, there is no specific API to handle configuration properties, which can beinjected in your services. Nor, there is a formal way to describe how clients can interact with RESTEndpoint. Also, it would definitely help to include some features to monitor application health or toloadbalance requests, which are currently managed by vendors with custom technologies. TheEclipse Microprofile project is a collaboration initiative driven by top application vendors that aimto optimize the Enterprise API for Java applications, including all the features we have mentionedabove.

For this reason, the second part of this book will show you how to integrate the Jakarta EE stackwith Microprofile API.

Finally, in the last part of the book, we will show the configuration changes required to turn ourapplications into lightweight Microservices using the Thorntail runtime environment. We dobelieve that once you have gone through this book, you will have the best of the two worlds at yourhands with the right depth of knowledge.

Red Hat, Red Hat Enterprise Linux, JBoss, are trademarks of Red Hat, Inc., registered in the

1

Page 14: Table of Contents · Introduction: Java EE, Jakarta EE and Microprofile The Java Enterprise Edition (Java EE) is a superset of Java Standard Edition (Java SE) that dictates the basic

United States and other countries.

Linux ® is the registered trademark of Linus Torvalds in the United States and other countries.

Java ® is a registered trademark of Oracle and/or its affiliates.

Microprofile ® is a registered trademark of Eclipse.org foundation, Inc.

The Author of the BookFrancesco Marchioni has been working for Red Hat since 2014. He has joined the JBosscommunity in early 2000 when the application server was a mere EJB container, running release2.x.

In 2008 he started an IT portal focused on JBoss products

(http://www.mastertheboss.com) which is pleased to serve an average of 8000 daily visits.

He has authored the following titles:

JBossAS 5 Development, Packt Publishing (December 2009)

JBoss AS 5 Performance Tuning, Packt Publishing (December 2010)

JBoss AS 7 Configuration, Deployment, and Administration, Packt Publishing (December 2011)

Infinispan Data Grid Platform, Packt Publishing (June 2012) co-authored with Manik Surtani(Infinispan Project lead)

JBoss AS 7 Development, Packt Publishing (June 2013)

Enterprise Application Server CookBook, ItBuzzPress (September 2013)

WildFly Performance Tuning (December 2018)

Hands-On Cloud-Native Applications with Java and Quarkus (December 2019)

WildFly Administration Guide (June 2014 – Updated on March 2020)

In March 2018 Francesco published his first sci-fi novel named Chronicles from a Simulated Worldwhich covers in an earthy and rational style the discussion about the Simulation Hypothesis.

The reviewersLuca Vargetto is a Senior Software Engineer, Java EE & Android Developer graduated at"Politecnico di Torino" University and now working for a company located in the beautiful Sicilyisland. Luca is an enthusiast contributor of JBoss User Group (JUG) Sicily with an eye towards newtechnological horizons.

2

Page 15: Table of Contents · Introduction: Java EE, Jakarta EE and Microprofile The Java Enterprise Edition (Java EE) is a superset of Java Standard Edition (Java SE) that dictates the basic

Who this book is for

If you are a Java architect or developer who wants to get the most out of Enterprise API using thelatest release of WildFly application server, then this book is for you. You are not expected to haveaccumulated experience on the application server though you must know the basic concepts of JavaEE.

How to Contact Us

Please address comments and questions concerning this book to the publisher:[email protected]. We have created a web page for this book, where we list errata, examples,and any other information. You can access this page at:http://www.itbuzzpress.com/news/wildflydevelop-errata.html[http://www.itbuzzpress.com/news/wildflydevelop-errata.html ]

For more information about our books, and future projects see our website at:http://www.itbuzzpress.com

Piracy

The uploading/downloading of copyrighted material without the express written consent of thecontent copyright holder is strictly forbidden. Piracy is an illegal act that you may aspire toreconsider. Besides this, piracy is not a victimless crime! It is financially damaging and personallyhurtful to company employees and their families. Legitimate users suffer as well. We appreciateyour help in protecting the valuable content of this book.

Book Version

This is the version 1.7 of the book – Updated 1 April 2020

Conventions used in this book

This book contains lots of script files and commands to be executed on your machine. Much efforthas been put to make the code as much as readable as possible.

The following script snippet (in a blizzard blue) identifies a command to be executed on youroperating system’s shell:

$ ./jboss-cli.sh

As you can see from the prompt, we have assumed that you are executing on a Linux/Unixmachine. At the beginning of the book, we have also provided the equivalent Windows syntax ofsome core commands:

C:\Users\jboss\wildfly-19.0.0.Final\bin jboss-cli.bat

To avoid being repetitive, we have however used the Linux shell syntax for the rest of the book.

3

Page 16: Table of Contents · Introduction: Java EE, Jakarta EE and Microprofile The Java Enterprise Edition (Java EE) is a superset of Java Standard Edition (Java SE) that dictates the basic

Within the book, you will find also some gray-filled block of code like the following one:

[disconnected /] patch apply /tmp/wildfly-9.0.1.Final.patch

{

"outcome" :"success",

"result" : {}

}

This piece of code identifies a command to be executed within the application server’s CommandLine Interface ([Using the CLI]). Therefore, executing this command in the operating system’s shellwill obviously return an error.

Source code

The source code for this book is hosted on GitHub. The code has been split in three branches:

• The "master" branch of the source code (https://github.com/fmarchioni/practical-enterprise-development/tree/master) is configured to run on WildFly application server using thejakartaee8 dependencies.

• The "thorntail" branch is based on Thorntail fractions to generate the examples:https://github.com/fmarchioni/practical-enterprise-development/tree/thorntail

4

Page 17: Table of Contents · Introduction: Java EE, Jakarta EE and Microprofile The Java Enterprise Edition (Java EE) is a superset of Java Standard Edition (Java SE) that dictates the basic

Introduction: Java EE, Jakarta EE andMicroprofileThe Java Enterprise Edition (Java EE) is a superset of Java Standard Edition (Java SE) thatdictates the basic rules for writing code in Java. The content of Java EE has been reshaped throughthe years, but especially the focus of it has changed a lot since the beginning. The very first releasesof Java EE (1.2, 1.3, 1.4) featured a robust and scalable implementation of industry requirements,yet programming an Enterprise application was not a task for the faint hearts. The EJB specificationwas the living example of it: lots of interfaces to extend, plenty of boilerplate code and somemandatory XML descriptors to include. To make things worse, the code was hard to test and it wasnecessary to arrange for a completely new software infrastructure for creating and maintainingfunctional tests. No surprise that, after broken promises, many programmers turned to newframeworks that started to gain popularity such as Hibernate and Spring. It was in 2006 that wehad the first trend reversal, with the arrival of EJB 3.0 and the new POJO based programmingstandard. The ease of development era was just started; since then Java EE it is finally meeting theinitial promises and attracting again developers.

Here is a map of Java Enterprise core components:

Let’s see more in detail what the new specification is going to buy you, starting from the new (1.0)additions:

5

Page 18: Table of Contents · Introduction: Java EE, Jakarta EE and Microprofile The Java Enterprise Edition (Java EE) is a superset of Java Standard Edition (Java SE) that dictates the basic

Java EE 7 new APIJava API for WebSocket (JSR 356): this specification defines an API for establishing socketconnections between a web browser and a server. In a nutshell, this API leverages a persistentconnection between the client and the server and both parties can start sending data at any timeover a single TCP connection. Both client and server can be POJOs elected as WebSockets endpointsusing annotations or programmatically.

Batch API for Java applications (JSR 352) outlines an XML based Job Specification Language (JSL)used to define batch jobs along with a set of interfaces, abstract classes, and field annotations thatembrace the batch programming model and a batch runtime for running batch jobs.

Java API for JSON Processing (JSR 353) provides a portable APIs to parse, generate, transform, andquery JSON (JavaScript Object Notation) data. This API includes an object model API which creates atree-like structure that represents the JSON data in memory and a streaming API that provides away to parse and generate JSON in a streaming fashion.

Concurrency Utilities for Java EE (JSR 236) provides a standard API for enriching your Java EEapplications with concurrency capabilities without compromising the integrity of your container.

Let’s see the most interesting enhancements contained in Java EE 7 and Java EE 8

Java EE 7 enhancementsContext Dependency Injection 1.1 (JSR 346) includes auto-wiring of CDI beans which now featurea declarative transaction management (CMT) via the @Transactional annotation, plus the additionof new annotations (such as the @Vetoed annotation that allows programmatic disablement ofBeans ) and a rich set of Interceptors.

Java Server Faces 2.2 (JSF 344) features sensible HTML5 support, Resource Library Contracts(which are a kind of Multi-Templating feature) and Faces Flows, which can be used to encapsulate aset of steps guiding the user through the execution of a business tasks.

Java Persistence API 2.1 (JSR 338) which includes now a standard way to execute tasks formerlydelegated to the specific JPA providers such as DDL generation (e.g. automatic table, index andschema generation) and Stored Procedure invocation. The Unsynchronized persistence context anda richer set of Criteria APIs (including Bulk update/delete operations) are also some of the mostinteresting highlights.

JAX-RS 2.0 (JSR 339) further simplifies the development of applications using RESTful Web servicesby including a Client API for async processing, a matching server side asynchronous HTTP responseand the addition of Filters and Interceptors for proxying REST communications.

Bean Validation 1.1 (JSR 349) lets you express constraints on object models via annotations nowallows you to write custom constraints in an extensible way, providing also the APIs to validateparameters and return values of methods and constructors.

JMS 2.0 (JSR 343) is a new major release of JMS, which features a simplified and much shorter wayfor producing and consuming messages by using a single resource (the JMSContext) to wrap JMS

6

Page 19: Table of Contents · Introduction: Java EE, Jakarta EE and Microprofile The Java Enterprise Edition (Java EE) is a superset of Java Standard Edition (Java SE) that dictates the basic

Session and a JMS Connection. Besides this, the core JMS objects now implements thejava.lang.Autocloseable interface to allow them to be used in a Java SE 7 try-with-resourcesstatement. The inclusion of annotation for creating JMS Administered resources is another majorhit of this new specification.

Servlet 3.1 (JSR 340) revamps the Servlet API by allowing the creation of more scalable Servletsthanks to its non-blocking I/O features. Another major hit is the inclusion of the Http protocolUpgrade mechanism, which allows using protocols other than HTTP 1.1 (e.g. the WebSocketprotocol)

EJB 3.2 (JSR345) includes some minor enhancements such as the option to disable Stateful sessionbean passivation and the inclusion of Asynchronous session bean invocations and non-persistentEJB Timer Service in the EJB Lite, which is a smaller subset of EJB API.

Some minor updates are included also in the Java Mail and Java Connector Architecture mostlyto include annotations to inject administered objects registered in the JNDI tree.

Java EE 8 enhancementsContexts and Dependency Injection 2.0 ( JSR 365): Contexts and Dependency Injection 2.0. Thegoal is to make CDI modular and separating CDI into Core CDI, Java SE, Java EE.

JSON Processing 1.1 (JSR 374) : The goal is to include standards like Json pointer, patch and mergepatch.

JSON Binding 1.0 (JSR 367): JSON Binding 1.0. The goal is to provide mapping between JSON dataand Java objects. It provides a standard support to handle JSON media type for JAX-RS.

RESTful Web Services (JAX-RS 2.1): (JSR 370): The goal is to improve server-to-servercommunication, server-sent events, non-blocking I/O, asynchronous clients with reactiveprogramming, hypermedia API and integration with other JSRs like CDI and other frameworks.

JavaTM Servlet 4.0 : (JSR 369): Java Servlet 4.0 Specification. The goal is to support for HTTP/2 andcompatibility with HTTP 1.1. The early draft review is available.

Java Server Faces JSF 2.3 (JSR 372): Java Server Faces JSF 2.3 is a mature technology with someimprovements such as better CDI and WebSocket integration, Ajax method invocation and date andtime support.

Bean Validation 2.0 (JSR 380): Bean Validation 2.0. The goal is to leverage Java 8 languageconstructs, like optionals, date and time API, and repeatable annotations.

Java EE Security (https://jcp.org/en/jsr/detail?id=375): The goal of this specification is to improvethe Java EE platform by ensuring the Security API aspect is useful in the modern cloud/PaaSapplication paradigm. The reference implementation (RI) is called Soteria.

From Java EE to Jakarta EE 8 and beyondSince the first release of this book, some changes happened in the Java Enterprise landscape.

7

Page 20: Table of Contents · Introduction: Java EE, Jakarta EE and Microprofile The Java Enterprise Edition (Java EE) is a superset of Java Standard Edition (Java SE) that dictates the basic

Indeed, Java EE has been donated by Oracle to the Eclipse Foundation and re-branded as JakartaEE. What is Jakarta EE in a nutshell ? It is a platform to accelerate business applicationdevelopment for a cloud-native world. This effort has been led by top software vendors which havecollaborated to move Java EE technologies to the Eclipse Foundation where, as we said, it is nowevolving under the Jakarta EE brand.

The question which immediately follows up is: will developers be able to use the precious Java EEskills also in the Jakarta EE Context ? The answer is yes! Jakarta EE is not a replacement for Java EEAPI. The current Jakarta EE 8 release is functionally equivalent to Java EE 8 so that you cantransparently move your Enterprise processes, specifications, test suites and a compatibleimplementation into the Eclipse Foundation.

Within the scope of Jakarta EE 8 , we can still rely on the javax standards to work on every certifiedapplication server in the same way. This means you don’t have to rewrite your application code torun on Jakarta EE. When it comes to managing the application server, every vendor has a differentand proprietary API for managing its resources. In this book, we will be using WildFly applicationserver and its Command Line Interface to manage its resources.

What about the next version of Jakarta EE, namely Jakarta EE 9 ? The goal of the Jakarta EE 9release is to deliver a set of specifications functionally similar to Jakarta EE 8 but in the newJakarta EE 9 namespace jakarta.*.

In addition, the Jakarta EE 9 release will remove the specifications from Jakarta EE 8 that were old,optional, or deprecated in order to reduce the surface area of the APIs to ensure that it is easier fornew vendors to enter the ecosystem – as well as reduce the burden on implementation, migration,and maintenance of these old APIs.

In addition to classes namespaces, you should take into account also the followingchanges:

• javax.* property names should be renamed to jakarta.* for all propertiesdefined by Jakarta EE 9.

• The proposed target namespace for XML schemas ishttps://jakarta.ee/xml/ns/jakartaee/.

Summing up, the Jakarta EE project team sees the release 9 of Jakarta EE as a tooling release withthe following features:

• An Enterprise platform from which tooling vendors can create and update their tools to supportthe new jakarta.* namespace.

• An Enterprise platform that development teams can use as a stable target for testing migrationof their applications to the new namespace.

• An Enterprise platform that runtime vendors can use to test and deliver options and capabilitiesthat support migration and backwards compatibility with Jakarta EE 8.

• A foundation for innovation that Jakarta EE specification projects can use to drive new featuresfor release in Jakarta EE 10 and beyond.

8

Page 21: Table of Contents · Introduction: Java EE, Jakarta EE and Microprofile The Java Enterprise Edition (Java EE) is a superset of Java Standard Edition (Java SE) that dictates the basic

MicroProfile and Java EEMicroProfile is another initiative within the Enterprise Java ecosystem. MicroProfile was initiatedby multiple vendors to, on the one hand, be able to advance the development of vendor-independent enterprise technology, in a time in which one could not see much progress fromOracle’s side. On the other hand, the ecosystem wanted a way of realizing Microservicedeployments in which the implementations only ship what’s actually required by the individualapplications.

MicroProfile components are built upon the model of Java EE making natural the transition toMicroservices development. Therefore, you will be able to reuse the valuable knowledge of Java EEyou have accumulated in these years while having the flexibility to use multiple vendor specs todefine application requirements.

Here is an overview of the technology stack that is available using the MicroProfile stack:

As you can see, the technology stack mostly focuses on REST and CDI Services for the interactionbetween the Microservices. JSON-P and JSON-B is also included out of the box as common transportlanguage.

There are multiple implementations of MicroProfile on the top of which vendors can add theircustomizations. In this book we will be using SmallRye Microprofile implementation which isbeing used in the following projects:

• Red Hat Quarkus

• Red Hat JBoss EAP / WildFly

• Red Hat Thorntail

• IBM - Open Liberty

In the next part of the book, we will start our journey in the Jakarta EE land by downloadingWildFly application server and the required tools to run our example applications on the top of it.

9

Page 22: Table of Contents · Introduction: Java EE, Jakarta EE and Microprofile The Java Enterprise Edition (Java EE) is a superset of Java Standard Edition (Java SE) that dictates the basic

1. Getting started with Jakarta EEDevelopmentThis is the beginning of our journey through the Jakarta EE ship, using the latest release of WildFlyapplication server. Although we will be sailing the WildFly full-rigged ship across this book, withsome basic administration and configuration skills you will be also able to leverage this knowledgeon other Enterprise containers. We have lots of miles to run so let’s see more in detail the content ofthis chapter:

• We will learn at first how to install WildFly application server

• Next, we will focus on the development tools, specifically the Red Hat JBoss Developer StudioIDE and Maven framework.

1.1. Installing WildFly and Development toolsThe pre-requisite to the Application Server installation is that you have available a JDK on yourmachine.

The minimum requirement to start the current version of WildFly is Java 8,however it has been tested as well with more recent Java versions up to Java 13

The JDK can be either downloaded from Oracle site athttp://www.oracle.com/technetwork/java/javase/downloads/index.html or you can use an opensource implementation of it called OpenJDK http://openjdk.java.net/

Once installed the JDK, you have to set the JAVA_HOME environment variable accordingly.

Windows users: Right click on the My Computer icon on your desktop and select properties. Thenselect the Advanced Tab contained in the Environment Variables button. Under System Variable,click New. Enter the variable name as JAVA_HOME and value the Java install path. Click OK andClick Apply Changes.

Linux users: Enter in your .profile / .bash_profile script the following (substitute with the actualJDK installation path):

export JAVA_HOME=/usr/java/jdk-9

Done with JDK installation, let’s move to the application server. WildFly can be downloaded fromhttp://www.wildfly.org by following the Downloads link in the home page. Once downloaded,extract the archive to a folder and you are done with the installation. For example:

$ unzip wildfly-19.0.0.Final.zip

10

Page 23: Table of Contents · Introduction: Java EE, Jakarta EE and Microprofile The Java Enterprise Edition (Java EE) is a superset of Java Standard Edition (Java SE) that dictates the basic

Through this book we will refer to JBOSS_HOME as the location where you haveinstalled WildFly. As you will see later in this chapter, it is not howevermandatory to set this variable on your operating system to run WildFly.

You can optionally set the JBOSS_HOME environment variable to the location where WildFly isinstalled. This will enable starting the application server, which is located on a different path ofyour file system. For example Linux users:

export JBOSS_HOME=/opt/wildfly-19.0.0.Final

1.1.1. Testing the installed application server

Having completed the installation steps, we will now test the server startup. Move to theJBOSS_HOME/bin folder where you can find the core batch scripts used to operate on theapplication server. There you will find, among the others, a standalone.bat (and the equivalentstandalone.sh for Linux users) and a domain.bat (and its equivalent domain.sh).

Starting the application server in standalone mode will execute a single JVM server process withdifferent set of services, depending on the configuration file that you will select.

Starting the application server in domain mode will trigger the execution of a set of JVMs includinga Domain Controller which is the management control point of your domain, an Host Controllerthat is responsible for coordinating with a Domain Controller the life-cycle of server processes andthe distribution of deployments and a set of application server nodes where you are executing yourapplications.

In order to start the application server in standalone mode execute:

./standalone.sh

To start the application server using the default configuration in "domain" mode, change directoryto $JBOSS_HOME /bin.

./domain.sh

In the server console, you should find something like this, at the end of startup process:

19:14:51,148 INFO [org.jboss.as] (Controller Boot Thread) WFLYSRV0060: Httpmanagement interface listening on http://127.0.0.1:9990/management19:14:51,148 INFO [org.jboss.as] (Controller Boot Thread) WFLYSRV0051: Admin consolelistening on http://127.0.0.1:999019:14:51,149 INFO [org.jboss.as] (Controller Boot Thread) WFLYSRV0025: WildFly Full19.0.0.Final (WildFly Core 10.0.0.Final) started in 5615ms - Started 314 of 535services (321 services are lazy, passive or on-demand)

11

Page 24: Table of Contents · Introduction: Java EE, Jakarta EE and Microprofile The Java Enterprise Edition (Java EE) is a superset of Java Standard Edition (Java SE) that dictates the basic

You can verify that the server is reachable from the network by simply pointing your browser tothe application server’s welcome page, which is reachable by default at the following address:http://localhost:8080

1.1.1.1. Stopping WildFly

The simplest way to stop the application server is by sending an interrupt signal with Ctrl+C to theserver console. Linux/Unix users might as well have a look at the process table with the "ps"command and issue a "kill" to stop the application server.

On the other hand, the recommended approach is to use the Command Line Interface (CLI)interface to issue an immediate shutdown command. The CLI interface can be started from the$JBOSS_HOME/bin folder of your installation:

$ ./jboss-cli.sh

Windows user will start the CLI using the equivalent batch file:

jboss-cli.bat

Once there, issue the connect command:

[disconnected/] connect

Connected to localhost:9990

12

Page 25: Table of Contents · Introduction: Java EE, Jakarta EE and Microprofile The Java Enterprise Edition (Java EE) is a superset of Java Standard Edition (Java SE) that dictates the basic

Now issue the shutdown command that will stop the application server:

[localhost:9990/] shutdown

If you want to issue a command like shutdown in no-interactive mode, you can use as well thefollowing syntax:

$ jboss-cli.sh -c --command=shutdown

Done with the application server, we will now install some tooling required to build our examplesource code. We will start from Maven

1.2. Installing MavenIn order to build and deploy our source code, we will be using Maven, which is most commonsoftware and release management tool. It is used by a variety of developers mostly because itoffers:

• A standard structure for all your projects

• A centralized and automatic management of dependencies

Maven is distributed in several formats for users' convenience. You can downloaded it fromhttps://maven.apache.org/download.cgi

• Unzip the distribution archive (for example, apache-maven-3.6.1-bin.zip ) to the directory inwhich you want Maven to be installed (for example, in your $HOME/apache folder):

$ mkdir $HOME/apache$ unzip $HOME/Downloads/apache-maven-3.6.1-bin.zip -d $HOME/apache

• Add the Maven libraries to your system path as shown. This will update the PATH environmentvariable.

$ export PATH=$PATH:$HOME/apache/apache-maven-3.6.1/bin

• Once you have completed your installation, we need to check whether Maven has beencorrectly installed or not. Run mvn --version to verify this as shown:

13

Page 26: Table of Contents · Introduction: Java EE, Jakarta EE and Microprofile The Java Enterprise Edition (Java EE) is a superset of Java Standard Edition (Java SE) that dictates the basic

$ mvn --version

Apache Maven 3.6.0 (97c98ec64a1fdfee7767ce5ffb20918da4f719f3; 2018-10-24T20:41:47+02:00)Maven home: /home/francesco/apache/apache-maven-3.6.0Java version: 1.8.0_191, vendor: Oracle Corporation, runtime: /usr/lib/jvm/java-1.8.0-openjdk-1.8.0.191.b13-0.fc29.x86_64/jreDefault locale: en_US, platform encoding: UTF-8OS name: "linux", version: "4.18.16-300.fc29.x86_64", arch: "amd64", family: "unix"

Let’s complete the tooling set up by installing a development environment where you can checkand edit the source code for this book.

1.3. Installing a Development EnvironmentThere are several options for editing and designing your Enterprise applications. Most of these toolsare derived from Eclipse (http://www.eclipse.org) and often include the Tools for developingEnterprise Applications. By and large, you can choose any IDE that is capable ofimporting/exporting natively Maven or Gradle projects and a decent set of features for speeding upyour code or refactoring it.

We will be using IntelliJ Idea that is available for download at https://www.jetbrains.com/idea/ . Asyou can see from the Download page (https://www.jetbrains.com/idea/download/ ), both theUltimate and Community versions are available. We will be using the latter, which can be freelydownloaded. Choose to download the latest binary for your Operating System. Then unzip it in afolder of your preference (for example in your Home folder):

$ tar xvzf ideaIC-2019.1.tar.gz -C $HOME

Next, move into the bin folder of the installation and execute it with:

./idea.sh

Let’s have a minimal overview of the development environment’s visual elements.

1.3.1. A bird’s eye view of IntelliJ Idea

Although we don’t focus on a specific development environment to develop Enterprise applications,we will provide a short overview of the visual elements that compose IntelliJ Idea to understandwhat you actions you can do, in a quicker and easier matter. As you can see from the followingscreenshot, these are the main elements of IntelliJ Idea interface:

14

Page 27: Table of Contents · Introduction: Java EE, Jakarta EE and Microprofile The Java Enterprise Edition (Java EE) is a superset of Java Standard Edition (Java SE) that dictates the basic

Let’s have a look at the various highlighted sections:

1. Menu bar: The Menu bar includes the options to create or import projects and other key actionsrelated to projects such as code refactoring, builds, run, debug, version-controlling options, andso on.

2. Tool bar: The Tool bar contains some shortcuts to common execution action such as compile,debug, and run options. You can also customize it according to your requirements.

3. Navigation bar: The Navigation bar enables navigation between sources within a project. Thisfeature comes handy as your code base grows.

4. Tools tab: The Tools tab, which can show up on either side of the main window, lets you accesskey tools such as Maven/Ant builds, databases, and so on.

5. Project perspective: The Project perspective window contains all the elements of your projectsuch as packages, modules, classes, external libraries, and so on.

6. Editor window: This is where you edit your code in IntelliJ Idea, using advanced features suchas syntax highlighting, smart completion, quick-fix suggestions, and other useful features.

Great! Let’s check how to import the book’s example code into IntelliJ Idea.

1.3.2. Importing the book example code into your IDE

The book examples are part of a Maven project which can be imported from the File Menu. Start bydownloading the examples from https://github.com/fmarchioni/practical-enterprise-development

You can choose to download the project files as a zip archive or clone them usinggit. When you are cloning the Github repository, you will be able to propose PullRequests to the source code, in case you find any error or bug

Once downloaded the source code, choose File | Open and point to the location where you haveunpacked the examples:

15

Page 28: Table of Contents · Introduction: Java EE, Jakarta EE and Microprofile The Java Enterprise Edition (Java EE) is a superset of Java Standard Edition (Java SE) that dictates the basic

In a snap, you will see in your project Panel the examples for the repository you have selected:

Before starting to cover the single Enterprise modules, let’s briefly check how you can debug the

16

Page 29: Table of Contents · Introduction: Java EE, Jakarta EE and Microprofile The Java Enterprise Edition (Java EE) is a superset of Java Standard Edition (Java SE) that dictates the basic

examples with WildFly

1.4. Debugging WildFly with IntelliJ IdeaRemote debugging is a key feature that is necessary to track issues on your server side applications.In order to activate remote debugging on the application server, a set of JVM arguments needs to beenabled at application server startup. You can enable them in the file standalone.conf which islocated in the bin folder of the application server:

# Sample JPDA settings for remote socket debuggingJAVA_OPTS="$JAVA_OPTS-agentlib:jdwp=transport=dt_socket,address=8787,server=y,suspend=n"n

Next, set up one or more break-points into any of your server side classes. Now you can start theapplication server as usual with:

./standalone.sh

You will notice that the server log contains the following line, just at the beginning of it:

Listening for transport dt_socket at address: 8787

Next, you need to attach your IDE the Remote Socket started by WildFly on Port 8787. The simplestway to do that is to select Run | Attach to Process. The runnable process of WildFly will bedetected, as you can see from the following snapshot:

Now, if you try to access to the source code which contains a breakpoint, the execution will stop andyou will be able to manage the standard debugging flow fro the Debugger panel:

After the program has been suspended, you can use the debugger to get the information about the

17

Page 30: Table of Contents · Introduction: Java EE, Jakarta EE and Microprofile The Java Enterprise Edition (Java EE) is a superset of Java Standard Edition (Java SE) that dictates the basic

state of the program and how it changes during running.

The debugger provides you with the information about variable values, objects breakdown, thecurrent state of the threads, and so on. It also allows you to test your program in various conditionsby throwing exceptions or running arbitrary code right in the middle of the program execution.

While these tools let you examine the program state at a particular instant, the stepping featuregives you the control over step-by-step execution of the program. By combining the tools you candeduce where the bug is coming from and test your program for robustness.

18