The Java EE 6 Tutorial

Running JMS Clients on Multiple Systems

JMS clients that use the GlassFish 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 GlassFish Server.


Note –

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


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

  1. Create two new connection factories

  2. Change the name of the default JMS host on one system

  3. Edit the source code for the two examples

  4. Recompile and repackage the examples


    Note –

    A limitation in the JMS provider in the GlassFish Server may cause a runtime failure to create a connection to systems that use the Dynamic Host Configuration Protocol (DHCP) to obtain an IP address. You can, however, create a connection from a system that uses DHCP to a system that does not use DHCP. In the examples in this tutorial, earth can be a system that uses DHCP, and jupiter can be a system that does not use DHCP.


When you run the clients, they will work as shown in Figure 31–1. The client 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–1 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

For examples showing how to deploy more complex 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.

ProcedureTo Create Administered Objects for Multiple Systems

To run these clients, you must do the following:

You do not have to install the tutorial examples 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 examples 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/examples/bp-project/build.properties and change the location of the javaee.home property each time you build or run a client on a different system.

  1. Start the GlassFish Server on earth.

  2. Start the GlassFish Server on jupiter.

  3. To create a new connection factory on jupiter, follow these steps:

    1. From a command shell on jupiter, go to the directory tut-install/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.

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

    1. From a command shell on earth, go to the directory tut-install/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.

  5. 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
    

Changing the Default Host Name

By default, the default host name for the JMS service on the GlassFish Server is localhost. To access the JMS service from another system, however, you must change the host name. You can change it to either the actual host name or to 0.0.0.0.

You can change the default host name using either the Administration Console or the asadmin command.

ProcedureTo Change the Default Host Name Using the Administration Console

  1. On jupiter, start the Administration Console by opening a browser at http://localhost:4848/.

  2. In the navigation tree, expand the Configuration node.

  3. Under the Configuration node, expand the Java Message Service node.

  4. Under the Java Message Service node, expand the JMS Hosts node.

  5. Under the JMS Hosts node, select default_JMS_host.

    The Edit JMS Host page opens.

  6. In the Host field, type the name of the system, or type 0.0.0.0.

  7. Click Save.

  8. Restart the GlassFish Server.

ProcedureTo Change the Default Host Name Using the asadmin Command

  1. Specify a command like one of the following:


    asadmin set server-config.jms-service.jms-host.default_JMS_host.host="0.0.0.0"
    

    asadmin set server-config.jms-service.jms-host.default_JMS_host.host="hostname"
    
  2. Restart the GlassFish Server.

ProcedureTo Edit, Build, Package, Deploy, and Run the Clients Using NetBeans IDE

These steps assume that you have the tutorial installed on both of the two systems you are using and that you are able to access the file system of jupiter from earth or vice versa. You will edit the source files to specify the new connection factory. Then you will rebuild and run the clients. Follow these steps.

  1. To edit the source files, follow these steps:

    1. On earth,, open the following file in NetBeans IDE:

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

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

      @Resource(lookup = "jms/JupiterConnectionFactory")
    4. On jupiter, open the following file inNetBeans IDE:

      tut-install/examples/jms/simple/synchconsumer/src/java/SynchConsumer.java
      
    5. Repeat Step b and Step c.

  2. To recompile and repackage the Producer example on earth, right-click the producer project and select Clean and Build.

  3. To recompile and repackage the SynchConsumer example on jupiter, right-click the synchconsumer project and select Clean and Build.

  4. On earth, deploy and run Producer. Follow these steps:

    1. Right-click the producer project and select 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 select Run.

  5. On jupiter, run SynchConsumer. Follow these steps:

    1. Right-click the synchconsumer project and select 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 select Run.

ProcedureTo Edit, Build, Package, Deploy, and Run the Clients Using Ant and the appclient Command

These steps assume that you have the tutorial installed on both of the two systems you are using and that you are able to access the file system of jupiter from earth or vice versa. You will edit the source files to specify the new connection factory. Then you will rebuild and run the clients.

  1. To edit the source files, follow these steps:

    1. On earth,, open the following file in a text editor:

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

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

      @Resource(lookup = "jms/JupiterConnectionFactory")
    4. On jupiter, open the following file in a text editor:

      tut-install/examples/jms/simple/synchconsumer/src/java/SynchConsumer.java
      
    5. Repeat Step b and Step c.

  2. To recompile and repackage the Producer example on earth, type the following:


    ant
    
  3. To recompile and repackage the SynchConsumer example on jupiter, go to the synchconsumer directory and type the following:


    ant
    
  4. On earth, deploy and run Producer. Follow these steps:

    1. On earth, from the producer directory, deploy the client JAR file to the GlassFish Server, then retrieve the client stubs:


      ant getclient
      

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

    2. To run the client, type the following:


      appclient -client client-jar/producerClient.jar queue 3
      
  5. On jupiter, run SynchConsumer. Follow these steps:

    1. From the synchconsumer directory, deploy the client JAR file to the GlassFish Server, then retrieve the client stubs:


      ant getclient
      

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

    2. To run the client, type the following:


      appclient -client client-jar/synchconsumerClient.jar queue