table of contents - it buzz press · 14.4. packaging and compiling your jms server project . . . ....

30

Upload: others

Post on 23-Mar-2020

1 views

Category:

Documents


0 download

TRANSCRIPT

Page 1: Table of Contents - IT Buzz Press · 14.4. Packaging and compiling your JMS server project . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 251 14.5. Coding
Page 2: Table of Contents - IT Buzz Press · 14.4. Packaging and compiling your JMS server project . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 251 14.5. Coding

Table of Contents1. Practical Enterprise Application Development . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .  1

1.1. Preface . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .  1

1.2. The Author of the Book . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .  1

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

1.3.1. Who this book is for . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .  2

1.3.2. How to Contact Us . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .  2

1.3.3. Piracy . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .  2

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

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

1.3.6. Source code . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .  3

2. Introduction: Java EE, Jakarta EE and Microprofile . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .  4

2.1. Java EE 7 new API: . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .  5

2.2. Java EE 7 enhancements: . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .  5

2.3. Java EE 8 enhancements. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .  6

2.4. From Java EE to Jakarta EE . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .  6

2.5. MicroProfile and Java EE . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .  8

3. Getting started with WildFly . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .  9

3.1. Installing WildFly . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .  9

3.1.1. Testing the installed application server . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .  10

3.1.1.1. Stopping WildFly. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .  12

3.2. Installing Red Hat JBoss Developer Studio. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .  13

3.2.1. Connecting JDBS to WildFly . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .  13

3.2.2. Remote debugging with JBDS . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .  15

3.3. Installing Maven. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .  16

3.3.1. Testing the Installation . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .  17

4. Getting ready for development . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .  18

4.1. Using Maven archetypes for your projects . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .  18

4.1.1. Using the webapp-javaee7 archetype . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .  19

4.1.2. Using the ejb-javaee7 archetype . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .  20

4.1.3. Using the appclient-javaee7 archetype . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .  21

4.1.4. Using the ear-javaee7 archetype. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .  22

4.2. Creating Maven Projects from JBDS . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .  23

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

4.3.1. Project coordinates . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .  25

4.4. Packaging . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .  26

4.4.1. Properties . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .  26

4.4.2. Project Dependencies . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .  26

4.5. The pom’s build section . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .  28

Page 3: Table of Contents - IT Buzz Press · 14.4. Packaging and compiling your JMS server project . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 251 14.5. Coding

4.5.1. Managing application deployment . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .  29

5. Programming Servlets . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .  30

5.1. Defining Servlets . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .  30

5.1.1. Registering a Servlet Dynamically . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .  32

5.1.2. Using init Parameters in the your Servlets . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .  33

5.1.3. Using Filters in your Servlets. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .  33

5.1.4. Using Servlet Listeners in your applications . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .  34

5.1.4.1. Listening to your ServletContext . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .  35

5.1.4.2. Listening to your ServletContext attribute changes . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .  35

5.1.4.3. Listening to your HttpSession creation and disposal . . . . . . . . . . . . . . . . . . . . . . . . . . . . .  35

5.1.4.4. Listening to changes in HttpSession attributes . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .  36

5.1.4.5. Listening to changes in HttpSession activation and passivation . . . . . . . . . . . . . . . . . .  36

5.1.4.6. Listening to bindings in HttpSession . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .  37

5.1.4.7. Listening to Http Request creation and disposal. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .  38

5.2. Asynchronous support . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .  38

5.2.1. Coding an asynchronous Servlet . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .  38

5.3. Non-Blocking I/O in Servlets . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .  41

5.4. Using Server Push . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .  44

5.4.1. Using the PushBuilder Interface . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .  44

5.4.1.1. Using the Push Strategy with Servlet Filters . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .  45

5.5. Compiling applications using Servlets . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .  46

6. Developing Enterprise Java Beans. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .  48

6.1. Overview of Enterprise Java Beans . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .  48

6.2. Coding a Stateless Session Bean . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .  49

6.3. Coding a Stateful Session Bean . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .  49

6.3.1. Compiling and deploying the project . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .  50

6.3.2. Controlling Passivation for Stateful EJBs . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .  51

6.4. Coding EJB Clients . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .  52

