table of contents - it buzz press · table of contents 1. practical java ee development on wildfly...

20

Upload: others

Post on 05-Jul-2020

4 views

Category:

Documents


0 download

TRANSCRIPT

Page 1: Table of Contents - IT Buzz Press · Table of Contents 1. Practical Java EE Development on WildFly . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
Page 2: Table of Contents - IT Buzz Press · Table of Contents 1. Practical Java EE Development on WildFly . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .

Table of Contents1. Practical Java EE Development on WildFly . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .  1

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

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

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

1.4. What this book covers . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .  2

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

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

1.4.3. Piracy . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .  4

1.4.4. Book Version . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .  4

1.4.5. Conventions used in this book. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .  4

1.4.6. Source code . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .  5

2. Chapter 1: Getting started with WildFly . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .  6

2.1. Java EE at first glance . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .  6

2.1.1. Java EE 7 new API: . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .  7

2.1.2. Java EE 7 enhancements: . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .  8

2.1.3. Java EE 8 enhancements. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .  8

2.2. Installing WildFly application server . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .  9

2.2.1. Testing the installed application server . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .  10

2.2.1.1. Stopping WildFly. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .  11

2.3. Installing Red Hat JBoss Developer Studio. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .  12

2.3.1. Connecting JDBS to WildFly . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .  12

2.3.2. Remote debugging with JBDS . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .  13

2.4. Installing NetBeans environment . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .  14

2.4.1. Adding WildFly server to Netbeans . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .  15

2.5. Installing Maven. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .  16

2.5.1. Testing the Installation . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .  17

3. Chapter 2: Getting ready for development . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .  18

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

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

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

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

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

3.2. Creating Maven Projects from JBDS . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .  23

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

3.3.1. Project coordinates . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .  25

3.4. Packaging . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .  26

3.4.1. Properties . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .  26

3.4.2. Project Dependencies . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .  26

Page 3: Table of Contents - IT Buzz Press · Table of Contents 1. Practical Java EE Development on WildFly . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .

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

3.5.1. Managing application deployment . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .  29

4. Chapter 3: Programming Servlets . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .  30

4.1. Defining Servlets . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .  30

4.1.1. Registering a Servlet Dynamically . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .  32

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

4.1.3. Using Filters in your Servlets. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .  33

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

4.1.4.1. Listening to your ServletContext . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .  35

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

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

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

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

4.1.4.6. Listening to bindings in HttpSession . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .  37

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

4.2. Asynchronous support . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .  38

4.2.1. Coding an asynchronous Servlet . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .  38

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

4.4. Compiling applications using Servlets . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .  44

4.5. Using Server Push . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .  45

4.5.1. Using the PushBuilder Interface . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .  46

4.5.1.1. Using the Push Strategy with Servlet Filters . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .  47

5. Chapter 4: Developing Enterprise Java Beans . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .  48

5.1. Overview of Enterprise Java Beans . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .  48

5.2. Coding a Stateless Session Bean . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .  49

5.3. Coding a Stateful Session Bean . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .  49

5.3.1. Compiling and deploying the project . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .  50

5.3.2. Controlling Passivation for Stateful EJBs . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .  51

5.4. Coding EJB Clients . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .  52

5.4.1. Coding the remote EJB client . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .  53

5.4.1.1. Configuring your Remote EJB Client . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .  55

5.4.2. Configuring your Client’s Maven dependencies . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .  56

5.4.3. Running the client class . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .  58

5.4.3.1. Using jboss-ejb-client.properties configuration file . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .  58

5.5. The other type of Beans . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .  59

5.5.1. Coding Singleton EJBs . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .  59

5.5.1.1. Controlling Singleton Concurrency . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .  60

5.6. Using Bean Managed Concurrency . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .  60

5.7. Coding Asynchronous EJBs . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .  61

5.8. Coding EJB Timers . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .  62

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

Page 4: Table of Contents - IT Buzz Press · Table of Contents 1. Practical Java EE Development on WildFly . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .

5.8.2. Defining Automatic timers using the @Schedule annotation . . . . . . . . . . . . . . . . . . . . . . . . .  63

5.8.3. Getting a list of active timers . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .  64

