The Java EE 6 Tutorial

An Application Example That Consumes Messages from a Remote Server

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 message-driven 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 17, A Message-Driven Bean Example into two modules: one containing the application client, and the other containing the message-driven bean.

You will find the source files for this section in tut-install/examples/jms/consumeremote/. Path names in this section are relative to this directory.

Overview of the consumeremote Example Modules

Except for the fact that it is packaged as two separate modules, this example is very similar to the one in Chapter 17, A Message-Driven Bean Example:

    The basic steps of the modules are as follows.

  1. The administrator starts two Java EE servers, one on each system.

  2. On the local server, the administrator deploys the message-driven bean module, which specifies the remote server where the client is deployed.

  3. On the remote server, the administrator places the client JAR file.

  4. The client module sends three messages to a queue.

  5. The message-driven bean consumes the messages.

Figure 31–5 illustrates the structure of this application. You can see that it is almost identical to Figure 17–1 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 31–5 A Java EE Application That Consumes Messages from a Remote Server

Diagram of application showing a message-driven bean
that consumes messages from an application client on a remote server

Writing the Module Components for the consumeremote Example

Writing the components of the modules involves

The application client, jupiterclient/src/java/SimpleClient.java, is almost identical to the one in The simplemessage Application Client.

Similarly, the message-driven bean, earthmdb/src/java/MessageBean.java, is almost identical to the one in The Message-Driven Bean Class. The only significant difference is that the activation config properties include one property that specifies the name of the remote system. You need to edit the source file to specify the name of your system.

Creating Resources for the consumeremote Example

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 To Create JMS Administered Objects for the Synchronous Receive Example. The message-driven bean does not need a previously created connection factory; the resource adapter creates one for it.

Using Two Application Servers for the consumeremote Example

As in Running JMS Clients on Multiple Systems, the two servers are named earth and jupiter.

The GlassFish Server must be running on both systems.

Before you can run the example, you must change the default name of the JMS host on jupiter, as described in To Change the Default Host Name Using the Administration Console. If you have already performed this task, you do not have to repeat it.

Which system you use to package and deploy the modules and which system you use to run the client depend on your network configuration (which 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 Clients on Multiple Systems.)

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.

ProcedureTo Build, Package, Deploy, and Run the consumeremoteModules Using NetBeans IDE

To edit the message-driven bean source file and then package, deploy, and run the modules using NetBeans IDE, follow these steps.

  1. In NetBeans IDE, select File->Open Project.

  2. In the Open Project dialog, navigate to:


    tut-install/examples/jms/consumeremote/
    
  3. Select the earthmdb folder.

  4. Select the Open as Main Project check box.

  5. Click Open Project.

  6. Edit the MessageBean.java file as follows:

    1. In the Projects tab, expand the earthmdb, Source Packages, and mdb nodes, then double-click MessageBean.java.

    2. Find the following line within the @MessageBean annotation:

              @ActivationConfigProperty(propertyName = "addressList",
                  propertyValue = "remotesystem"),
    3. Replace remotesystem with the name of your remote system.

  7. Right-click the earthmdb project and select Build.

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

  8. Select File->Open Project.

  9. Select the jupiterclient folder.

  10. Select the Open as Main Project check box.

  11. Click Open Project.

  12. In the Projects tab, right-click the jupiterclient project and select Build.

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

  13. Right-click the earthmdb project and select Deploy.

  14. To copy the jupiterclient module to the remote system, follow these steps:

    1. Change to the directory jupiterclient/dist:


      cd ../jupiterclient/dist
      
    2. Type a command like the following:


      cp jupiterclient.jar F:/
      

      That is, copy the client JAR file to a location on the remote filesystem. You can use the file system user interface on your system instead of the command line.

  15. To run the application client, follow these steps:

    1. Go to the directory on the remote system (jupiter) where you copied the client JAR file.

    2. To deploy the client module and retrieve the client stubs, use the following command:


      asadmin deploy --retrieve . jupiterclient.jar
      

      This command deploys the client JAR file and retrieves the client stubs in a file named jupiterclientClient.jar

    3. To run the client, use the following command:


      appclient -client jupiterclientClient.jar
      

      On jupiter, the output of the appclient command looks like this (preceded by application client container output):


      Sending message: This is message 1 from jupiter
      Sending message: This is message 2 from jupiter
      Sending message: This is message 3 from jupiter

      On earth, the output in the server log looks something like this (preceded by logging information):


      MESSAGE BEAN: Message received: This is message 1 from jupiter
      MESSAGE BEAN: Message received: This is message 2 from jupiter
      MESSAGE BEAN: Message received: This is message 3 from jupiter

ProcedureTo Build, Package, Deploy, and Run the consumeremote Modules Using Ant

To edit the message-driven bean source file and then package, deploy, and run the modules using Ant, follow these steps.

  1. Open the file tut-install/examples/jms/consumeremote/earthmdb/src/java/mdb/MessageBean.java in an editor.

  2. Find the following line within the @MessageBean annotation:

            @ActivationConfigProperty(propertyName = "addressList",
                propertyValue = "remotesystem"),
  3. Replace remotesystem with the name of your remote system, then save and close the file.

  4. Go to the following directory:

    tut-install/examples/jms/consumeremote/earthmdb/
    
  5. Type the following command:


    ant
    

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

  6. Type the following command:


    ant deploy
    
  7. Go to the jupiterclient directory:


    cd ../jupiterclient
    
  8. Type the following command:


    ant
    

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

  9. To copy the jupiterclient module to the remote system, follow these steps:

    1. Change to the directory jupiterclient/dist:


      cd ../jupiterclient/dist
      
    2. Type a command like the following:


      cp jupiterclient.jar F:/
      

      That is, copy the client JAR file to a location on the remote filesystem.

  10. To run the application client, follow these steps:

    1. Go to the directory on the remote system (jupiter) where you copied the client JAR file.

    2. To deploy the client module and retrieve the client stubs, use the following command:


      asadmin deploy --retrieve . jupiterclient.jar
      

      This command deploys the client JAR file and retrieves the client stubs in a file named jupiterclientClient.jar

    3. To run the client, use the following command:


      appclient -client jupiterclientClient.jar
      

      On jupiter, the output of the appclient command looks like this (preceded by application client container output):


      Sending message: This is message 1 from jupiter
      Sending message: This is message 2 from jupiter
      Sending message: This is message 3 from jupiter

      On earth, the output in the server log looks something like this (preceded by logging information):


      MESSAGE BEAN: Message received: This is message 1 from jupiter
      MESSAGE BEAN: Message received: This is message 2 from jupiter
      MESSAGE BEAN: Message received: This is message 3 from jupiter