6.4.1. Coding the remote EJB client . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .  53

6.4.1.1. Configuring your Remote EJB Client . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .  55

6.4.2. Configuring your Client’s Maven dependencies . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .  57

6.4.3. Running the client class . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .  58

6.4.3.1. Using jboss-ejb-client.properties configuration file . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .  59

6.5. The other type of Beans . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .  59

6.5.1. Coding Singleton EJBs . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .  59

6.5.1.1. Controlling Singleton Concurrency . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .  60

6.6. Using Bean Managed Concurrency . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .  61

6.7. Coding Asynchronous EJBs . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .  61

6.8. Coding EJB Timers . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .  63

6.8.1. Defining programmatic timers using @Timeout annotation . . . . . . . . . . . . . . . . . . . . . . . . .  63

6.8.2. Defining Automatic timers using the @Schedule annotation . . . . . . . . . . . . . . . . . . . . . . . . .  64

Page 4: Table of Contents - IT Buzz Press · 14.4. Packaging and compiling your JMS server project . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 251 14.5. Coding

6.8.3. Getting a list of active timers . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .  65

6.8.4. Timers and Persistence . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .  65

6.9. Configuring Transactions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .  66

6.9.1. Container Managed Transactions. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .  66

6.9.2. Container Managed Transactions and Exceptions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .  68

6.9.3. Bean Managed Transactions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .  69

6.9.3.1. Compiling EJBs with BMT . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .  70

7. Context Dependency Injection . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .  71

7.1. Overview of CDI and Dependency Injection . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .  71

7.1.1. Learning CDI Scopes . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .  72

7.1.2. Naming your Beans . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .  73

7.1.2.1. Using the Model annotation . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .  74

7.2. Managing the Bean configuration . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .  74

7.2.1. Using filter expressions in your beans.xml . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .  75

7.2.2. Vetoed Beans . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .  76

7.3. Beans Producers and Disposers . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .  77

7.4. Combining pieces to create a simple example . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .  78

7.4.1. Coding the Java Bean class . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .  79

7.4.2. Including a Qualifier . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .  79

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

7.4.4. Coding the SessionScoped CDI Bean . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .  81

7.4.5. Coding the User Interface . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .  82

7.4.6. Building and Running the example . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .  82

7.4.7. Adding Qualifiers members . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .  82

7.5. Adding Interceptors on Producers (CDI 2.0) . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .  83

7.6. Implementing Interceptor Beans . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .  86

7.6.1. Multiple interceptors . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .  87

7.6.2. Inheriting Interceptors . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .  88

7.6.3. Compiling classes with Interceptors . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .  89

7.7. Implementing Decorator Beans . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .  89

7.8. Implementing Alternative Beans . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .  90

7.9. Using Conversation Scope in your Beans . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .  92

7.10. Managing Events with CDI. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .  92

7.10.1. Firing events asynchronously . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .  93

7.10.2. Adding Qualifiers to your Observers . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .  94

7.11. Transactions and CDI . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .  95

7.12. Running CDI outside of WildFly . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .  98

8. Java Server Faces . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .  101

8.1. Introduction to JSF. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .  101

8.2. Creating your first Java Server Faces application . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .  101

8.3. Building the application Model . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .  102

Page 5: Table of Contents - IT Buzz Press · 14.4. Packaging and compiling your JMS server project . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 251 14.5. Coding

8.3.1. Building the view . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .  104

8.3.2. Compiling and deploying your application . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .  106

8.3.3. A bit of style . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .  107

8.4. JSF survival kit . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .  107

8.4.1. Using JSF facets to simplify JSF development . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .  107

8.4.2. Showing messages in your JSF applications . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .  108

8.4.3. Showing debugging information in your pages . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .  109

8.5. Validating your data . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .  110

8.6. Bean Validation 2.0 . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .  111

8.6.1. Constraints in Parameterized types . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .  112

8.6.2. Cascaded Validation of Containers . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .  113

8.6.3. Building applications using Bean Validation 2.0 . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .  113

8.7. Enhancing your application with Facelets . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .  113

8.7.1. Creating Composite components . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .  115

8.7.2. Caching Facelets . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .  117

8.8. Navigation using Java Server Faces . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .  118