5.8.4. Timers and Persistence . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .  65

5.9. Configuring Transactions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .  65

5.9.1. Container Managed Transactions. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .  66

5.9.2. Container Managed Transactions and Exceptions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .  68

5.9.3. Bean Managed Transactions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .  68

5.9.3.1. Compiling EJBs with BMT . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .  69

6. Chapter 5: Context Dependency Injection . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .  70

6.1. Overview of CDI and Dependency Injection . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .  70

6.1.1. Learning CDI Scopes . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .  71

6.1.2. Naming your Beans . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .  72

6.1.2.1. Using the Model annotation . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .  73

6.2. Managing the Bean configuration . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .  73

6.2.1. Using filter expressions in your beans.xml . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .  74

6.2.2. Vetoed Beans . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .  75

6.3. Beans Producers and Disposers . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .  76

6.4. Combining pieces to create a simple example . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .  77

6.4.1. Coding the Java Bean class . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .  78

6.4.2. Including a Qualifier . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .  79

6.4.3. Creating a Producer for our Bean . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .  79

6.4.4. Coding the SessionScoped CDI Bean . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .  80

6.4.5. Coding the User Interface . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .  81

6.4.6. Building and Running the example . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .  82

6.4.7. Adding Qualifiers members . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .  82

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

6.6. Implementing Interceptor Beans . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .  86

6.6.1. Multiple interceptors . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .  87

6.6.2. Inheriting Interceptors . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .  88

6.6.3. Compiling classes with Interceptors . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .  89

6.7. Implementing Decorator Beans . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .  89

6.8. Implementing Alternative Beans . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .  90

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

6.10. Managing Events with CDI. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .  92

6.10.1. Firing events asynchronously . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .  93

6.10.2. Adding Qualifiers to your Observers . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .  94

6.11. Transactions and CDI . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .  95

6.12. Running CDI outside of WildFly . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .  98

7. Chapter 6: Java Server Faces . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .  101

7.1. Introduction to JSF. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .  101

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

Page 5: Table of Contents - IT Buzz Press · Table of Contents 1. Practical Java EE Development on WildFly . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .

7.3. Building the application Model . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .  102

7.3.1. Building the view . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .  104

7.3.2. Compiling and deploying your application . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .  106

7.3.3. A bit of style . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .  107

7.4. JSF survival kit . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .  107

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

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

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

7.5. Validating your data . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .  110

7.6. Bean Validation 2.0 . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .  111

7.6.1. Constraints in Parameterized types . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .  112

7.6.2. Cascaded Validation of Containers . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .  113

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

7.7. Enhancing your application with Facelets . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .  113

7.7.1. Creating Composite components . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .  115

7.7.2. Caching Facelets . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .  117

7.8. Navigation using Java Server Faces . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .  118

7.8.1. Standard JSF navigation . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .  118

7.8.2. Implicit JSF Navigation . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .  118

7.8.3. JSF Conditional Navigation . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .  119

7.9. JSF 2.2 goodies . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .  120

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

7.9.1.1. Mixing multiple flows . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .  123

7.9.2. Using Resource Library contracts . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .  127

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

7.9.3. HTML 5 and JSF . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .  130

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

7.9.5. Pass-through Elements . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .  132

7.9.6. Uploading files with JSF . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .  132

7.10. JSF 2.3 goodies . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .  135

7.10.1. Using Command Script . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .  136

7.10.2. Networking and WebSockets updates . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .  138

7.10.2.1. WebSocket Usage: Client Side . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .  138

7.10.2.2. WebSocket Usage: Server Side . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .  138

7.10.3. Validation and conversion enhancements . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .  140

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

7.10.5. Deprecation of Managed Beans . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .  143

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

7.11. Class level bean validation . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .  148

7.11.1. Using Server Push with JSF . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .  151

8. Chapter 7: Learning Java Persistence API . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .  152

Page 6: Table of Contents - IT Buzz Press · Table of Contents 1. Practical Java EE Development on WildFly . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .

8.1. Introducing JPA . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .  152

8.2. Working with JPA . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .  152

8.2.1. Setting up a database connection . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .  153

8.2.2. Creating the datasource . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .  153

