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:
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:
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 ModulesThis 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.
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 ComponentsWriting the components of the modules involves two tasks:
Coding the Application Client: MultiAppServerClient.javaThe application client class, multiclient/src/java/MultiAppServerClient.java, does the following.
Coding the Message-Driven Bean: ReplyMsgBean.javaThe message-driven bean class, replybean/src/ReplyMsgBean.java, does the following:
The onMessage method of the message-driven bean class does the following:
On both servers, the bean will consume messages from the topic jms/Topic. Creating Resources for the ModulesThis 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 ServersThe 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:
There may be a delay while NetBeans 5.5 registers the remote domain. Building, Deploying, and Running the Modules Using NetBeans 5.5To package the modules using NetBeans 5.5, perform these steps:
To deploy the multiclient module on the local server, perform these steps:
To deploy the replybean module on the local and remote servers, perform these steps:
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:
To remove the generated files, follow these steps:
Building, Deploying, and Running the Modules Using AntTo package the modules, perform these steps:
To deploy the replybean module on the local and remote servers, perform the following steps:
To deploy and run the client, perform these steps:
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:
To undeploy the replybean module, perform these steps:
To remove the generated files, use the following command in both the replybean and multiclient directories: ant clean |