8.8.1. Standard JSF navigation . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .  118

8.8.2. Implicit JSF Navigation . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .  118

8.8.3. JSF Conditional Navigation . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .  119

8.9. JSF 2.2 goodies . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .  120

8.9.1. Using Faces Flow in your JSF application . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .  120

8.9.1.1. Mixing multiple flows . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .  123

8.9.2. Using Resource Library contracts . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .  127

8.9.2.1. Reusing library contracts in your applications . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .  130

8.9.3. HTML 5 and JSF . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .  130

8.9.4. Passing attributes from the server side . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .  131

8.9.5. Pass-through Elements . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .  132

8.9.6. Uploading files with JSF . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .  132

8.10. JSF 2.3 goodies . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .  135

8.10.1. Using Command Script . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .  136

8.10.2. Networking and WebSockets updates . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .  138

8.10.2.1. WebSocket Usage: Client Side . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .  138

8.10.2.2. WebSocket Usage: Server Side . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .  138

8.10.3. Validation and conversion enhancements . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .  140

8.10.4. Map Iteration in UIData and UIRepeat . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .  141

8.10.5. Deprecation of Managed Beans . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .  143

8.10.6. JSF Events after view is rendered . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .  145

8.11. Class level bean validation . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .  148

8.11.1. Using Server Push with JSF . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .  151

9. Learning Java Persistence API . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .  152

9.1. Introducing JPA . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .  152

Page 6: Table of Contents - IT Buzz Press · 14.4. Packaging and compiling your JMS server project . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 251 14.5. Coding

9.2. Working with JPA . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .  152

9.2.1. Setting up a database connection . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .  153

9.2.1.1. Configuring the Datasource using the CLI . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .  153

9.3. Setting up a sample Project . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .  154

9.3.1. The JPA Configuration file . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .  156

9.3.2. Adding an Entity Manager to our application . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .  158

9.3.3. Compiling a JPA Project . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .  159

9.3.4. Coding a Test class for our JPA project . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .  160

9.4. Focus on Schema generation . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .  160

9.5. Using Named queries . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .  161

9.6. Other ways to perform CRUD operations . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .  162

9.6.1. Using Criteria Queries . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .  162

9.6.1.1. Performing Bulk Updates and Deletes using Criteria Queries. . . . . . . . . . . . . . . . . . . .  163

9.6.2. Executing Native SQL queries . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .  164

9.7. Adding Validation to your Entities . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .  165

9.7.1. Using built-in constraints . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .  166

9.7.2. Available built-in constraints . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .  166

9.7.3. Creating Custom Constraints . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .  169

9.8. Calling Stored Procedures . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .  170

9.9. Caching data . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .  172

9.9.1. Enabling Hibernate Cache . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .  173

9.10. Managing JPA Persistence context . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .  174

9.11. Using Listeners in your Entities . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .  175

10. Testing applications using Arquillian. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .  177

10.1. Moving from unit tests to Arquillian . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .  177

10.2. Introducing Arquillian . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .  180

10.3. Setting up a Maven project for Arquillian . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .  181

10.3.1. Compiling your project . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .  183

10.3.2. Changing the defaults of remote environment. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .  186

10.3.3. Running a test in a managed environment . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .  186

11. Developing applications with WebSockets . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .  189

11.1. Overview of WebSockets . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .  189

11.1.1. Into the WebSocket protocol . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .  189

11.2. Coding your First WebSocket application . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .  190

11.2.1. Coding a simple Javascript client . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .  192

11.2.2. Compiling the example . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .  193

11.3. Websockets messaging styles . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .  194

11.4. Creating a Java WebSocket Endpoint . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .  195

11.5. Using Encoders and Decoders to send custom Java types . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .  195

11.6. Sending data asynchronously. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .  200

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

Page 7: Table of Contents - IT Buzz Press · 14.4. Packaging and compiling your JMS server project . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 251 14.5. Coding

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

11.7. Sending binary content . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .  201

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

11.8.1. Using Path parameters in Endpoints . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .  203

11.9. Defining an Endpoint Configurator Class . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .  204

12. Developing SOAP Based Web services . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .  206

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

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

12.3. Creating the Web Service Project . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .  207

12.3.1. Developing the Implementation class . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .  209

