| JavaTM Message Service Tutorial |
| Tutorial Homepage | TOC | Prev | Next | Index |
10 An Application Example that Uses Two J2EETM Servers
This chapter explains how to write, compile, package, deploy, and run a pair of J2EETM applications that use the JMS API and run on two J2EE servers. A common practice is to deploy different components of an enterprise application on different systems within a company, and this example illustrates on a small scale how to do this for an application that uses the JMS API.
The applications use the following components:
- An application client that uses two connection factories--one ordinary one and one that is configured to communicate with the remote server--to create two publishers and two subscribers and to publish and to consume messages
- A message-driven bean that is deployed twice--once on the local server and once on the remote one--to process the messages and to send replies
In this chapter, the term local server means the server on which the application client is deployed. The term remote server means the server on which only the message-driven bean is deployed.
Another possible situation is that an application deployed on a J2EE server must be accessed from another system on which no J2EE server is running. The last section of this chapter discusses how to handle this situation.
The chapter covers the following topics:
- An overview of the applications
- Writing and compiling the application components
- Creating and packaging the applications
- Deploying and running the applications
- Accessing a J2EE application from a remote system that is not running a J2EE server
If you downloaded the tutorial examples as described in the preface, you will find the source code files for this chapter in
jms_tutorial/examples/multi_server(on UNIX® systems) orjms_tutorial\examples\multi_server(on Microsoft Windows systems). The directoryear_filesin theexamplesdirectory contains two built applications, calledSampleMultiApp.earandSampleReplyBeanApp.ear. If you run into difficulty at any time, you can open one of these files in the deploytool and compare that file to your own version.10.1 Overview of the Applications
This pair of applications is somewhat similar to the application in Chapter 7 in that the only components are a client and a message-driven bean. However, the applications here use these components in more complex ways. One application consists of the application client. The other application contains only the message-driven bean and is deployed twice, once on each server.
The basic steps of the applications are as follows.
- The administrator starts two J2EE servers.
- On the local server, the administrator creates a connection factory to communicate with the remote server.
- The application client uses two connection factories--a preconfigured one and the one just created--to create two connections, sessions, publishers, and subscribers. Each publisher publishes five messages.
- The local and the remote message-driven beans each receive five messages and send replies.
- The client's message listener consumes the replies.
Figure 10.1 illustrates the structure of this application.
Figure 10.1 A J2EE Two-Server Application
10.2 Writing and Compiling the Application Components
Writing and compiling the components of the applications involve
10.2.1 Coding the Application Client:
MultiAppServerRequester.javaThe application client class,
MultiAppServerRequester.java, does the following.
- It uses the JavaTM Naming and Directory InterfaceTM (JNDI) API naming context
java:comp/envto look up two connection factories and a topic.- For each connection factory, it creates a connection, a publisher session, a publisher, a subscriber session, a subscriber, and a temporary topic for replies.
- Each subscriber sets its message listener,
ReplyListener, and starts the connection.- Each publisher publishes five messages and creates a list of the messages the listener should expect.
- When each reply arrives, the message listener displays its contents and removes it from the list of expected messages.
- When all the messages have arrived, the client exits.
10.2.2 Coding the Message-Driven Bean:
ReplyMsgBean.javaThe
onMessagemethod of the message-driven bean class,ReplyMsgBean.java, does the following:
- Casts the incoming message to a
TextMessageand displays the text- Creates a connection, session, and publisher for the reply message
- Publishes the message to the reply topic
- Closes the connection
10.2.3 Compiling the Source Files
To compile the files in the application, go to the directory
multi_serverand do the following.
- Make sure that you have set the environment variables shown in Table 4.1:
JAVA_HOME,J2EE_HOME,CLASSPATH, andPATH.- At a command line prompt, compile the source files:
javac MultiAppServerRequester.java javac ReplyMsgBean.java10.3 Creating and Packaging the Application
Creating and packaging this application involve several steps:
- Starting the J2EE servers and the deploytool
- Creating a connection factory
- Creating the first J2EE application
- Packaging the application client
- Creating the second J2EE application
- Packaging the message-driven bean
- Checking the JNDI API names ("JNDI names")
10.3.1 Starting the J2EE Servers and the Deploytool
Before you can create and package the application, you must start the local and remote J2EE servers and the deploytool. Follow these steps.
- At a command line prompt on the local system, start the J2EE server:
j2ee -verboseWait until the server displays the message "J2EE server startup complete."
- At another command line prompt on the local system, start the deploytool:
deploytool- At a command line prompt on the remote system, start the J2EE server:
j2ee -verbose10.3.2 Creating a Connection Factory
For this example, you create on the local system a connection factory that allows the client to communicate with the remote server. If you downloaded the tutorial examples as described in the preface, you will find in the
multi_serverdirectory in themulti_serverdirectory a Microsoft Windows script calledsetup.batand a UNIX script calledsetup.sh. You can use one of these scripts to create the connection factory on the local system. The command insetup.batlooks like this:call j2eeadmin -addJmsFactory jms/RemoteTCF topic -props url=corbaname:iiop:%1:1050#%1The UNIX command in
setup.shlooks like this:#!/bin/sh -x j2eeadmin -addJmsFactory jms/RemoteTCF topic -props url=corbaname:iiop:$1:1050#$1
- To run the script, specify the name of the remote server as an argument. Use the host name that is visible to you on your network; do not use an IP address. For example, if the remote system is named
mars, enter the following:setup.bat marssetup.sh mars- Verify that the connection factory was created:
j2eeadmin -listJmsFactoryOne line of the output looks like this:
< JMS Cnx Factory : jms/RemoteTCF , Topic , [ url=corbaname:iiop:mars:1050#mars ] >10.3.3 Creating the First J2EE Application
Create a new J2EE application called
MultiAppand store it in the file namedMultiApp.ear. Follow these steps.
- In the deploytool, select the File menu.
- From the File menu, choose New -> Application.
- Click Browse next to the Application File Name field, and use the file chooser to locate the directory
multi_server.- In the File Name field, enter
MultiApp.- Click New Application.
- Click OK.
A diamond icon labeled
MultiAppappears in the tree view on the left side of the deploytool window. The full path name ofMultiApp.earappears in the General tabbed pane on the right side.10.3.4 Packaging the Application Client
In this section, you will run the New Application Client Wizard of the deploytool to package the application client. To start the New Application Client Wizard, follow these steps.
- In the tree view, select
MultiApp.- From the File menu, choose New -> Application Client. The wizard displays a series of dialog boxes.
10.3.4.1 Introduction Dialog Box
10.3.4.2 JAR File Contents Dialog Box
- In the combo box labeled Create Archive Within Application, select
MultiApp.- Click the Edit button next to the Contents text area.
- In the dialog box Edit Contents of <Application Client>, choose the
multi_serverdirectory. If the directory is not already in the Starting Directory field, type it in the field, or locate it by browsing through the Available Files tree.- Select
MultiAppServerRequester.classandMultiAppServerRequester$ReplyListener.classfrom the Available Files tree area and click Add.- Click OK.
- Click Next.
10.3.4.3 General Dialog Box
- In the Application Client combo box, select
MultiAppServerRequesterin the Main Class field, and enterMultiAppServerRequesterin the Display Name field.- In the Callback Handler Class combo box, verify that container-managed authentication is selected.
- Click Next.
10.3.4.4 Environment Entries Dialog Box
10.3.4.5 Enterprise Bean References Dialog Box
10.3.4.6 Resource References Dialog Box
In this dialog box, you associate the JNDI API context names for the connection factories in the
MultiAppServerRequester.javasource file with the names of the local and remote connection factories. You also specify container authentication for the connection factory resources, defining the user name and the password that the user must enter in order to be able to create a connection. Follow these steps.
- Click Add.
- In the Coded Name field, enter
jms/TopicConnectionFactory1--the first logical name referenced byMultiAppServerRequester.- In the Type field, select
javax.jms.TopicConnectionFactory.- In the Authentication field, select Container.
- In the Sharable field, make sure that the checkbox is selected. This allows the container to optimize connections.
- In the JNDI Name field, enter
jms/TopicConnectionFactory.- In the User Name field, enter
j2ee.- In the Password field, enter
j2ee.- Click Add.
- In the Coded Name field, enter
jms/TopicConnectionFactory2--the other logical name referenced byMultiAppServerRequester.- In the Type field, select
javax.jms.TopicConnectionFactory.- In the Authentication field, select Container.
- In the Sharable field, make sure that the checkbox is selected.
- In the JNDI Name field, enter
jms/RemoteTCF.- In the User Name field, enter
j2ee. (If the user name and the password appear to be filled in already, make sure that you follow the instructions at the end of Section 10.3.4.8, "Review Settings Dialog Box," after you exit the Wizard.)- In the Password field, enter
j2ee.- Click Next.
10.3.4.7 JMS Destination References Dialog Box
In this dialog box, you associate the JNDI API context name for the topic in the
MultiAppServerRequester.javasource file with the name of the default topic. The client code also uses a reply topic, but it is a temporary topic created programmatically rather than administratively and does not have to be specified in the deployment descriptor. Follow these steps.
- Click Add.
- In the Coded Name field, enter
jms/PTopic--the logical name for the publisher topic referenced byMultiAppServerRequester.- In the Type field, select
javax.jms.Topic.- In the JNDI Name field, enter
jms/Topic--the preconfigured topic.- Click Next.
10.3.4.8 Review Settings Dialog Box
After you exit the Wizard, do the following.
- Select the
MultiAppServerRequesternode in the tree.- Select the Resource Refs tabbed pane.
- Select the second entry in the table,
jms/TopicConnectionFactory2.- If the User Name and the Password fields are blank, enter
j2eein each field.- Choose Save from the File menu to save the application.
10.3.5 Creating the Second J2EE Application
Create a new J2EE application, called
ReplyBeanApp, and store it in the file namedReplyBeanApp.ear. Follow these steps.
- In the deploytool, select the File menu.
- From the File menu, choose New -> Application.
- Click Browse next to the Application File Name field, and use the file chooser to locate the directory
multi_server.- In the File Name field, enter
ReplyBeanApp.- Click New Application.
- Click OK.
A diamond icon labeled
ReplyBeanAppappears in the tree view on the left side of the deploytool window. The full path name ofReplyBeanApp.earappears in the General tabbed pane on the right side.10.3.6 Packaging the Message-Driven Bean
In this section, you will run the New Enterprise Bean Wizard of the deploytool to package the message-driven bean. To start the New Enterprise Bean Wizard, follow these steps.
10.3.6.1 Introduction Dialog Box
10.3.6.2 EJB JAR Dialog Box
- In the combo box labeled JAR File Location, verify that Create New JAR File in Application and
ReplyBeanAppare selected.- In the JAR Display Name field, verify that the name is
Ejb1, the default display name.- Click the Edit button next to the Contents text area.
- In the dialog box Edit Contents of Ejb1, choose the
multi_serverdirectory. If the directory is not already in the Starting Directory field, type it in the field, or locate it by browsing through the Available Files tree.- Select the
ReplyMsgBean.classfile from the Available Files tree area and click Add.- Click OK.
- Click Next.
10.3.6.3 General Dialog Box
- In the Bean Type combo box, select the Message-Driven radio button.
- Under Enterprise Bean Class, select
ReplyMsgBean. The combo boxes for the local and remote interfaces are grayed out.- In the Enterprise Bean Name field, enter
ReplyMDB. This name will represent the message-driven bean in the tree view.- Click Next.
10.3.6.4 Transaction Management Dialog Box
- Select the Container-Managed radio button.
- In the Transaction Attribute field opposite the
onMessagemethod, verify that Required is selected.- Click Next.
10.3.6.5 Message-Driven Bean Settings Dialog Box
- In the Destination Type combo box, select Topic.
- In the Destination field, select
jms/Topic.- In the Connection Factory field, select
jms/TopicConnectionFactory.- Click Next.
10.3.6.6 Environment Entries Dialog Box
10.3.6.7 Enterprise Bean References Dialog Box
10.3.6.8 Resource References Dialog Box
- Click Add.
- In the Coded Name field, enter
jms/TopicConnectionFactory.- In the Type field, select
javax.jms.TopicConnectionFactory.- In the Authentication field, select Container.
- In the JNDI Name field, enter
jms/TopicConnectionFactory.- In the User Name field, enter
j2ee.- In the Password field, enter
j2ee.- Click Finish. You do not need to enter anything in the other dialog boxes.
10.3.7 Checking the JNDI Names
Verify that the JNDI names for the application components are correct. To do so, do the following.
- In the tree view, select the
MultiAppapplication.- Select the JNDI Names tabbed pane.
- Verify that the JNDI names appear as shown in Table 10.1.
Table 10.1: References Pane
- In the tree view, select the
ReplyBeanAppapplication.- Select the JNDI Names tabbed pane.
- Verify that the JNDI names appear as shown in Table 10.2 and Table 10.3.
Table 10.2: Application Pane
Table 10.3: References Pane
10.4 Deploying and Running the Applications
Deploying and running this application involve several steps:
- Adding the server
- Deploying the applications
- Running the client
- Undeploying the applications
- Removing the applications and stopping the server
10.4.1 Adding the Server
Before you can deploy the application, you must make available to the deploytool both the J2EE servers you started in Section 10.3.1, "Starting the J2EE Servers and the Deploytool." To add the remote server, follow these steps.
- From the File menu, choose Add Server.
- In the Add Server dialog box, enter the name of the remote system in the Server Name field. Use the same name you specified when you ran the
setupscript in Section 10.3.2, "Creating a Connection Factory."- Click OK.
A node with the name of the remote system appears under
Serversin the tree view.Because you started the local J2EE server before you started the deploytool, the server, named
localhost, probably appears in the tree underServers. If it does not, do the following.
- From the File menu, choose Add Server.
- In the Add Server dialog box, enter
localhostin the Server Name field.- Click OK.
The
localhostnode appears underServersin the tree view.10.4.2 Deploying the Applications
To deploy the
MultiAppapplication, perform the following steps.
- In the tree view, select the
MultiAppapplication.- From the Tools menu, choose Deploy.
- In the Introduction dialog box, verify that the Object to Deploy selection is
MultiApp, and selectlocalhostas the Target Server.- Click Next.
- In the JNDI Names dialog box, verify that the JNDI names are correct.
- Click Next.
- Click Finish.
- In the Deployment Progress dialog box, click OK when the "Deployment of MultiApp is complete" message appears.
- In the tree view, expand
Serversand select the host name. Verify thatMultiAppis deployed.To deploy the
ReplyBeanAppapplication on the local server, perform the following steps.
- In the tree view, select the
ReplyBeanAppapplication.- From the Tools menu, choose Deploy.
- In the Introduction dialog box, verify that the Object to Deploy selection is
ReplyBeanApp, and select the local server as the Target Server.- Click Next.
- In the JNDI Names dialog box, verify that the JNDI names are correct.
- Click Next.
- Click Finish.
- In the Deployment Progress dialog box, click OK when the "Deployment of ReplyBeanApp is complete" message appears.
- In the tree view, expand
Serversand select the host name. Verify thatReplyBeanAppis deployed.- Repeat steps 19 for the remote server, selecting the remote server as the Target Server in step 3.
10.4.3 Running the Client
To run the client, perform the following steps.
- At a command line prompt on the local system, enter the following:
runclient -client MultiApp.ear -name MultiAppServerRequester -textauth- At the login prompts, enter
j2eeas the user name andj2eeas the password.- Click OK.
The client program runs in the command window. Output from the message-driven beans appears on both the local and the remote systems, in the windows in which you started each J2EE server.
10.4.4 Undeploying the Applications
To undeploy the J2EE applications, follow these steps.
- In the tree view, select
localhostunder Servers.- Select
MultiAppin the Deployed Objects area.- Click Undeploy.
- Answer Yes in the confirmation dialog.
- Repeat steps 14 for
ReplyBeanAppon both the local and the remote servers.10.4.5 Removing the Applications and Stopping the Servers
To remove the applications from the deploytool, follow these steps.
- Select
MultiAppin the tree view.- Select Close from the File menu.
- Repeat these steps for
ReplyBeanApp.To delete the connection factory you created, enter the following at a command line prompt on the local system:
j2eeadmin -removeJmsFactory jms/RemoteTCFTo stop the J2EE servers, use the following command on each system:
j2ee -stopTo exit the deploytool, choose Exit from the File menu.
10.5 Accessing a J2EE Application from a Remote System that Is Not Running a J2EE Server
To run an application installed on a J2EE server from a system that is not running a J2EE server, you perform tasks similar to those described in Section 4.4.2, "Communicating Between a J2EE Server and a System Not Running a J2EE Server." Again, the J2EE SDK must be installed on both systems. You may also want to use the
runclientcommand to run an application client installed on a remote system.This section describes both of these situations:
- Accessing a J2EE application from a standalone client
- Using
runclientto access a remote application client10.5.1 Accessing a J2EE Application from a Standalone Client
You can run a standalone client that uses messages to communicate with a J2EE application. For example, you can use the deploytool to deploy the
ReplyBeanAppapplication on a system running the J2EE server, then use a standalone client to publish messages to the topic that theReplyMsgBeanis listening on and receive replies on a temporary topic.For example, suppose that the
ReplyBeanAppapplication is deployed on the server running on the systemearth, and suppose that the standalone client is namedPubSuband will run on the systemmars. Section 10.5.1.1 shows the client program.To specify the remote system on the command line, you use a command line just like the one in Section 4.4.2 (you do so after setting your environment variables as shown in Table 4.1).
- On a Microsoft Windows system, type the following command:
java -Djms.properties=%J2EE_HOME%\config\jms_client.properties -Dorg.omg.CORBA.ORBInitialHost=earth PubSub jms/Topic- On a UNIX system, type the following command:
java -Djms.properties=$J2EE_HOME/config/jms_client.properties -Dorg.omg.CORBA.ORBInitialHost=earth PubSub jms/TopicIf all the remote applications you need to access are deployed on the same server, you can edit the file
%J2EE_HOME%\config\orb.properties(on Microsoft Windows systems) or$J2EE_HOME/config/orb.properties(on UNIX systems) on the local system. The second line of this file looks like this:host=localhostChange
localhostto the name of the system on which the remote applications are deployed (for example,earth):host=earthYou can now run the client program as before, but you do not need to specify the option
-Dorg.omg.CORBA.ORBInitialHost.10.5.1.1 The Sample Client Program:
PubSub.javaThe sample client program
PubSub.javacan publish messages to a topic that theReplyMsgBeanis listening on and receive the message bean's replies.10.5.2 Using
runclientto Access a Remote Application ClientIf you need to run a J2EE application that contains an application client and that is deployed on a remote system, you can use the
runclientcommand to do so. For example, if you deploy bothReplyBeanAppandMultiAppon the server running onearth, the steps are as follows.
- Make sure that the
multi_serverdirectory onearthis accessible to you via the file system so that therunclientcommand can find it.- Follow the instructions in Section 10.3.2, "Creating a Connection Factory," and create on
marsa connection factory that will refer to the corresponding connection factory onearth.- Set the
hostproperty in theorb.propertiesfile in the J2EE SDK onmars, as described in Section 10.5.1, because therunclientcommand does not allow you to specify theORBInitialHostvalue:host=earth- Go to the
multi_serverdirectory onearth--or specify the complete path to theMultiApp.earfile--and issue therunclientcommand:runclient -client MultiApp.ear -name MultiAppServerRequester -textauth
This Tutorial contains information on the 1.3.1 version of the Java 2 Platform, Enterprise Edition.
Copyright © 2002 Sun Microsystems, Inc. All rights reserved.