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 That Uses the JMS API with an Entity

Overview of the clientmdbentity Example Application

Writing the Application Components for the clientmdbentity Example

Coding the Application Client: HumanResourceClient.java

Coding the Message-Driven Beans for the clientmdbentity Example

Coding the Entity Class for the clientmdbentity Example

Creating Resources for the clientmdbentity Example

Running the clientmdbentity Example

To Run the clientmdbentity Example Using NetBeans IDE

To Run the clientmdbentity 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

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 Example That Deploys a Message-Driven Bean on Two Servers

This section, like the preceding one, explains how to write, compile, package, deploy, and run a pair of Java EE modules that use the JMS API and run on two Java EE servers. These modules are slightly more complex than the ones in the first example.

The modules use the following components:

  • An application client that is deployed on the local server. It uses two connection factories, an ordinary one and one configured to communicate with the remote server, to create two publishers and two subscribers and to publish and consume messages.

  • A message-driven bean that is deployed twice: once on the local server, and once on the remote one. It processes the messages and sends replies.

In this section, the term local server means the server on which both the application client and the message-driven bean are deployed (earth in the preceding example). The term remote server means the server on which only the message-driven bean is deployed (jupiter in the preceding example).

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

Overview of the sendremote Example Modules

This pair of modules is somewhat similar to the modules in An Application Example That Consumes Messages from a Remote Server in that the only components are a client and a message-driven bean. However, the modules here use these components in more complex ways. One module consists of the application client. The other module contains only the message-driven bean and is deployed twice, once on each server.

The basic steps of the modules are as follows.

  1. You start two Java EE servers, one on each system.

  2. On the local server (earth), you create two connection factories: one local and one that communicates with the remote server (jupiter). On the remote server, you create a connection factory that has the same name as the one that communicates with the remote server.

  3. The application client looks up the two connection factories (the local one and the one that communicates with the remote server) to create two connections, sessions, publishers, and subscribers. The subscribers use a message listener.

  4. Each publisher publishes five messages.

  5. Each of the local and the remote message-driven beans receives five messages and sends replies.

  6. The client’s message listener consumes the replies.

Figure 48-6 illustrates the structure of this application. M1 represents the first message sent using the local connection factory, and RM1 represents the first reply message sent by the local MDB. M2 represents the first message sent using the remote connection factory, and RM2 represents the first reply message sent by the remote MDB.

Figure 48-6 A Java EE Application That Sends Messages to Two Servers

Diagram of application showing an application client sending messages to two servers and receiving the replies

Writing the Module Components for the sendremote Example

Writing the components of the modules involves coding the application client and the message-driven bean.

Coding the Application Client: MultiAppServerClient.java

The application client class, multiclient/src/java/MultiAppServerClient.java, does the following.

  1. It injects resources for two connection factories and a topic.

  2. For each connection factory, it creates a connection, a publisher session, a publisher, a subscriber session, a subscriber, and a temporary topic for replies.

  3. Each subscriber sets its message listener, ReplyListener, and starts the connection.

  4. Each publisher publishes five messages and creates a list of the messages the listener should expect.

  5. When each reply arrives, the message listener displays its contents and removes it from the list of expected messages.

  6. When all the messages have arrived, the client exits.

Coding the Message-Driven Bean: ReplyMsgBean.java

The message-driven bean class, replybean/src/ReplyMsgBean.java, does the following:

  1. Uses the @MessageDriven annotation:

    @MessageDriven(mappedName = "jms/Topic")
  2. Injects resources for the MessageDrivenContext and for a connection factory. It does not need a destination resource because it uses the value of the incoming message’s JMSReplyTo header as the destination.

  3. Uses a @PostConstruct callback method to create the connection, and a @PreDestroy callback method to close the connection.

The onMessage method of the message-driven bean class does the following:

  1. Casts the incoming message to a TextMessage and displays the text

  2. Creates a connection, a session, and a publisher for the reply message

  3. Publishes the message to the reply topic

  4. Closes the connection

