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. The 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 factoriesone ordinary one and one that is configured to communicate with the remote serverto create two publishers and two subscribers and to publish and to 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).

The section covers the following topics:

  • Overview of the modules

  • Writing the module components

  • Creating resources for the modules

  • Using two application servers

  • Building, deploying, and running the modules using NetBeans 5.5

  • Building, deploying, and running the modules using Ant

You will find the source files for this section in <INSTALL>/javaeetutorial5/examples/jms/sendremote/. Path names in this section are relative to this directory.

Overview of the Modules

This pair of modules is somewhat similar to the modules in An Application Example That Consumes Messages from a Remote Server (page 1146) 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.

3.

The application client looks up the two connection factoriesthe local one and the one that communicates with the remote serverto 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 324 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 324. A Java EE Application That Sends Messages to Two Servers


Writing the Module Components

Writing the components of the modules involves two tasks:

  • Coding the application client: MultiAppServerClient.java

  • Coding the message-driven bean: ReplyMsgBean.java

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 Modules

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 Creating Administered Objects for Multiple Systems (page 1094). This connection factory must exist on the local server.

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

Using Two Application Servers

The Application Server must be running on both systems. You package, deploy, and run the modules from the local system.

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

1.

In NetBeans 5.5, click the Runtime tab.

2.

Right-click the Servers node and choose Add Server. In the Add Server Instance dialog, perform these steps:

a. Select Sun Java System Application Server (the default) from the Server list.

b. Accept the default name, which is typically Sun Java System Application Server (1).

c. Click Next.

d. For the Platform Folder location, you can either browse to the location of the Application Server on the remote system or, if that location is not visible from the local system, use the default location on the local system.

e. Select the Register Remote Domain radio button.

f. Click Next.

g. Type the system name of the host in the Host field.

h. Click Next.

i. Type the administrative username and password for the remote system in the Admin Username and Admin Password fields.

j. Click Finish.

There may be a delay while NetBeans 5.5 registers the remote domain.

Building, Deploying, and Running the Modules Using NetBeans 5.5

To package the modules using NetBeans 5.5, perform these steps:

1.

In NetBeans 5.5, choose Open Project from the File menu.

2.

In the Open Project dialog, navigate to <INSTALL>/javaeetutorial5/examples/jms/sendremote/.

3.

Select the replybean folder.

4.

Select the Open as Main Project checkbox.

5.

Click Open Project Folder.

6.

Right-click the replybean project and choose Build Project.

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

7.

Choose Open Project from the File menu.

8.

Select the multiclient folder.

9.

Select the Open as Main Project checkbox.

10.

Click Open Project Folder.

11.

Right-click the multiclient project and choose Build Project.

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

To deploy the multiclient module on the local server, perform these steps:

1.

Right-click the multiclient project and choose Properties.

2.

Select Run from the Categories tree.

3.

From the Server list, select Sun Java System Application Server (the local server).

4.

Click OK.

5.

Right-click the multiclient project and choose Deploy Project.

To deploy the replybean module on the local and remote servers, perform these steps:

1.

Right-click the replybean project and choose Properties.

2.

Select Run from the Categories tree.

3.

From the Server list, select Sun Java System Application Server (the local server).

4.

Click OK.

5.

Right-click the replybean project and choose Deploy Project.

6.

Right-click the replybean project again and choose Properties.

7.

Select Run from the Categories tree.

8.

From the Server list, select Sun Java System Application Server (1) (the remote server).

9.

Click OK.

10.

Right-click the replybean project and choose Deploy Project.

You can use the Runtime tab to verify that multiclient is deployed as an App Client Module on the local server and that replybean is deployed as an EJB Module on both servers.

To run the application client, right-click the multiclient project and choose Run Project.

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: Receivedmessage: text: id=2toremoteappserver    ReplyMsgBean: Receivedmessage: text: id=4toremoteappserver    ReplyMsgBean: Receivedmessage: text: id=6toremoteappserver    ReplyMsgBean: Receivedmessage: text: id=8toremoteappserver    ReplyMsgBean: Receivedmessage: text: id=10toremoteappserver


Undeploy the modules after you finish running the client. To undeploy the modules, perform these steps:

1.

Click the Runtime tab.

2.

Expand the Servers node.

3.

Expand the Sun Java System Application Server node (the local system).

4.

Expand the Applications node.

5.

Expand the EJB Modules node.

6.

Right-click replybean and choose Undeploy.

7.

Expand the App Client Modules node.

8.

Right-click multiclient and choose Undeploy.

9.

Expand the Sun Java System Application Server (1) node (the remote system).

10.

Expand the Applications node.

11.

Expand the EJB Modules node.

12.

Right-click replybean and choose Undeploy.

To remove the generated files, follow these steps:

1.

Right-click the replybean project and choose Clean Project.

2.

Right-click the multiclient project and choose Clean Project.

Building, Deploying, and Running the Modules Using Ant

To package the modules, perform these steps:

1.

Go to the following directory:

<INSTALL>/javaeetutorial5/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.

To deploy the replybean module on the local and remote servers, perform the following 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.

To deploy and run the client, perform these steps:

1.

Change to the directory multiclient:

cd ../multiclient


2.

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: Receivedmessage: text: id=2toremoteappserver    ReplyMsgBean: Receivedmessage: text: id=4toremoteappserver    ReplyMsgBean: Receivedmessage: text: id=6toremoteappserver    ReplyMsgBean: Receivedmessage: text: id=8toremoteappserver    ReplyMsgBean: Receivedmessage: text: id=10toremoteappserver


Undeploy the modules after you finish running the client. To undeploy the multiclient module, perform these steps:

1.

Verify that you are still in the directory multiclient.

2.

Type the following command:

ant undeploy


To undeploy the replybean module, perform these steps:

1.

Change to the directory replybean:

cd ../replybean


2.

Type the following command:

ant undeploy


3.

Type the following command:

ant undeploy-remote -Dsys=remote_system_name


Replace remote_system_name with the actual name of the remote system.

To remove the generated files, use the following command in both the replybean and multiclient directories:

   ant clean




The JavaT EE 5 Tutorial
The JavaT EE 5 Tutorial
ISBN: N/A
EAN: N/A
Year: 2004
Pages: 309

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net