8.3. Setting up a sample Project . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .  154

8.3.1. The JPA Configuration file . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .  156

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

8.3.3. Coding a Test class for our JPA project . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .  159

8.4. Focus on Schema generation . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .  160

8.5. Using Named queries . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .  161

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

8.6.1. Using Criteria Queries . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .  162

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

8.6.2. Executing Native SQL queries . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .  164

8.7. Adding Validation to your Entities . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .  165

8.7.1. Using built-in constraints . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .  165

8.7.2. Available built-in constraints . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .  166

8.7.3. Creating Custom Constraints . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .  168

8.8. Calling Stored Procedures . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .  170

8.9. Caching data . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .  171

8.9.1. Enabling Hibernate Cache . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .  172

8.10. Managing JPA Persistence context . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .  173

8.11. Using Listeners in your Entities . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .  174

9. Chapter 8: Testing applications using Arquillian. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .  176

9.1. Moving from unit tests to Arquillian. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .  176

9.2. Introducing Arquillian . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .  179

9.3. Setting up a Maven project for Arquillian . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .  180

9.3.1. Compiling your project . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .  182

9.3.2. Changing the defaults of remote environment . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .  185

9.3.3. Running a test in a managed environment . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .  185

10. Chapter 9: Developing applications with WebSockets . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .  188

10.1. Overview of WebSockets . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .  188

10.1.1. Into the WebSocket protocol . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .  188

10.2. Coding your First WebSocket application . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .  189

10.2.1. Coding a simple Javascript client . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .  191

10.2.2. Compiling the example . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .  192

10.3. Websockets messaging styles . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .  193

10.4. Creating a Java WebSocket Endpoint . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .  194

10.5. Using Encoders and Decoders to send custom Java types . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .  194

10.6. Sending data asynchronously. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .  199

10.6.1. Option 1: the J2SE Future object approach . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .  199

Page 7: Table of Contents - IT Buzz Press · Table of Contents 1. Practical Java EE Development on WildFly . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .

10.6.2. Option 2: Implementing an Handler . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .  200

10.7. Sending binary content . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .  200

10.8. Maintaining Client State and other advanced features . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .  201

10.8.1. Using Path parameters in Endpoints . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .  202

10.9. Defining an Endpoint Configurator Class . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .  203

11. Chapter 10: Developing SOAP Based Web services . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .  205

11.1. Developing SOAP-based Web services . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .  205

11.2. Strategies for building SOAP web services . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .  206

11.3. Creating the Web Service Project . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .  206

11.3.1. Developing the Implementation class . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .  208

11.3.2. Deploying the Web service . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .  209

11.3.3. Creating a WS Test client . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .  210

11.3.4. Creating a native Apache CXF Client . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .  212

11.4. Customizing the Web Service Properties . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .  213

11.5. Using Handlers in your Web services . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .  214

11.5.1. Coding SOAP Handlers . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .  216

11.5.2. Coding Logical Handlers . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .  218

12. Chapter 11: Developing RESTful Web services . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .  220

12.1. Entering RESTFul Web services . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .  220

12.2. Coding RESTFul Web services . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .  220

12.2.1. Packaging and deploying your REST Web service . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .  222

12.3. Using parameters in your REST services . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .  223

12.4. RESTful Web services client API . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .  225

12.4.1. Compiling and Running the Test example . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .  227

12.5. Asynchronous JAX-RS Server API . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .  228

12.6. Asynchronous JAX-RS Client API . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .  229

12.7. Filters and Interceptors . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .  231

12.7.1. Server Side Filters . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .  231

12.7.2. Client Side Filters . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .  234

12.7.3. Reader and Writer Interceptors . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .  235

12.8. JAX-RS support for Server Side Events (JAX-RS 2.1) . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .  237

12.8.1. Coding a full Server-Client SSE example . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .  238

12.8.2. Coding a Java Client for Server Side Events . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .  240

12.8.3. SSE Broadcasting . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .  242

13. Chapter 12: Developing applications with JMS . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .  244

13.1. Messaging styles . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .  244

13.2. Coding a simple JMS Producer . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .  245

13.2.1. Setting message delivery options . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .  247