12.3.2. Deploying the Web service . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .  210

12.3.3. Creating a WS Test client . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .  211

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

12.4. Customizing the Web Service Properties . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .  214

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

12.5.1. Coding SOAP Handlers . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .  217

12.5.2. Coding Logical Handlers . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .  219

13. Developing RESTful Web services . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .  221

13.1. Entering RESTFul Web services . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .  221

13.2. Coding RESTFul Web services . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .  221

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

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

13.4. RESTful Web services client API . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .  226

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

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

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

13.7. Filters and Interceptors . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .  232

13.7.1. Server Side Filters . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .  232

13.7.2. Client Side Filters . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .  235

13.7.3. Reader and Writer Interceptors . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .  236

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

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

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

13.8.3. SSE Broadcasting . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .  243

14. Developing applications with JMS. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .  245

14.1. Messaging styles . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .  245

14.2. Coding a simple JMS Producer . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .  246

14.2.1. Setting message delivery options . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .  248

14.2.2. Controlling the JMS Session . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .  249

14.3. Consuming JMS messages . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .  249

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

Page 8: Table of Contents - IT Buzz Press · 14.4. Packaging and compiling your JMS server project . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 251 14.5. Coding

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

14.5. Coding a Standalone JMS Consumer . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .  251

14.6. JMS Application scoped resources . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .  252

14.7. Securing your JMS destinations . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .  253

14.8. Coding JMS remote clients . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .  254

14.8.1. Compiling your JMS client projects . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .  256

15. JSON Processing . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .  257

15.1. JSON Structure . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .  257

15.2. Producing and Consuming JSON data. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .  258

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

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

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

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

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

15.3.1. Mapping a collection . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .  267

15.3.2. Customized mapping. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .  267

15.4. Compiling your JSON projects . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .  268

16. Batch Processing . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .  269

16.1. Batch processing core elements . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .  269

16.2. Coding a Chunk Steps application . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .  271

16.2.1. Creating a Batch Client . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .  275

16.3. Packaging and deploying your application . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .  277

16.3.1. Defining a checkpoint policy . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .  278

16.4. Coding your first Batchlet . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .  280

16.4.1. Grouping steps in a flow. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .  281

16.4.2. Executing flows in parallel . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .  282

16.4.3. Using Decisions to drive flows . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .  283

16.4.4. Using Listeners in Batch Applications. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .  285

16.4.5. Handling exceptions in Jobs . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .  287

16.4.6. Using Retry Listeners . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .  290

16.4.7. Partitioning Jobs . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .  291

16.4.8. Restarting Jobs . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .  292

17. Java EE Concurrency . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .  294

17.1. Overview of Concurrency Utilities . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .  294

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

17.3. Coding a simple Asynchronous Task . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .  295

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

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

17.4. Using Transaction in asynchronous Tasks . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .  299

17.5. Scheduling tasks with the ManagedScheduledExecutorService . . . . . . . . . . . . . . . . . . . . . . . .  300

17.5.1. Submitting a simple ScheduledTask . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .  301

Page 9: Table of Contents - IT Buzz Press · 14.4. Packaging and compiling your JMS server project . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 251 14.5. Coding

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

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

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

17.7. Using a Managed Executor Service . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .  303

17.8. Using Dynamic Contextual objects . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .  305

17.8.1. Executing Contextual Tasks . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .  306

17.9. Building examples using Concurrency API . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .  307

18. Securing WildFly applications . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .  307

18.1. Elytron building blocks . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .  308

18.1.1. Default Security Domain and Security Realms . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .  309

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

18.3. Approaching Java Security API . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .  311

18.4. Securing Enterprise applications with Elytron . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .  313

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

18.4.2. Configuring a JDBC Realm . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .  316

18.5. Securing a Web application . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .  317

18.6. Securing an EJB application . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .  319

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

18.6.1.1. Coding the EJB Client . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .  322

18.6.1.2. Testing the project . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .  323

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

18.7. Securing the communication layer . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .  324

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

18.8.1. Creating Server and Client certificates . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .  327

18.8.2. Configuring One-Way SSL / HTTPS on WildFly . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .  328

18.8.3. Configuring Mutual SSL Authentication on WildFly . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .  330