On both servers, the bean will consume messages from the topic jms/Topic.

Creating Resources for the sendremote Example

This example uses the connection factory named jms/ConnectionFactory and the topic named jms/Topic. These objects must exist on both the local and the remote servers.

This example uses an additional connection factory, jms/JupiterConnectionFactory, which communicates with the remote system; you created it in To Create Administered Objects for Multiple Systems. This connection factory must exist on the local server.

The build.xml file for the multiclient module contains targets you can use to create these resources if you deleted them previously.

To create the resource needed only on the local system, use the following command:

ant create-remote-factory -Dsys=remote-system-name

The other resources will be created when you deploy the application.

To Enable Deployment on the Remote System

GlassFish Server by default does not allow deployment on a remote system. You must create a password for the administrator on the remote system, then enable secure administration on that system. After that, you will be able to deploy the message-driven bean on the remote system.

  1. On jupiter, start the Administration Console by opening a browser at http://localhost:4848/.
  2. In the navigation tree, expand the Configurations node, then expand the server-config node.
  3. Expand the Security node.
  4. Expand the Realms node.
  5. Select the admin-realm node.
  6. On the Edit Realm page, click Manage Users.
  7. In the File Users table, click admin in the User ID column.
  8. On the Edit File Realm Users page, type a password (for example, jmsadmin) in the New Password and Confirm New Password fields, then click Save.
  9. In the navigation tree, click the Server (Admin Server) node.
  10. On the General Information page, click Secure Administration.
  11. Click Enable Secure Admin, accepting the default values for the alias and instance.
  12. The server on jupiter will stop and restart automatically. Log in to the Administration Console with the admin user ID and the password you created and verify that the settings are correct.

To Use Two Application Servers for the sendremote Example

If you are using NetBeans IDE, you need to add the remote server in order to deploy the message-driven bean there. To do so, follow these steps.

  1. In NetBeans IDE, click the Services tab.
  2. Right-click the Servers node and select Add Server. In the Add Server Instance dialog, follow these steps:
    1. Select GlassFish Server 3+ from the Server list.
    2. In the Name field, specify a name slightly different from that of the local server, such as GlassFish Server 3+ (2).
    3. Click Next.
    4. For the Server Location, browse to the location of the GlassFish Server on the remote system. This location must be visible from the local system.
    5. Click Next.
    6. Select the Register Remote Domain radio button.
    7. In the Host Name field, type the name of the remote system.
    8. Click Finish.
    9. In the dialog that appears, enter the user name (admin) and the password you created.

Next Steps

Before you can run the example, you must change the default name of the JMS host on jupiter, as described in To Change the Default Host Name Using the Administration Console. If you have already performed this task, you do not have to repeat it.

Running the sendremote Example

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