13.2.2. Controlling the JMS Session . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .  248

13.3. Consuming JMS messages . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .  248

13.3.1. Specifying a selector on your MDB . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .  249

Page 8: Table of Contents - IT Buzz Press · Table of Contents 1. Practical Java EE Development on WildFly . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .

13.4. Packaging and compiling your JMS server project . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .  250

13.5. Coding a Standalone JMS Consumer . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .  250

13.6. JMS Application scoped resources . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .  251

13.7. Securing your JMS destinations . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .  252

13.8. Coding JMS remote clients . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .  253

13.8.1. Compiling your JMS client projects . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .  255

14. Chapter 13: JSON Processing. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .  256

14.1. JSON Structure . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .  256

14.2. Producing and Consuming JSON data. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .  257

14.2.1. Producing JSON using the Object Model API . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .  257

14.2.1.1. Parsing JSON using the Object Model API . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .  259

14.2.2. Producing JSON using the Streaming API . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .  259

14.2.2.1. Consuming JSON using the Streaming API . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .  260

14.3. Using JSON-B for mapping Java objects to/from JSON . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .  263

14.3.1. Mapping a collection . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .  267

14.3.2. Customized mapping. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .  267

14.4. Compiling your JSON projects . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .  268

15. Chapter 14: Batch Processing . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .  269

15.1. Batch processing core elements . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .  269

15.2. Coding a Chunk Steps application . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .  271

15.2.1. Creating a Batch Client . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .  275

15.3. Packaging and deploying your application . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .  277

15.3.1. Defining a checkpoint policy . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .  278

15.4. Coding your first Batchlet . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .  279

15.4.1. Grouping steps in a flow. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .  281

15.4.2. Executing flows in parallel . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .  281

15.4.3. Using Decisions to drive flows . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .  282

15.4.4. Using Listeners in Batch Applications. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .  284

15.4.5. Handling exceptions in Jobs . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .  286

15.4.6. Using Retry Listeners . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .  289

15.4.7. Partitioning Jobs . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .  290

15.4.8. Restarting Jobs . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .  291

16. Chapter 15: Java EE Concurrency . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .  293

16.1. Overview of Concurrency Utilities . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .  293

16.2. Using the ManagedExecutorService to submit tasks . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .  293

16.3. Coding a simple Asynchronous Task . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .  294

16.3.1. Retrieving the result from the Asynchronous Task . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .  295

16.3.2. Monitoring the state of a Future Task . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .  297

16.4. Using Transaction in asynchronous Tasks . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .  298

16.5. Scheduling tasks with the ManagedScheduledExecutorService . . . . . . . . . . . . . . . . . . . . . . . .  299

16.5.1. Submitting a simple ScheduledTask . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .  300

Page 9: Table of Contents - IT Buzz Press · Table of Contents 1. Practical Java EE Development on WildFly . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .

16.5.2. Capturing the result of a Scheduled execution . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .  300

16.6. Creating Managed Threads using the ManagedThreadFactory . . . . . . . . . . . . . . . . . . . . . . . . .  301

16.6.1. Creating Managed Threads from a Factory . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .  301

16.7. Using a Managed Executor Service . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .  302

16.8. Using Dynamic Contextual objects . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .  304

16.8.1. Executing Contextual Tasks . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .  305

16.9. Building examples using Concurrency API . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .  306

17. Chapter 16: Securing WildFly applications . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .  306

17.1. Elytron building blocks . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .  307

17.1.1. Default Security Domain and Security Realms . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .  308

17.2. How to enable Elytron for Authentication . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .  309

17.3. Approaching Java Security API . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .  310

17.4. Securing Enterprise applications with Elytron . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .  312

17.4.1. Configuring a File System Security Realm . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .  313

17.4.2. Configuring a JDBC Realm . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .  315

17.5. Securing a Web application . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .  316

17.6. Securing an EJB application . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .  318

17.6.1. Coding a secured EJB application Server . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .  320

17.6.1.1. Coding the EJB Client . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .  321

17.6.1.2. Testing the project . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .  322

17.6.2. Securing EJBs a using declarative approach . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .  322

17.7. Securing the communication layer . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .  323

