The Java EE 5 Tutorial

Running JMS Client Programs on Multiple Systems

JMS client programs using the Application Server can exchange messages with each other when they are running on different systems in a network. The systems must be visible to each other by name (the UNIX host name or the Microsoft Windows computer name) and must both be running the Application Server. You do not have to install the tutorial examples on both systems; you can use the examples installed on one system if you can access its file system from the other system.


Note –

Any mechanism for exchanging messages between systems is specific to the Java EE server implementation. This tutorial describes how to use the Application Server for this purpose.


Suppose that you want to run the Producer program on one system, earth, and the SynchConsumer program on another system, jupiter. Before you can do so, you need to perform these tasks:

    Before you begin, start the server on both systems:

  1. Start the Application Server on earth.

  2. Start the Application Server on jupiter.

Creating Administered Objects for Multiple Systems

To run these programs, you must do the following:

You do not have to install the tutorial on both systems, but you must be able to access the filesystem where it is installed. You may find it more convenient to install the tutorial on both systems if the two systems use different operating systems (for example, Windows and Solaris). Otherwise you will have to edit the file tut-install/javaeetutorial5/examples/bp-project/build.properties and change the location of the javaee.home property each time you build or run a program on a different system.

    To create a new connection factory on jupiter, perform these steps:

  1. From a command shell on jupiter, go to the directory tut-install/javaeetutorial5/examples/jms/simple/producer/.

  2. Type the following command:


    ant create-local-factory
    

The create-local-factory target, defined in the build.xml file for the Producer example, creates a connection factory named jms/JupiterConnectionFactory.

    To create a new connection factory on earth that points to the connection factory on jupiter, perform these steps:

  1. From a command shell on earth, go to the directory tut-install/javaeetutorial5/examples/jms/simple/producer/.

  2. Type the following command:


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

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

The create-remote-factory target, defined in the build.xml file for the Producer example, also creates a connection factory named jms/JupiterConnectionFactory. In addition, it sets the AddressList property for this factory to the name of the remote system.

If you have already been working on either earth or jupiter, you have the queue and topic on one system. On the system that does not have the queue and topic, type the following command:


ant create-resources

When you run the programs, they will work as shown in Figure 31–6. The program run on earth needs the queue on earth only in order that the resource injection will succeed. The connection, session, and message producer are all created on jupiter using the connection factory that points to jupiter. The messages sent from earth will be received on jupiter.

Figure 31–6 Sending Messages from One System to Another

Diagram showing a message being sent to a queue by a
producer on Earth and being received by a consumer on Jupiter

Editing, Recompiling, Repackaging, and Running the Programs

These steps assume that you have the tutorial installed on only one of the two systems you are using and that you are able to access the file system of jupiter from earth or vice versa.

    After you create the connection factories, edit the source files to specify the new connection factory. Then recompile, repackage, and run the programs. Perform the following steps:

  1. Open the following file in a text editor:

    tut-installjavaeetutorial5/examples/jms/simple/producer/src/java/Producer.java
    
  2. Find the following line:

    @Resource(mappedName="jms/ConnectionFactory")
  3. Change the line to the following:

    @Resource(mappedName="jms/JupiterConnectionFactory")
  4. Recompile and repackage the Producer example on earth.

    If you are using NetBeans IDE, right-click the producer project and choose Clean and Build.

    If you are using Ant, type the following:


    ant
    
  5. Open the following file in a text editor:

    tut-installjavaeetutorial5/examples/jms/simple/synchconsumer/src/java/SynchConsumer.java
    
  6. Repeat steps 2 and 3.

  7. Recompile and repackage the SynchConsumer example on jupiter.

    If you are using NetBeans IDE, right-click the synchconsumer project and choose Clean and Build.

    If you are using Ant, go to the synchconsumer directory and type:


    ant
    
  8. On earth, run Producer. If you are using NetBeans IDE on earth, perform these steps:

    1. Right-click the producer project and choose Properties.

    2. Select Run from the Categories tree.

    3. In the Arguments field, type the following:


      queue 3
      
    4. Click OK.

    5. Right-click the project and choose Run.

      If you are using the appclient command, go to the producer/dist directory and type the following:


      appclient -client producer.jar queue 3
      
  9. On jupiter, run SynchConsumer. If you are using NetBeans IDE on jupiter, perform these steps:

    1. Right-click the synchconsumer project and choose Properties.

    2. Select Run from the Categories tree.

    3. In the Arguments field, type the following:


      queue
      
    4. Click OK.

    5. Right-click the project and choose Run.

      If you are using the appclient command, go to the synchconsumer/dist directory and type the following:


      appclient -client synchconsumer.jar queue
      

For examples showing how to deploy Java EE applications on two different systems, see An Application Example That Consumes Messages from a Remote Server and An Application Example That Deploys a Message-Driven Bean on Two Servers.

Deleting the Connection Factory and Stopping the Server

You will need the connection factory jms/JupiterConnectionFactory in Chapter 32, Java EE Examples Using the JMS API. However, if you wish to delete it, go to the producer directory and type the following command:


ant delete-remote-factory

Remember to delete the connection factory on both systems.

You can also use Ant targets in the producer/build.xml file to delete the destinations and connection factories you created in Creating JMS Administered Objects for the Synchronous Receive Example. However, it is recommended that you keep them, because they will be used in most of the examples in Chapter 32, Java EE Examples Using the JMS API. After you have created them, they will be available whenever you restart the Application Server.

To delete the class and JAR files for each program using NetBeans IDE, right-click each project and choose Clean.

To delete the class and JAR files for each program using Ant, type the following:


ant clean

You can also stop the Application Server, but you will need it to run the sample programs in the next section.