Document Information

Preface

Part I Introduction

1.  Overview

2.  Using the Tutorial Examples

Part II The Web Tier

3.  Getting Started with Web Applications

4.  JavaServer Faces Technology

5.  Introduction to Facelets

6.  Expression Language

7.  Using JavaServer Faces Technology in Web Pages

8.  Using Converters, Listeners, and Validators

9.  Developing with JavaServer Faces Technology

10.  JavaServer Faces Technology: Advanced Concepts

11.  Using Ajax with JavaServer Faces Technology

12.  Composite Components: Advanced Topics and Example

13.  Creating Custom UI Components and Other Custom Objects

14.  Configuring JavaServer Faces Applications

15.  Java Servlet Technology

16.  Uploading Files with Java Servlet Technology

17.  Internationalizing and Localizing Web Applications

Part III Web Services

18.  Introduction to Web Services

19.  Building Web Services with JAX-WS

20.  Building RESTful Web Services with JAX-RS

21.  JAX-RS: Advanced Topics and Example

Part IV Enterprise Beans

22.  Enterprise Beans

23.  Getting Started with Enterprise Beans

24.  Running the Enterprise Bean Examples

25.  A Message-Driven Bean Example

26.  Using the Embedded Enterprise Bean Container

27.  Using Asynchronous Method Invocation in Session Beans

Part V Contexts and Dependency Injection for the Java EE Platform

28.  Introduction to Contexts and Dependency Injection for the Java EE Platform

29.  Running the Basic Contexts and Dependency Injection Examples

30.  Contexts and Dependency Injection for the Java EE Platform: Advanced Topics

31.  Running the Advanced Contexts and Dependency Injection Examples

Part VI Persistence

32.  Introduction to the Java Persistence API

33.  Running the Persistence Examples

34.  The Java Persistence Query Language

35.  Using the Criteria API to Create Queries

36.  Creating and Using String-Based Criteria Queries

37.  Controlling Concurrent Access to Entity Data with Locking

38.  Using a Second-Level Cache with Java Persistence API Applications

Part VII Security

39.  Introduction to Security in the Java EE Platform

40.  Getting Started Securing Web Applications

41.  Getting Started Securing Enterprise Applications

42.  Java EE Security: Advanced Topics

Part VIII Java EE Supporting Technologies

43.  Introduction to Java EE Supporting Technologies

44.  Transactions

45.  Resources and Resource Adapters

46.  The Resource Adapter Example

47.  Java Message Service Concepts

48.  Java Message Service Examples

Writing Simple JMS Applications

A Simple Example of Synchronous Message Receives

Writing the Clients for the Synchronous Receive Example

Starting the JMS Provider

JMS Administered Objects for the Synchronous Receive Example

Running the Clients for the Synchronous Receive Example

A Simple Example of Asynchronous Message Consumption

Writing the Clients for the Asynchronous Receive Example

To Build and Package the AsynchConsumer Client Using NetBeans IDE

To Deploy and Run the Clients for the Asynchronous Receive Example Using NetBeans IDE

To Build and Package the AsynchConsumer Client Using Ant

To Deploy and Run the Clients for the Asynchronous Receive Example Using Ant and the appclient Command

A Simple Example of Browsing Messages in a Queue

Writing the Client for the QueueBrowser Example

To Run the MessageBrowser Client Using NetBeans IDE

To Run the MessageBrowser Client Using Ant and the appclient Command

Running JMS Clients on Multiple Systems

To Create Administered Objects for Multiple Systems

Changing the Default Host Name

To Run the Clients Using NetBeans IDE

To Run the Clients Using Ant and the appclient Command

Undeploying and Cleaning the Simple JMS Examples

Writing Robust JMS Applications

A Message Acknowledgment Example

To Run ackequivexample Using NetBeans IDE

To Run ackequivexample Using Ant

A Durable Subscription Example

To Run durablesubscriberexample Using NetBeans IDE

To Run durablesubscriberexample Using Ant

A Local Transaction Example

To Run transactedexample Using NetBeans IDE

To Run transactedexample Using Ant and the appclient Command

An Application That Uses the JMS API with a Session Bean

Writing the Application Components for the clientsessionmdb Example

Coding the Application Client: MyAppClient.java

Coding the Publisher Session Bean

Coding the Message-Driven Bean: MessageBean.java

Creating Resources for the clientsessionmdb Example

Running the clientsessionmdb Example

To Run the clientsessionmdb Example Using NetBeans IDE