To Run the sendremote Example Using NetBeans IDE

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

      This command creates a JAR file that contains the bean class file.

  2. To build the multiclient module, follow these steps:
    1. From the File menu, choose Open Project.
    2. Select the multiclient folder.
    3. Select the Open as Main Project check box.
    4. Click Open Project.
    5. In the Projects tab, right-click the multiclient project and select Build.

      This command creates a JAR file that contains the client class file and a manifest file.

  3. To create any needed resources and deploy the multiclient module on the local server, follow these steps:
    1. Right-click the multiclient project and select Properties.
    2. Select Run from the Categories tree.
    3. From the Server list, select GlassFish Server 3+ (the local server).
    4. Click OK.
    5. Right-click the multiclient project and select Deploy.

      You can use the Services tab to verify that multiclient is deployed as an App Client Module on the local server.

  4. To deploy the replybean module on the local and remote servers, follow these steps:
    1. Right-click the replybean project and select Properties.
    2. Select Run from the Categories tree.
    3. From the Server list, select GlassFish Server 3+ (the local server).
    4. Click OK.
    5. Right-click the replybean project and select Deploy.
    6. Right-click the replybean project again and select Properties.
    7. Select Run from the Categories tree.
    8. From the Server list, select GlassFish Server 3+ (2) (the remote server).
    9. Click OK.
    10. Right-click the replybean project and select Deploy.

      You can use the Services tab to verify that replybean is deployed as an EJB Module on both servers.

  5. To run the application client, right-click the multiclient project and select Run.

    This command returns a JAR file named multiclientClient.jar and then executes it.

    On the local system, the output of the appclient command looks something like this:

    running application client container.
    ...
    Sent message: text: id=1 to local app server
    Sent message: text: id=2 to remote app server
    ReplyListener: Received message: id=1, text=ReplyMsgBean processed message: 
    text: id=1 to local app server
    Sent message: text: id=3 to local app server
    ReplyListener: Received message: id=3, text=ReplyMsgBean processed message: 
    text: id=3 to local app server
    ReplyListener: Received message: id=2, text=ReplyMsgBean processed message: 
    text: id=2 to remote app server
    Sent message: text: id=4 to remote app server
    ReplyListener: Received message: id=4, text=ReplyMsgBean processed message: 
    text: id=4 to remote app server
    Sent message: text: id=5 to local app server
    ReplyListener: Received message: id=5, text=ReplyMsgBean processed message: 
    text: id=5 to local app server
    Sent message: text: id=6 to remote app server
    ReplyListener: Received message: id=6, text=ReplyMsgBean processed message: 
    text: id=6 to remote app server
    Sent message: text: id=7 to local app server
    ReplyListener: Received message: id=7, text=ReplyMsgBean processed message: 
    text: id=7 to local app server
    Sent message: text: id=8 to remote app server
    ReplyListener: Received message: id=8, text=ReplyMsgBean processed message: 
    text: id=8 to remote app server
    Sent message: text: id=9 to local app server
    ReplyListener: Received message: id=9, text=ReplyMsgBean processed message: 
    text: id=9 to local app server
    Sent message: text: id=10 to remote app server
    ReplyListener: Received message: id=10, text=ReplyMsgBean processed message: 
    text: id=10 to remote app server
    Waiting for 0 message(s) from local app server
    Waiting for 0 message(s) from remote app server
    Finished
    Closing connection 1
    Closing connection 2

    On the local system, where the message-driven bean receives the odd-numbered messages, the output in the server log looks like this (wrapped in logging information):

    ReplyMsgBean: Received message: text: id=1 to local app server
    ReplyMsgBean: Received message: text: id=3 to local app server
    ReplyMsgBean: Received message: text: id=5 to local app server
    ReplyMsgBean: Received message: text: id=7 to local app server
    ReplyMsgBean: Received message: text: id=9 to local app server

    On the remote system, where the bean receives the even-numbered messages, the output in the server log looks like this (wrapped in logging information):

    ReplyMsgBean: Received message: text: id=2 to remote app server
    ReplyMsgBean: Received message: text: id=4 to remote app server
    ReplyMsgBean: Received message: text: id=6 to remote app server
    ReplyMsgBean: Received message: text: id=8 to remote app server
    ReplyMsgBean: Received message: text: id=10 to remote app server

