This section and the following section explain how to write, compile, package, deploy, and run a pair of Java EE modules that run on two Java EE servers and that use the JMS API to interchange messages with each other. It is a common practice to deploy different components of an enterprise application on different systems within a company, and these examples illustrate on a small scale how to do this for an application that uses the JMS API. However, the two examples work in slightly different ways. In this first example, the deployment information for a message-driven bean specifies the remote server from which it will consume messages. In the next example, the same bean is deployed on two different servers, so it is the client module that specifies the servers (one local, one remote) to which it is sending messages. This first example divides the example in Chapter 23 into two modules (not applications): one containing the application client, and the other containing the message-driven bean. This section covers the following topics:
You will find the source files for this section in <INSTALL>/javaeetutorial5/examples/jms/consumeremote/. Path names in this section are relative to this directory. Overview of the ModulesExcept for the fact that it is packaged as two separate modules, this example is very similar to the one in Chapter 23:
The basic steps of the modules are as follows.
Figure 323 illustrates the structure of this application. You can see that it is almost identical to Figure 231 except that there are two Java EE servers. The queue used is the one on the remote server; the queue must also exist on the local server for resource injection to succeed. Figure 323. A Java EE Application That Consumes Messages from a Remote Server
Writing the Module ComponentsWriting the components of the modules involves
The application client, jupiterclient/src/java/SimpleClient.java, is almost identical to the one in The Application Client (page 792). Similarly, the message-driven bean, earthmdb/src/java/MessageBean.java, is almost identical to the one in The Message-Driven Bean Class (page 793). The only major difference is that the client and the bean are packaged in two separate modules. Creating Resources for the ApplicationFor this example, the message-driven bean uses the connection factory named jms/JupiterConnectionFactory, which you created in Creating Administered Objects for Multiple Systems (page 1094). Use the Admin Console to verify that the connection factory still exists and that its AddressList property is set to the name of the remote system. Because this bean must use a specific connection factory, the connection factory is specified in the mdb-connection-factory element of the sun-ejb-jar.xml file. If you deleted the connection factory, you can recreate it as follows:
The application client can use any connection factory that exists on the remote server; it uses jms/ConnectionFactory. Both components use the queue named jms/Queue, which you created in Creating JMS Administered Objects (page 1076). Using Two Application ServersWe'll assume, as we did in Running JMS Client Programs on Multiple Systems (page 1093), that the two servers are named earth and jupiter. The Application Server must be running on both systems. Which system you use to package and deploy the modules and which system you use to run the client depend on your network configurationwhich file system you can access remotely. These instructions assume that you can access the file system of jupiter from earth but cannot access the file system of earth from jupiter. (You can use the same systems for jupiter and earth that you used in Running JMS Client Programs on Multiple Systems, page 1093.) You can package both modules on earth and deploy the message-driven bean there. The only action you perform on jupiter is running the client module. Building, Deploying, and Running the Modules Using NetBeans 5.5To package the modules using NetBeans 5.5, perform these steps:
To deploy the earthmdb module and run the application client, perform these steps:
On jupiter, the output of the appclient command looks like this: Sending message: This is message 1 Sending message: This is message 2 Sending message: This is message 3 On earth, the output in the server log looks something like this (wrapped in logging information): MESSAGE BEAN: Message received: This is message 1 MESSAGE BEAN: Message received: This is message 2 MESSAGE BEAN: Message received: This is message 3 Undeploy the message-driven bean after you finish running the client. To undeploy the earthmdb module, perform these steps:
To remove the generated files, follow these steps:
You can also delete the jupiterclient.jar file from the remote filesystem. Building, Deploying, and Running the Modules Using AntTo package the modules using Ant, perform these steps:
To deploy the earthmdb module, perform these steps:
To copy the jupiterclient module to the remote system, perform these steps:
To run the client, perform the following steps:
On jupiter, the output of the appclient command looks like this: Sending message: This is message 1 Sending message: This is message 2 Sending message: This is message 3 On earth, the output in the server log looks something like this (wrapped in logging information): MESSAGE BEAN: Message received: This is message 1 MESSAGE BEAN: Message received: This is message 2 MESSAGE BEAN: Message received: This is message 3 Undeploy the message-driven bean after you finish running the client. To undeploy the earthmdb module, perform these steps:
You can also delete the jupiterclient.jar file from the remote filesystem. To remove the generated files, use the following command in both the earthmdb and jupiterclient directories: ant clean |