To Run the clientsessionmdb Example Using Ant

An Application Example That Consumes Messages from a Remote Server

Overview of the consumeremote Example Modules

Writing the Module Components for the consumeremote Example

Creating Resources for the consumeremote Example

Using Two Application Servers for the consumeremote Example

Running the consumeremote Example

To Run the consumeremote Example Using NetBeans IDE

To Run the consumeremote Example Using Ant

An Application Example That Deploys a Message-Driven Bean on Two Servers

Overview of the sendremote Example Modules

Writing the Module Components for the sendremote Example

Coding the Application Client: MultiAppServerClient.java

Coding the Message-Driven Bean: ReplyMsgBean.java

Creating Resources for the sendremote Example

To Enable Deployment on the Remote System

To Use Two Application Servers for the sendremote Example

Running the sendremote Example

To Run the sendremote Example Using NetBeans IDE

To Run the sendremote Example Using Ant

To Disable Deployment on the Remote System

49.  Bean Validation: Advanced Topics

50.  Using Java EE Interceptors

Part IX Case Studies

51.  Duke's Bookstore Case Study Example

52.  Duke's Tutoring Case Study Example

53.  Duke's Forest Case Study Example

Index

 

An Application That Uses the JMS API with an Entity

This section explains how to write, compile, package, deploy, and run an application that uses the JMS API with an entity. The application uses the following components:

  • An application client that both sends and receives messages

  • Two message-driven beans

  • An entity class

You will find the source files for this section in the tut-install/examples/jms/clientmdbentity/ directory. Path names in this section are relative to this directory.

Overview of the clientmdbentity Example Application

This application simulates, in a simplified way, the work flow of a company’s human resources (HR) department when it processes a new hire. This application also demonstrates how to use the Java EE platform to accomplish a task that many JMS applications need to perform.

A JMS client must often wait for several messages from various sources. It then uses the information in all these messages to assemble a message that it then sends to another destination. The common term for this process is joining messages. Such a task must be transactional, with all the receives and the send as a single transaction. If not all the messages are received successfully, the transaction can be rolled back. For an application client example that illustrates this task, see A Local Transaction Example.

A message-driven bean can process only one message at a time in a transaction. To provide the ability to join messages, an application can have the message-driven bean store the interim information in an entity. The entity can then determine whether all the information has been received; when it has, the entity can report this back to one of the message-driven beans, which then creates and sends the message to the other destination. After it has completed its task, the entity can be removed.

The basic steps of the application are as follows.

  1. The HR department’s application client generates an employee ID for each new hire and then publishes a message (M1) containing the new hire’s name, employee ID, and position. The client then creates a temporary queue, ReplyQueue, with a message listener that waits for a reply to the message. (See Creating Temporary Destinations for more information.)

  2. Two message-driven beans process each message: One bean, OfficeMDB, assigns the new hire’s office number, and the other bean, EquipmentMDB, assigns the new hire’s equipment. The first bean to process the message creates and persists an entity named SetupOffice, then calls a business method of the entity to store the information it has generated. The second bean locates the existing entity and calls another business method to add its information.

  3. When both the office and the equipment have been assigned, the entity business method returns a value of true to the message-driven bean that called the method. The message-driven bean then sends to the reply queue a message (M2) describing the assignments. Then it removes the entity. The application client’s message listener retrieves the information.

Figure 48-4 illustrates the structure of this application. Of course, an actual HR application would have more components; other beans could set up payroll and benefits records, schedule orientation, and so on.

Figure 48-4 assumes that OfficeMDB is the first message-driven bean to consume the message from the client. OfficeMDB then creates and persists the SetupOffice entity and stores the office information. EquipmentMDB then finds the entity, stores the equipment information, and learns that the entity has completed its work. EquipmentMDB then sends the message to the reply queue and removes the entity.

Figure 48-4 An Enterprise Bean Application: Client to Message-Driven Beans to Entity

Diagram of application showing an application client, two message-driven beans, and an entity

Writing the Application Components for the clientmdbentity Example

Writing the components of the application involves coding the application client, the message-driven beans, and the entity class.

Coding the Application Client: HumanResourceClient.java

The application client, clientmdbentity-app-client/src/java/HumanResourceClient.java, performs the following steps:

  1. Injects ConnectionFactory and Topic resources

  2. Creates a TemporaryQueue to receive notification of processing that occurs, based on new-hire events it has published

  3. Creates a MessageConsumer for the TemporaryQueue, sets the MessageConsumer’s message listener, and starts the connection

  4. Creates a MessageProducer and a MapMessage

  5. Creates five new employees with randomly generated names, positions, and ID numbers (in sequence) and publishes five messages containing this information