To Run the sendremote Example Using Ant

  1. To package the modules, follow these steps:
    1. Go to the following directory:
      tut-install/examples/jms/sendremote/multiclient/
    2. Type the following command:
      ant

      This command creates a JAR file that contains the client class file and a manifest file.

    3. Change to the directory replybean:
      cd ../replybean
    4. Type the following command:
      ant

      This command creates a JAR file that contains the bean class file.

  2. To deploy the replybean module on the local and remote servers, follow these steps:
    1. Verify that you are still in the directory replybean.
    2. Type the following command:
      ant deploy

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

    3. Type the following command:
      ant deploy-remote -Dsys=remote-system-name

      Replace remote-system-name with the actual name of the remote system.

  3. To deploy the client, follow these steps:
    1. Change to the directory multiclient:
      cd ../multiclient
    2. Type the following command:
      ant getclient
  4. To run the client, type the following command:
    ant run

    On the local system, the output looks something like this:

    running application client container.
    ...
    Sent message: text: id=1 to local app server
    Sent message: text: id=2 to remote app server
    ReplyListener: Received message: id=1, text=ReplyMsgBean processed message: 
    text: id=1 to local app server
    Sent message: text: id=3 to local app server
    ReplyListener: Received message: id=3, text=ReplyMsgBean processed message: 
    text: id=3 to local app server
    ReplyListener: Received message: id=2, text=ReplyMsgBean processed message: 
    text: id=2 to remote app server
    Sent message: text: id=4 to remote app server
    ReplyListener: Received message: id=4, text=ReplyMsgBean processed message: 
    text: id=4 to remote app server
    Sent message: text: id=5 to local app server
    ReplyListener: Received message: id=5, text=ReplyMsgBean processed message: 
    text: id=5 to local app server
    Sent message: text: id=6 to remote app server
    ReplyListener: Received message: id=6, text=ReplyMsgBean processed message: 
    text: id=6 to remote app server
    Sent message: text: id=7 to local app server
    ReplyListener: Received message: id=7, text=ReplyMsgBean processed message: 
    text: id=7 to local app server
    Sent message: text: id=8 to remote app server
    ReplyListener: Received message: id=8, text=ReplyMsgBean processed message: 
    text: id=8 to remote app server
    Sent message: text: id=9 to local app server
    ReplyListener: Received message: id=9, text=ReplyMsgBean processed message: 
    text: id=9 to local app server
    Sent message: text: id=10 to remote app server
    ReplyListener: Received message: id=10, text=ReplyMsgBean processed message: 
    text: id=10 to remote app server
    Waiting for 0 message(s) from local app server
    Waiting for 0 message(s) from remote app server
    Finished
    Closing connection 1
    Closing connection 2

    On the local system, where the message-driven bean receives the odd-numbered messages, the output in the server log looks like this (wrapped in logging information):

    ReplyMsgBean: Received message: text: id=1 to local app server
    ReplyMsgBean: Received message: text: id=3 to local app server
    ReplyMsgBean: Received message: text: id=5 to local app server
    ReplyMsgBean: Received message: text: id=7 to local app server
    ReplyMsgBean: Received message: text: id=9 to local app server

    On the remote system, where the bean receives the even-numbered messages, the output in the server log looks like this (wrapped in logging information):

    ReplyMsgBean: Received message: text: id=2 to remote app server
    ReplyMsgBean: Received message: text: id=4 to remote app server
    ReplyMsgBean: Received message: text: id=6 to remote app server
    ReplyMsgBean: Received message: text: id=8 to remote app server
    ReplyMsgBean: Received message: text: id=10 to remote app server

To Disable Deployment on the Remote System

After running this example and undeploying the components, you should disable secure administration on the remote system (jupiter). In addition, you will probably want to return the GlassFish Server on jupiter to its previous state of not requiring a user name and password for administration, to make it easier to run subsequent examples there.

  1. On the remote system (jupiter), start the Administration Console by opening a browser at http://localhost:4848/, if it is not already running.

    You will need to log in.

  2. In the navigation tree, click the Server (Admin Server) node.
  3. On the General Information page, click Secure Administration.
  4. Click Disable Secure Admin.
  5. The server will stop and restart automatically. Log in to the Administration Console again.
  6. In the navigation tree, expand the Configurations node, then expand the server-config node.
  7. Expand the Security node.
  8. Expand the Realms node.
  9. Select the admin-realm node.
  10. On the Edit Realm page, click Manage Users.
  11. In the File Users table, click admin in the User ID column.
  12. On the Edit File Realm Users page, click Save.
  13. In the dialog that asks you to confirm that you are setting an empty password for the specified user, click OK.

    The next time you start the Administration Console or issue an asadmin command, you will not need to provide login credentials.

Next Steps

On earth, if you used NetBeans IDE to add the remote server, you may also want to remove the server.