| JavaTM Message Service Tutorial |
| Tutorial Homepage | TOC | Prev | Next | Index |
7 A Simple J2EETM Application that Uses the JMS API
This chapter explains how to write, compile, package, deploy, and run a simple J2EETM application that uses the JMS API. The application in this chapter uses the following components:
- An application client that sends several messages to a queue
- A message-driven bean that asynchronously receives and processes the messages
The chapter covers the following topics:
- Writing and compiling the application components
- Creating and packaging the application
- Deploying and running the application
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/client_mdb(on UNIX® systems) orjms_tutorial\examples\client_mdb(on Microsoft Windows systems). The directoryear_filesin theexamplesdirectory contains a built application calledSampleMDBApp.ear. If you run into difficulty at any time, you can open this file in the deploytool and compare that file to your own version.7.1 Writing and Compiling the Application Components
The first and simplest application contains the following components:
- An application client that sends several messages to a queue
- A message-driven bean that asynchronously receives and processes the messages
Figure 7.1 illustrates the structure of this application.
Figure 7.1 A Simple J2EE Application: Client to Message-Driven Bean
The application client sends messages to the queue, which is created administratively, using the
j2eeadmincommand. The JMS provider--in this case, the J2EE server--delivers the messages to the message-driven bean instances, which then process the messages.Writing and compiling the components of this application involve
7.1.1 Coding the Application Client:
SimpleClient.javaThe application client class,
SimpleClient.java, is almost identical toSimpleQueueSender.java. The only significant differences are as follows.
- You do not specify the queue name on the command line. Instead, the client obtains the queue name through a JavaTM Naming and Directory InterfaceTM (JNDI) API lookup.
- You do not specify the number of messages on the command line; the number of messages is set at 3 for simplicity, and no end-of-messages message is sent.
- The JNDI API lookup uses the
java:/comp/env/jmsnaming context.7.1.2 Coding the Message-Driven Bean:
MessageBean.javaThe message-driven bean class,
MessageBean.java, implements the methodssetMessageDrivenContext,ejbCreate,onMessage, andejbRemove. TheonMessagemethod, almost identical to that ofTextListener.java, casts the incoming message to aTextMessageand displays the text. The only significant difference is that it calls theMessageDrivenContext.setRollbackOnlymethod in case of an exception. This method rolls back the transaction so that the message will be redelivered.7.1.3 Compiling the Source Files
To compile the files in the application, go to the
client_mdbdirectory and 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 *.java7.2 Creating and Packaging the Application
Creating and packaging this application involve several steps:
- Starting the J2EE server and the Application Deployment Tool
- Creating a queue
- Creating the J2EE application
- Packaging the application client
- Packaging the message-driven bean
- Checking the JNDI API names ("JNDI names")
7.2.1 Starting the J2EE Server and the Deploytool
Before you can create and package the application, you must start the J2EE server and the deploytool. Follow these steps.
- At the command line prompt, start the J2EE server:
j2ee -verboseWait until the server displays the message "J2EE server startup complete."
- At another command line prompt, start the deploytool:
deploytool7.2.2 Creating a Queue
In Section 4.2.4, "Creating the JMS Administered Objects," you used the
j2eeadmincommand to create a queue. This time, you will create it by using the deploytool, as follows.
- In the deploytool, select the Tools menu.
- From the Tools menu, choose Server Configuration.
- Under the JMS folder, select Destinations.
- In the JMS Queue Destinations area, click Add.
- In the text field, enter
jms/MyQueue. (We will observe the J2EE convention of placing the queue in thejmsnamespace.)- Click OK.
- Verify that the queue was created:
j2eeadmin -listJmsDestination7.2.3 Creating the J2EE Application
Create a new J2EE application called
MDBAppand store it in the file namedMDBApp.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
client_mdb.- In the File Name field, enter
MDBApp.- Click New Application.
- Click OK.
A diamond icon labeled
MDBAppappears in the tree view on the left side of the deploytool window. The full path name ofMDBApp.earappears in the General tabbed pane on the right side.7.2.4 Packaging the Application Client
In this section, you will run the New Application Client Wizard of the deploytool to package the application client. The New Application Client Wizard does the following:
- Identifies the application toward which the client is targeted
- Identifies the main class for the application client
- Identifies the queue and the connection factory referenced by the application client
To start the New Application Client Wizard, follow these steps.
- In the tree view, select
MDBApp.- From the File menu, choose New -> Application Client. The wizard displays a series of dialog boxes.
7.2.4.1 Introduction Dialog Box
7.2.4.2 JAR File Contents Dialog Box
- In the combo box labeled Create Archive Within Application, select
MDBApp.- Click the Edit button next to the Contents text area.
- In the dialog box Edit Contents of <Application Client>, choose the
client_mdbdirectory. 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
SimpleClient.classfrom the Available Files tree area and click Add.- Click OK.
- Click Next.
7.2.4.3 General Dialog Box
- Verify that the Main Class and the Display Name are both
SimpleClient.- In the Callback Handler Class combo box, verify that container-managed authentication is selected.
- Click Next.
7.2.4.4 Environment Entries Dialog Box
7.2.4.5 Enterprise Bean References Dialog Box
7.2.4.6 Resource References Dialog Box
In this dialog box, you associate the JNDI API context name for the connection factory in the
SimpleClient.javasource file with the name of theQueueConnectionFactory. You also specify container authentication for the connection factory resource, 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/MyQueueConnectionFactory--the logical name referenced bySimpleClient.- In the Type field, select
javax.jms.QueueConnectionFactory.- In the Authentication field, select Container.
- In the Sharable field, make sure that the checkbox is checked. This allows the container to optimize connections.
- In the JNDI Name field, enter
jms/QueueConnectionFactory.- In the User Name field, enter
j2ee.- In the Password field, enter
j2ee.- Click Next.
7.2.4.7 JMS Destination References Dialog Box
In this dialog box, you associate the JNDI API context name for the queue in the
SimpleClient.javasource file with the name of the queue you created usingj2eeadmin. Follow these steps.
- Click Add.
- In the Coded Name field, enter
jms/QueueName--the logical name referenced bySimpleClient.- In the Type field, select
javax.jms.Queue.- In the JNDI Name field, enter
jms/MyQueue.- Click Next.
7.2.4.8 Review Settings Dialog Box
7.2.5 Packaging the Message-Driven Bean
In this section, you will run the New Enterprise Bean Wizard of the deploytool to perform these tasks:
- Create the bean's deployment descriptor
- Package the deployment descriptor and the bean class in an enterprise bean JAR file
- Insert the enterprise bean JAR file into the application's
MDBApp.earfileTo start the New Enterprise Bean Wizard, follow these steps.
- In the tree view, select
MDBApp.- From the File menu, choose New -> Enterprise Bean. The wizard displays a series of dialog boxes.
7.2.5.1 Introduction Dialog Box
7.2.5.2 EJB JAR Dialog Box
- In the combo box labeled JAR File Location, verify that Create New JAR File in Application and
MDBAppare selected.- In the JAR Display Name field, verify that the name is
Ejb1, the default display name. Representing the enterprise bean JAR file that contains the bean, this name will be displayed in the tree view.- Click the Edit button next to the Contents text area.
- In the dialog box Edit Contents of Ejb1, choose the
client_mdbdirectory. 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
MessageBean.classfile from the Available Files tree area and click Add.- Click OK.
- Click Next.
7.2.5.3 General Dialog Box
- In the Bean Type combo box, select the Message-Driven radio button.
- Under Enterprise Bean Class, select
MessageBean. The combo boxes for the local and remote interfaces are grayed out.- In the Enterprise Bean Name field, enter
MessageBean. This name will represent the message-driven bean in the tree view. The display name does not have to be different from the bean class name.- Click Next.
7.2.5.4 Transaction Management Dialog Box
In this dialog box, you specify how transactions for the
onMessagemethod should be handled. Although an ordinary enterprise bean has six possible transaction attributes, a message-driven bean has only two. (The others are meaningful only if there might be a preexisting transaction context, which doesn't exist for a message-driven bean.) Follow these steps.
- Select the Container-Managed radio button.
- In the Transaction Attribute field opposite the
onMessagemethod, verify that Required is selected.- Click Next.
7.2.5.5 Message-Driven Bean Settings Dialog Box
In this dialog box, you specify the deployment properties for the bean. Because you are using container-managed transactions, the Acknowledgment field is grayed out. Follow these steps.
- In the Destination Type combo box, select Queue.
- In the Destination field, select
jms/MyQueue.- In the Connection Factory field, select
jms/QueueConnectionFactory.- Click Next.
7.2.5.6 Environment Entries Dialog Box
7.2.5.7 Enterprise Bean References Dialog Box
7.2.5.8 Resource References Dialog Box
Click Next. (You do not need to specify the connection factory for the message-driven bean, because it is not referred to in the message-driven bean code. You specified the connection factory in the Message-Driven Bean Settings dialog box.)
7.2.5.9 Resource Environment References Dialog Box
Click Next. (You do not need to specify the queue name here, because it is not referred to in the message-driven bean code. You specified it in the Message-Driven Bean Settings dialog box.)
7.2.5.10 Security Dialog Box
Use the default Security Identity setting for a message-driven bean, Run As Specified Role. Click Next.
7.2.5.11 Review Settings Dialog Box
7.2.6 Checking the JNDI Names
Verify that the JNDI names for the application components are correct.
- You give the JNDI name of the destination--in this case, the queue--to the message-driven bean component.
- You check to make sure that the context names for the connection factory and the destination are correctly matched to their JNDI names.
- In the tree view, select the
MDBAppapplication.- Select the JNDI Names tabbed pane.
- Verify that the JNDI names appear as shown in Table 7.1 and Table 7.2.
Table 7.1: Application Pane
Table 7.2: References Pane
7.3 Deploying and Running the Application
Deploying and running this application involve several steps:
- Looking at the deployment descriptor
- Adding the server, if necessary
- Deploying the application
- Running the client
- Undeploying the application
- Removing the application and stopping the server
7.3.1 Looking at the Deployment Descriptor
As you package an application, the deploytool creates a deployment descriptor in accordance with the packaging choices you make. To see some of the JMS API-related elements of the enterprise bean deployment descriptor, follow these steps.
- Select
Ejb1in the tree view.- Choose Descriptor Viewer from the Tools menu.
- Select
SimpleClientin the tree view and repeat step 2.In the Deployment Descriptor Viewer window, click Save As if you want to save the contents as an XML file for future reference. Table 7.3 describes the elements that are related to the JMS API.
Table 7.3: JMS API-Related Deployment Descriptor Elements
7.3.2 Adding the Server
Before you can deploy the application, you must make available to the deploytool the J2EE server you started in Section 7.2.1, "Starting the J2EE Server and the Deploytool." Because you started the 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. A
localhostnode appears underServersin the tree view.7.3.3 Deploying the Application
You have now created an application that consists of an application client and a message-driven bean. To deploy the application, perform the following steps.
- From the File menu, choose Save to save the application.
- From the Tools menu, choose Deploy.
- In the Introduction dialog box, verify that the Object to Deploy selection is
MDBAppand that the Target Server selection islocalhost.- 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 MDBApp is complete" message appears.
- In the tree view, expand
Serversand selectlocalhost. Verify thatMDBAppis deployed.7.3.4 Running the Client
To run the client, you use the
MDBApp.earfile that you created in Section 7.2.3, "Creating the J2EE Application." Make sure that you are in the directoryclient_mdb. Then perform the following steps.
- At the command line prompt, enter the following:
runclient -client MDBApp.ear -name SimpleClient- When the Login for user: dialog box appears, enter
j2eefor the user name andj2eefor the password.- Click OK.
The client program runs in the command window, generating output that looks like this:
Binding name:'java:comp/env/jms/QueueName' Binding name:'java:comp/env/jms/MyQueueConnectionFactory' Java(TM) Message Service 1.0.2 Reference Implementation (build b14) Sending message: This is message 1 Sending message: This is message 2 Sending message: This is message 3 Unbinding name:'java:comp/env/jms/QueueName' Unbinding name:'java:comp/env/jms/MyQueueConnectionFactory'Output from the application appears in the window in which you started the J2EE server. By default, the server creates three instances of the
MessageBeanto receive messages.In MessageBean.MessageBean() In MessageBean.setMessageDrivenContext() In MessageBean.ejbCreate() MESSAGE BEAN: Message received: This is message 1 In MessageBean.MessageBean() In MessageBean.setMessageDrivenContext() In MessageBean.ejbCreate() In MessageBean.MessageBean() In MessageBean.setMessageDrivenContext() In MessageBean.ejbCreate() MESSAGE BEAN: Message received: This is message 2 MESSAGE BEAN: Message received: This is message 37.3.5 Undeploying the Application
To undeploy the J2EE application, follow these steps.
- In the tree view, select
localhost.- Select
MDBAppin the Deployed Objects area.- Click Undeploy.
- Answer Yes in the confirmation dialog.
7.3.6 Removing the Application and Stopping the Server
To remove the application from the deploytool, follow these steps.
To delete the queue you created, enter the following at the command line prompt:
j2eeadmin -removeJmsDestination jms/MyQueueTo stop the J2EE server, use the following command:
j2ee -stopTo exit the deploytool, choose Exit from the File menu.
![]()
TOC | Prev | Next | Index
![]()
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.