18.8.3.1. Encrypting the EJB communication . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .  332

18.8.3.2. Updating your client configuration . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .  332

19. Building Microservices using Thorntail . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .  336

19.1. Getting started with Thorntail . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .  336

19.1.1. Changing the runtime properties. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .  340

19.2. Coding Enterprise applications as Microservices . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .  341

19.3. Building MicroServices using Servlets . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .  343

19.3.1. Creating Hollow Jars from your applications . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .  346

19.4. Building EJB based Microservices . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .  347

19.4.1. Coding Remote EJB Client for Microservices . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .  349

19.5. Building Microservices using CDI . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .  350

19.6. Adding JSF to Microservices . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .  352

19.7. Building Microservices using JPA . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .  353

19.7.1. Configuring Datasource connectivity . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .  356

19.8. Testing Microservices with Arquillian . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .  357

Page 10: Table of Contents - IT Buzz Press · 14.4. Packaging and compiling your JMS server project . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 251 14.5. Coding

19.9. Building Microservices using Websockets . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .  360

19.10. Building JAX-WS and JAX-RS Microservices . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .  361

19.10.1. Building RESTful Microservices . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .  361

19.10.1.1. Creating a MicroService REST Client . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .  364

19.11. Building SOAP Microservices . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .  365

19.12. Developing Event Driven Microservices . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .  367

19.13. Using JSON in your Microservices . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .  370

19.14. Running Batch Jobs with Microservices . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .  371

19.15. Running parallel tasks in your Microservices . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .  373

19.16. Securing Microservices . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .  374

19.16.1. Keycloak Authentication . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .  374

19.16.1.1. Building the MicroService application . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .  378

19.16.2. JAAS authentication with Thorntail . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .  380

19.16.3. Using Java EE Security in Thorntail. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .  383

19.16.4. HTTPS Support in Microservices . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .  384

19.17. Extending Enterprise applications with MicroProfile API . . . . . . . . . . . . . . . . . . . . . . . . . . . .  385

19.17.1. Managing Configuration with the MicroProfile Config component . . . . . . . . . . . . . . . .  385

19.17.1.1. Using ConfigSource from Class . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .  387

19.17.2. MicroProfile Health Checks . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .  388

19.17.2.1. Using the Health API with Containers . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .  391

19.17.3. MicroProfile OpenAPI . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .  392

19.17.4. MicroProfile OpenTracing . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .  394

19.17.5. MicroProfile Metrics . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .  398

19.17.6. Fault tolerance . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .  401

19.17.6.1. Timeout Fault tolerance . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .  401

19.17.6.2. Retry Fault tolerance . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .  402

19.17.6.3. Circuit Breaker Fault Tolerance . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .  403

20. Appendix . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .  405

20.1. Sending mails using WildFly. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .  405

Page 11: Table of Contents - IT Buzz Press · 14.4. Packaging and compiling your JMS server project . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 251 14.5. Coding
Page 12: Table of Contents - IT Buzz Press · 14.4. Packaging and compiling your JMS server project . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 251 14.5. Coding
Page 13: Table of Contents - IT Buzz Press · 14.4. Packaging and compiling your JMS server project . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 251 14.5. Coding

1. Practical Enterprise ApplicationDevelopmentAuthor : Francesco Marchioni

© ItBuzzPress 2019

1.1. 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. In the first part of this book, we will learn through clearexamples how to develop Jakarta EE Services which can be executed on a traditional JEEapplication server like WildFly Application Server. In the second part of the book, we will show theconfiguration changes required to turn our applications into lightweight Microservices 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 theUnited States and other countries.

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

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

1.2. The Author of the BookFrancesco Marchioni is a Quality Engineer working at 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)

1

Page 14: Table of Contents - IT Buzz Press · 14.4. Packaging and compiling your JMS server project . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 251 14.5. Coding

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 Administration Guide (June 2014 – Updated on March 2018)

Practical Java EE Development on WildFly (June 2014 – Updated on April 2018)

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.

1.3. 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.

1.3.1. 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.

1.3.2. 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

1.3.3. 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.

2

Page 15: Table of Contents - IT Buzz Press · 14.4. Packaging and compiling your JMS server project . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 251 14.5. Coding