17.8. Enabling the Secure Socket Layer on WildFly . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .  325

17.8.1. Creating Server and Client certificates . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .  326

17.8.2. Configuring One-Way SSL / HTTPS on WildFly . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .  327

17.8.3. Configuring Mutual SSL Authentication on WildFly . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .  329

17.8.3.1. Encrypting the EJB communication . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .  331

17.8.3.2. Updating your client configuration . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .  331

18. Appendix . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .  334

18.1. Sending mails using WildFly. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .  334

Page 10: Table of Contents - IT Buzz Press · Table of Contents 1. Practical Java EE Development on WildFly . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
Page 11: Table of Contents - IT Buzz Press · Table of Contents 1. Practical Java EE Development on WildFly . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
Page 12: Table of Contents - IT Buzz Press · Table of Contents 1. Practical Java EE Development on WildFly . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .

1. Practical Java EE Development on WildFlyAuthor : Francesco Marchioni

© ItBuzzPress 2018

1.1. PrefaceWildFly carries an important inheritance: it’s the successor of the JBoss application servercommunity edition, which has been an important reference for worldwide developers in the lastdecade. The new server release includes several administration enhancements such as the newRole Based Access Control that allows a granular selection of the administration capabilities, amodern Web container (Undertow Webserver) that supports the latest standards of webdevelopment and support for a Patching system allowing automatic server upgrades. Most of all,WildFly provides full support for Enterprise applications, which in its latest version (WildFly 12) isbased on EE8 capabilities. Walking through such a large API stack can be daunting both for a readerand for a book writer. In particular, as author, the greatest challenge is to sort out all the new andold technologies and propose them in a simple yet effective way so that the reader becomesproficient in the least amount of time. We do believe that once you have gone through this book,you will have a rich enterprise development platform at your hands and the right depth ofknowledge to run them on WildFly application server (with minimal administration skills, also onother Java EE containers).

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)

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

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

1

Page 13: Table of Contents - IT Buzz Press · Table of Contents 1. Practical Java EE Development on WildFly . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .

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.4. What this book coversChapter 1, Getting started with WildFly covers the installation of the server platform andintroduces the Java EE platform. You will learn as well how to install the tools required fordeveloping applications

Chapter 2, Getting ready for development discusses the basic steps for configuring thefoundation of your Maven projects using the available WildFly archetypes.

Chapter 3, Programming Servlet is a first access to application development using the long-livedServlet API. The chapter spans from the basics to the new Java EE 7 non-blocking I/O feature.

Chapter 4, Developing Enterprise Java Beans, is about developing applications using theavailable EJB types, thus including the basic Stateless and Stateful beans, and the other variants(EJB Timers and Singleton EJB).

Chapter 5, Context Dependency Injection discusses about the CDI API starting from the basicsand then diving into more advanced concepts like Interceptors, Decorators, Alternatives, Eventsand the new Transactional option.

Chapter 6, Java Server Faces covers the Java Server Faces API with particular focus on buildingapplications using Facelets, shaping navigation between JSF views and the new exciting features ofJava Server Faces 2.2 and 2.3

Chapter 7, Learning the Java Persistence API after a quick introduction to Java Persistence API,describes how to set up a proof of concept project, how to spice it up using JPA 2.1 new features.Finally, this chapter describes some advanced development strategies such as Caching data andListeners.

2

Page 14: Table of Contents - IT Buzz Press · Table of Contents 1. Practical Java EE Development on WildFly . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .

Chapter 8, Testing applications using Arquillian is a deep dive into the application serverintegration testing framework. You will learn how to test our application across several differentcontexts, including a managed environment, a remote environment and an OpenShift cloud-hostedenvironment.

Chapter 9, Developing applications with WebSockets teaches you how to leverage a full client-server WebSockets application including advanced examples using Encoders/Decoders andAsynchronous communication.

Chapter 10, Developing SOAP based Web services discusses about creating, deploying, andtesting web services using the JBoss JAX-WS implementation (Apache CXF) and advanced featuressuch as JAX-WS Handlers.

Chapter 11, Developing RESTful Web services covers concrete examples of REST Web servicesincluding the new JAX-RS client API and some advanced topics such as Interceptors and Filters.