The message listener, HRListener, waits for messages that contain the assigned office and equipment for each employee. When a message arrives, the message listener displays the information received and determines whether all five messages have arrived. When they have, the message listener notifies the main method, which then exits.

Coding the Message-Driven Beans for the clientmdbentity Example

This example uses two message-driven beans:

  • clientmdbentity-ejb/src/java/eb/EquipmentMDB.java

  • clientmdbentity-ejb/src/java/eb/OfficeMDB.java

The beans take the following steps:

  1. They inject MessageDrivenContext and ConnectionFactory resources.

  2. The onMessage method retrieves the information in the message. The EquipmentMDB’s onMessage method chooses equipment, based on the new hire’s position; the OfficeMDB’s onMessage method randomly generates an office number.

  3. After a slight delay to simulate real world processing hitches, the onMessage method calls a helper method, compose.

  4. The compose method takes the following steps:

    1. It either creates and persists the SetupOffice entity or finds it by primary key.

    2. It uses the entity to store the equipment or the office information in the database, calling either the doEquipmentList or the doOfficeNumber business method.

    3. If the business method returns true, meaning that all of the information has been stored, it creates a connection and a session, retrieves the reply destination information from the message, creates a MessageProducer, and sends a reply message that contains the information stored in the entity.

    4. It removes the entity.

Coding the Entity Class for the clientmdbentity Example

The SetupOffice class, clientmdbentity-ejb/src/java/eb/SetupOffice.java, is an entity class. The entity and the message-driven beans are packaged together in an EJB JAR file. The entity class is declared as follows:

@Entity
public class SetupOffice implements Serializable {

The class contains a no-argument constructor and a constructor that takes two arguments, the employee ID and name. It also contains getter and setter methods for the employee ID, name, office number, and equipment list. The getter method for the employee ID has the @Id annotation to indicate that this field is the primary key:

@Id
public String getEmployeeId() {
    return id;
}

The class also implements the two business methods, doEquipmentList and doOfficeNumber, and their helper method, checkIfSetupComplete.

The message-driven beans call the business methods and the getter methods.

The persistence.xml file for the entity specifies the most basic settings:

<?xml version="1.0" encoding="UTF-8"?>
<persistence version="2.0" 
             xmlns="http://java.sun.com/xml/ns/persistence" 
             xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 
             xsi:schemaLocation="http://java.sun.com/xml/ns/persistence 
               http://java.sun.com/xml/ns/persistence/persistence_2_0.xsd">
  <persistence-unit name="clientmdbentity-ejbPU" transaction-type="JTA">
    <provider>org.eclipse.persistence.jpa.PersistenceProvider</provider>
    <jta-data-source>jdbc/__default</jta-data-source>
    <class>eb.SetupOffice</class>
    <properties>
      <property name="eclipselink.ddl-generation" 
                value="drop-and-create-tables"/>
    </properties>
  </persistence-unit>
</persistence>

Creating Resources for the clientmdbentity Example

This example uses the connection factory jms/ConnectionFactory and the topic jms/Topic, both of which you used in An Application That Uses the JMS API with a Session Bean. It also uses the JDBC resource named jdbc/__default, which is enabled by default when you start the GlassFish Server.

If you deleted the connection factory or topic, they will be created when you deploy the example.

Running the clientmdbentity Example

You can use either NetBeans IDE or Ant to build, package, deploy, and run the clientmdbentity example.

To Run the clientmdbentity Example Using NetBeans IDE

  1. From the File menu, choose Open Project.
  2. In the Open Project dialog, navigate to:
    tut-install/examples/jms/
  3. Select the clientmdbentity folder.
  4. Select the Open as Main Project check box and the Open Required Projects check box.
  5. Click Open Project.
  6. In the Projects tab, right-click the clientmdbentity project and select Build.

    This task creates the following:

    • An application client JAR file that contains the client class and listener class files, along with a manifest file that specifies the main class

    • An EJB JAR file that contains the message-driven beans and the entity class, along with the persistence.xml file

    • An application EAR file that contains the two JAR files along with an application.xml file

  7. If the Java DB database is not already running, follow these steps:
    1. Click the Services tab.
    2. Expand the Databases node.
    3. Right-click the Java DB node and select Start Server.
  8. In the Projects tab, right-click the project and select Run.