1.3.4. Book Version

This is the version 1.5 of the book – Updated 10 June 2019

1.3.5. 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-12.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.

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.

1.3.6. Source code

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

• Enterprise Examples: https://github.com/fmarchioni/practical-enterprise-development/

• Thorntail and MicroProfile Examples: https://github.com/fmarchioni/practical-enterprise-development/tree/thorntail

3

Page 16: Table of Contents - IT Buzz Press · 14.4. Packaging and compiling your JMS server project . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 251 14.5. Coding

2. 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:

4

Page 17: Table of Contents - IT Buzz Press · 14.4. Packaging and compiling your JMS server project . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 251 14.5. Coding

2.1. Java EE 7 new API:Java 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

2.2. Java EE 7 enhancements:Context 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

5

Page 18: Table of Contents - IT Buzz Press · 14.4. Packaging and compiling your JMS server project . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 251 14.5. Coding

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 nonpersistentEJB 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.

2.3. Java EE 8 enhancements.Contexts 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.

2.4. From Java EE to Jakarta EESince the first release of this book, some changes happened in the Java Enterprise landscape.

6

Page 19: Table of Contents - IT Buzz Press · 14.4. Packaging and compiling your JMS server project . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 251 14.5. Coding

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, leading software vendors are collaborating to move Java EEtechnologies to the Eclipse Foundation where, as we said, they will evolve under the Jakarta EEbrand.

The next question is: will developers be able to use the precious Java EE skills also in the Jakarta EEContext ? The answer is yes! Jakarta EE is not supposed to be a replacement for Java EE API. Theupcoming Jakarta EE 8 release will be functionally equivalent to Java EE 8 in order to be able totransfer from Oracle all the Enterprise processes, specifications, test suites and a compatibleimplementation through the Eclipse Foundation.

As for Jakarta EE 8, we can still rely on the javax standards to work on every certified applicationserver in the same way. This means you don’t have to rewrite your application code to run onJakarta EE. When it comes to managing the application server, every vendor has a different andproprietary 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 future releases of Jakarta EE? the Eclipse foundation has no restriction on using thejavax namespace. Neither will APIs that currently exist be deleted. But any API that is created orupdated within the newly formed Jakarta EE specification process will need to live in a newnamespace that is most likely imitating the one in existence but with jakarta as its prefix instead ofjavax. For example, if a new method was to be added to the javax.servlet.Servlet interface, thenext version of the servlet specification would need to publish a new interface namedjakarta.servlet.Servlet instead of adding this method to the existing API.

APIs moved to the jakarta namespace maintain class names and method signatures compatiblewith equivalent class names and method signatures in the javax.* namespace. Although binarycompatibility for existing applications in the javax namespace is an agreed goal by the majority ofexisting vendors in the Jakarta EE Working Group and would be a priority in their products, thereis a also strong desire not to deter new implementers of the jakarta namespace from entering theecosystem by requiring they also implement an equivalent javax legacy API.

It is envisioned binary compatibility can be achieved and offered byimplementations via tooling that performs bytecode modification at either build-time, deploy-time or runtime.

Regarding future Enterprise components, the key areas which are going to be predominant in theJakarta EE context are mostly:

• Enhanced support for Microservices architecture

• Move to Cloud Native Java, which includes better integrations with technologies like Docker andKubernetes

7

Page 20: Table of Contents - IT Buzz Press · 14.4. Packaging and compiling your JMS server project . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 251 14.5. Coding

Summing up, Jakarta EE 8 will be exactly the same as Java EE 8. The onlydifference is it will be licensed from Jakarta, not Oracle and only requiresmembership in the Working Group. Future versions of Jakarta EE will be underthe new jakarta.* packaging. Jakarta EE will be inherently community-based,multi-vendor, and open to contribution by the enterprise consumers of thesetechnologies. In the past, the Java Community Process (JCP) has done a good job atintegrating the community it serves by being more open and transparent.However, it is something ultimately controlled by Oracle, and its intellectualproperty rules gave the Spec Lead a distinct advantage relative to the othercompetitors.