Chapter 12, Developing applications with JMS introduces the basics of the messaging service,showing off how to use the new JMS 2.0 API to code producers and consumers and how to createremote WildFly JMS clients.

Chapter 13, JSON Processing discusses, after a quick introduction to JSON Structure, somepractical aspects of JSON such as creating and parsing JSON models using the Object Model API andthe JSON Streaming API.

Chapter 14, Batch Processing covers Batch jobs and its specification language as specified by JSR-352. In this chapter, you will learn the two core components of batch jobs: Chunks and Batchletsand some advanced concepts such as Flows and Decision flows.

Chapter 15, Java EE Concurrency introduces the Java EE Concurrency API (JSR 236) whichoutlines a standard way for executing more tasks in parallel on a Java EE Container using a set ofManaged resources.

Chapter 16, Securing WildFly applications gets you quickly to know WildFly security subsystem,showing how to set up login modules to Web applications and EJB applications and how to use theSecure Sockets Layer (SSL) to encrypt their communication.

1.4.1. Who this book is for

If you are a Java architect or developer who wants to get the most out of Java EE API using the latestrelease 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.4.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 ]

3

Page 15: Table of Contents - IT Buzz Press · Table of Contents 1. Practical Java EE Development on WildFly . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .

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

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

1.4.4. Book Version

This is the version 1.4 of the book – Updated 1 May 2018

1.4.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" : {}

}

4

Page 16: Table of Contents - IT Buzz Press · Table of Contents 1. Practical Java EE Development on WildFly . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .

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.4.6. Source code

Check out the source code for this book on GitHub at: https://github.com/fmarchioni/practical-javaee7-development-wildfly/tree/javaee8

5

Page 17: Table of Contents - IT Buzz Press · Table of Contents 1. Practical Java EE Development on WildFly . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .

2. Chapter 1: Getting started with WildFlyThis chapter sets the beginning of our journey through the Java Enterprise Edition (Java EE inshort) on-board of the latest release of WildFly application server. Although we will be sailing theWildFly full-rigged ship across this book, with some basic administration and configuration skillsyou will be also able to leverage this knowledge on other Java EE Enterprise containers. We havelots of miles to run so let’s see more in detail the content of this chapter:

• We will start with a quick introduction to Java EE and its most significant news

• Then we will learn how to install WildFly application server

• In the latter part of this chapter, we will focus on the development tools, specifically the Red HatJBoss Developer Studio and NetBeans IDEs and Maven framework.

2.1. Java EE at first glanceThe 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.

The current focus in the current edition of Java EE is on the integration between differenttechnologies, using Context Dependency Injection as glue between them; at the same time, severalbrand new specifications have been released, including support in some key areas such as HTML5development, Batch executions and JSON processing that formerly needed a third-party set of APIto run on the application server container.

Here is a map of Java Enterprise core components:

6

Page 18: Table of Contents - IT Buzz Press · Table of Contents 1. Practical Java EE Development on WildFly . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .

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

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

7

Page 19: Table of Contents - IT Buzz Press · Table of Contents 1. Practical Java EE Development on WildFly . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .

2.1.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 JMSSession 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.1.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 merge

8

Page 20: Table of Contents - IT Buzz Press · Table of Contents 1. Practical Java EE Development on WildFly . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .

patch.

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.

2.2. Installing WildFly application serverBefore downloading and installing the application server, you need to check for the availability ofJDK 1.8 or higher on your machine. If you are missing this requirement, or you have an older Javaenvironment on your machine, you can download it from the following link:http://www.oracle.com/technetwork/java/javase/downloads/index.html.

Once installed the JDK, you have to set the JAVA_HOME environment variable accordingly. See thefollowing frame if you don’t know how to do it:

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 Javainstall path. Click OK and Click Apply Changes.

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

export JAVA_HOME=/opt/java/jdk1.8.0_60

Once you have completed your installation, run java -version to verify that it is correctly installed:

$ java -version

java version "1.8.0_60"Java(TM) SE Runtime Environment (build 1.8.0_60-b27)Java HotSpot(TM) 64-Bit Server VM (build 25.60-b23, mixed mode)

9