    This command creates any needed resources, deploys the project, returns a client JAR file named clientmdbentityClient.jar, and then executes it.

    The output of the application client in the Output pane looks something like this:

    PUBLISHER: Setting hire ID to 50, name Bill Tudor, position Programmer
    PUBLISHER: Setting hire ID to 51, name Carol Jones, position Senior Programmer
    PUBLISHER: Setting hire ID to 52, name Mark Wilson, position Manager
    PUBLISHER: Setting hire ID to 53, name Polly Wren, position Senior Programmer
    PUBLISHER: Setting hire ID to 54, name Joe Lawrence, position Director
    Waiting for 5 message(s)
    New hire event processed:
      Employee ID: 52
      Name: Mark Wilson
      Equipment: PDA
      Office number: 294
    Waiting for 4 message(s)
    New hire event processed:
      Employee ID: 53
      Name: Polly Wren
      Equipment: Laptop
      Office number: 186
    Waiting for 3 message(s)
    New hire event processed:
      Employee ID: 54
      Name: Joe Lawrence
      Equipment: Java Phone
      Office number: 135
    Waiting for 2 message(s)
    New hire event processed:
      Employee ID: 50
      Name: Bill Tudor
      Equipment: Desktop System
      Office number: 200
    Waiting for 1 message(s)
    New hire event processed:
      Employee ID: 51
      Name: Carol Jones
      Equipment: Laptop
      Office number: 262

    The output from the message-driven beans and the entity class appears in the server log, wrapped in logging information.

    For each employee, the application first creates the entity and then finds it. You may see runtime errors in the server log, and transaction rollbacks may occur. The errors occur if both of the message-driven beans discover at the same time that the entity does not yet exist, so they both try to create it. The first attempt succeeds, but the second fails because the bean already exists. After the rollback, the second message-driven bean tries again and succeeds in finding the entity. Container-managed transactions allow the application to run correctly, in spite of these errors, with no special programming.

To Run the clientmdbentity Example Using Ant

  1. Go to the following directory:
    tut-install/examples/jms/clientmdbentity/
  2. To compile the source files and package the application, use the following command:
    ant

    The ant command creates the following:

    • An application client JAR file that contains the client class and listener class files, along with a manifest file that specifies the main class

    • An EJB JAR file that contains the message-driven beans and the entity class, along with the persistence.xml file

    • An application EAR file that contains the two JAR files along with an application.xml file

  3. To create any needed resources, deploy the application, and run the client, use the following command:
    ant run

    This command starts the database server if it is not already running, then deploys and runs the application.

    Ignore the message that states that the application is deployed at a URL.

    The output in the terminal window looks something like this (preceded by application client container output):

    running application client container.
    PUBLISHER: Setting hire ID to 50, name Bill Tudor, position Programmer
    PUBLISHER: Setting hire ID to 51, name Carol Jones, position Senior Programmer
    PUBLISHER: Setting hire ID to 52, name Mark Wilson, position Manager
    PUBLISHER: Setting hire ID to 53, name Polly Wren, position Senior Programmer
    PUBLISHER: Setting hire ID to 54, name Joe Lawrence, position Director
    Waiting for 5 message(s)
    New hire event processed:
      Employee ID: 52
      Name: Mark Wilson
      Equipment: PDA
      Office number: 294
    Waiting for 4 message(s)
    New hire event processed:
      Employee ID: 53
      Name: Polly Wren
      Equipment: Laptop
      Office number: 186
    Waiting for 3 message(s)
    New hire event processed:
      Employee ID: 54
      Name: Joe Lawrence
      Equipment: Java Phone
      Office number: 135
    Waiting for 2 message(s)
    New hire event processed:
      Employee ID: 50
      Name: Bill Tudor
      Equipment: Desktop System
      Office number: 200
    Waiting for 1 message(s)
    New hire event processed:
      Employee ID: 51
      Name: Carol Jones
      Equipment: Laptop
      Office number: 262

    The output from the message-driven beans and the entity class appears in the server log, wrapped in logging information.

    For each employee, the application first creates the entity and then finds it. You may see runtime errors in the server log, and transaction rollbacks may occur. The errors occur if both of the message-driven beans discover at the same time that the entity does not yet exist, so they both try to create it. The first attempt succeeds, but the second fails because the bean already exists. After the rollback, the second message-driven bean tries again and succeeds in finding the entity. Container-managed transactions allow the application to run correctly, in spite of these errors, with no special programming.