2.5. 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. (In blue you can see recently updated API in the Microprofile 2.0 spec) There are multipleimplementations of MicroProfile on the top of which vendors can add their customizations. Forexample, if you want to add Persistence stack to your Microservices, then you can use Thorntailpersistence implementation which is the well-known JPA Stack. Throntail is not the only availableoption to develop with MicroProfile. Here is a list of the top options:

• Red Hat - Thorntail

8

Page 21: Table of Contents - IT Buzz Press · 14.4. Packaging and compiling your JMS server project . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 251 14.5. Coding

• Red Hat - Red Hat Application Runtimes

• IBM - WebSphere Liberty

• IBM - Open Liberty

• Payara Foundation - Payara Micro

• Payara Foundation - Payara Server

• Tomitribe - TomEE

• Oracle - Helidon

• Fujitsu - Launcher

• SmallRye

• Hammock

• KumuluzEE

As Eclipse MicroProfile continues to define new APIs, there is a growing interest and hope thatJakarta EE and Microprofile will be working closely together. For example by Eclipse MicroProfilebeing an incubator for APIs which get consumed by Jakarta EE. On the other hand, they might aswell work just as two peers, but complementary projects. In terms of release strategy, theMicroProfile API will be more focused around a rapid release strategy and faster innovation basedupon real-world experiences. Enterprise development so far traditionally focused on major multi-year stable platform releases. We will see if Jakarta EE will be able to leverage MicroProfile’ssuccess and experience in this area.

We will now start learning the core Enterprise API that will be deployed on a Java EE compatibleapplication server, like WildFly. In the second part of this book, we will show how to run ourexamples with the Thorntail project, which is built from the from ground-up to be MicroProfile-native. We will also learn how to add Microprofile API to our application to extend the features ofour applications.

3. Getting started with WildFlyThis chapter sets the beginning of our journey through the Jakarta EE ship, using the latest releaseof WildFly application server. Although we will be sailing the WildFly full-rigged ship across thisbook, with some basic administration and configuration skills you will be also able to leverage thisknowledge on other Enterprise containers. We have lots of miles to run so let’s see more in detailthe content of this 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.

3.1. Installing WildFlyThe pre-requisite to the Application Server installation is that you have available a JDK on yourmachine.

9

Page 22: Table of Contents - IT Buzz Press · 14.4. Packaging and compiling your JMS server project . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 251 14.5. Coding

It is recommended to use a JDK 9 or higher to start WildFly

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-17.0.0.Final.zip

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-17.0.0.Final

3.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 Controller

10

Page 23: Table of Contents - IT Buzz Press · 14.4. Packaging and compiling your JMS server project . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 251 14.5. Coding

that 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 Full17.0.0.Final (WildFly Core 9.0.0.Final) started in 5615ms - Started 314 of 535services (321 services are lazy, passive or on-demand)

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

11

Page 24: Table of Contents - IT Buzz Press · 14.4. Packaging and compiling your JMS server project . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 251 14.5. Coding

3.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

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

12

Page 25: Table of Contents - IT Buzz Press · 14.4. Packaging and compiling your JMS server project . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 251 14.5. Coding

[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

3.2. Installing Red Hat JBoss Developer StudioJBoss Developer Studio is a free and powerful tool to use for developing applications with WildFly.This IDE is substantially an Eclipse (http://www.eclipse.org) on steroids which already includes theTools for developing Enterprise Applications. If you already use Eclipse and don’t want to installanother IDE, then you can just skip this section and install JBoss Tools plugins(https://tools.jboss.org/) on your Eclipse.

You can download the installer of JBDS fromhttps://developers.redhat.com/products/devstudio/download/

Please note that to download this product, you must have an account and acceptthe terms and conditions of the Red Hat Developer Program, which provides no-cost subscriptions for development use only.

Once downloaded, navigate to the folder that contains the JBoss Developer Studio JAR file andexecute the installer:

$ java -jar devstudio-12.9.0.GA-installer-standalone.jar

3.2.1. Connecting JDBS to WildFly

Now, you should be able to see the WildFly server enlisted as a server by choosing New | Serverfrom the upper menu and expanding the JBoss Community option. Pick up the latest versionavailable of the Server plugin (even if it’s not up-to-date with the latest WildFly release):

13

Page 26: Table of Contents - IT Buzz Press · 14.4. Packaging and compiling your JMS server project . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 251 14.5. Coding

In your first Server definition, you will have to Add a Runtime environment to your server byclicking on the Add link and selecting your application server home.

14

Page 27: Table of Contents - IT Buzz Press · 14.4. Packaging and compiling your JMS server project . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 251 14.5. Coding

Click on Finish to complete your server installation. Now you should be able to use the Start/Stopbutton on the Server tab to trigger the server startup and shutdown. The tiny beetle icon can beused to Debug your applications, and besides it a watch button that can be used to Tune theapplication server.

3.2.2. Remote debugging with JBDS

Remote debugging is a key feature that is necessary to track issues on your server side applications.

15

Page 28: Table of Contents - IT Buzz Press · 14.4. Packaging and compiling your JMS server project . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 251 14.5. Coding

In order to activate remote debugging on the application server a set of JVM arguments needs to beenabled at application server startup:

-agentlib:jdwp=transport=dt_socket,address=8787,server=y,suspend=n

Luckily, JBoss Developer Studio does it for you transparently, therefore it’s enough to start theapplication server in debug mode. Let’s see in a few simple steps how to do it:

Set up a break-point into any of your server side classes:

Now start up the application server in debug mode:

Target your application on WildFly application server, by choosing the Add and Remove optionavailable by right-clicking on the WildFly Server

Then, as soon as your application hits a breakpoint, you will be notified by Eclipse which suggests achange of perspective, stopping the execution at the breakpoint:

3.3. Installing MavenCreating applications using the Eclipse/JBoss Developer Studio environment is the quickest way to

16

Page 29: Table of Contents - IT Buzz Press · 14.4. Packaging and compiling your JMS server project . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 251 14.5. Coding

provide a simple Enterprise application. Nevertheless, as your application grows in complexity, sodoes the amount of libraries to be used (and especially the dependencies between them). For thisreason, you are strongly encouraged to use Maven that is the de facto standard tool for project andrelease management. Maven is distributed in several formats and can be downloaded from thislink: http://maven.apache.org/download.html

Once that you have completed the download, unzip the distribution archive (for example, apache-maven-3.5.3-bin.zip) to the directory where you wish to install Maven.

Next, add the M2_HOME environment variable to your system, so that it will point to the folderwhere Maven has been unpacked. Next, update the PATH environment variable by adding theMaven binaries to your system path. For example, on the Windows platform, you should include%M2_HOME%/bin, to make Maven available to the command line.

3.3.1. Testing the Installation

Once completed with your installation, run mvn --version, to verify that Maven has been correctlyinstalled:

$ 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"

17

Page 30: Table of Contents - IT Buzz Press · 14.4. Packaging and compiling your JMS server project . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 251 14.5. Coding

4. Getting ready for developmentBefore diving into the examples contained in this book, we need at first some directions for settingup, compiling and deploying your projects using Maven. What you will learn in this chapter will beyour survivor kit in the jungle of Enterprise applications. So, before turning you into a first citizenEnterprise developer, you need to learn the following topics:

• How to create your projects using Maven archetypes

• How to configure the Maven’s project configuration file (pom.xml)

• How to build and deploy applications using WildFly’s Maven plugin

4.1. Using Maven archetypes for your projectsAs we said, we will be using Maven for setting up, compiling and deploying your applications.Although Eclipse and JBoss Developer Studio have some other viable alternatives for creatingEnterprise projects (such as Dynamic Web projects), by using Maven for this purpose you willachieve some important benefits such as:

• A well-defined standard project structure

• Automatic dependency management

• Build life-cycle management

• Quick project setup by using archetypes

The latter point will be our first touchdown with this great framework. In a nutshell, an archetypeis a Maven project templating toolkit; you can think of it like a template for a kind of projects. Lotsof archetypes are available and you can contribute as well with your own projects. Using anarchetype is pretty simple and can be achieved in two ways: interactively or in batch mode. Youcan enter in Maven’s interactive option by executing:

$ mvn archetype:generate

Next, you have to fill up the required parameters so that you match a specific version of anarchetype.

If you accept the default artifact, you will begin with theorg.apache.maven.archetypes:maven-archetype-quickstart which is a simpleJava project to get started with Maven.

18