| JavaTM Message Service Tutorial |
| Tutorial Homepage | TOC | Prev | Next | Index |
8 A J2EETM Application that Uses the JMS API with a Session Bean
This chapter explains how to write, compile, package, deploy, and run a J2EETM application that uses the JMS API in conjunction with a session bean. The application contains the following components:
- An application client that calls an enterprise bean
- A session bean that publishes several messages to a topic
- A message-driven bean that receives and processes the messages, using a durable topic subscriber and a message selector
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_ses_mdb(on UNIX® systems) orjms_tutorial\examples\client_ses_mdb(on Microsoft Windows systems). The directoryear_filesin theexamplesdirectory contains a built application calledSamplePubSubApp.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.8.1 Writing and Compiling the Application Components
This application demonstrates how to send messages from an enterprise bean--in this case, a session bean--rather than from an application client, as in the example in Chapter 7. Figure 8.1 illustrates the structure of this application.
Figure 8.1 A J2EE Application: Client to Session Bean to Message-Driven Bean
The Publisher enterprise bean in this example is the enterprise-application equivalent of a wire-service news feed that categorizes news events into six news categories. The message-driven bean could represent a newsroom, where the Sports desk, for example, would set up a subscription for all news events pertaining to sports news.
The application client in the example obtains a handle to the Publisher enterprise bean's home interface and calls the enterprise bean's business method. The enterprise bean creates 18 text messages. For each message, it sets a
Stringproperty randomly to one of six values representing the news categories and then publishes the message to a topic. The message-driven bean uses a message selector for the property to limit which of the published messages it receives.Writing and compiling the components of the application involve
- Coding the application client
- Coding the Publisher session bean
- Coding the message-driven bean
- Compiling the source files
8.1.1 Coding the Application Client:
MyAppClient.javaThe application client program,
MyAppClient.java, performs no JMS API operations and so is simpler than the client program in Chapter 7. The program obtains a handle to the Publisher enterprise bean's home interface, using the JavaTM Naming and Directory InterfaceTM (JNDI) API naming contextjava:comp/env. The program then creates an instance of the bean and calls the bean's business method twice.8.1.2 Coding the Publisher Session Bean
The Publisher bean is a stateless session bean with one
createmethod and one business method. The Publisher bean uses remote interfaces rather than local interfaces because it is accessed from outside the EJB container.8.1.2.1 The Remote Home Interface:
PublisherHome.javaThe remote home interface source file is
PublisherHome.java.8.1.2.2 The Remote Interface:
Publisher.javaThe remote interface,
Publisher.java, declares a single business method,publishNews.8.1.2.3 The Bean Class:
PublisherBean.javaThe bean class,
PublisherBean.java, implements thepublishNewsmethod and its helper methodchooseType. The bean class also implements the required methodsejbCreate,setSessionContext,ejbRemove,ejbActivate, andejbPassivate.The
ejbCreatemethod of the bean class allocates resources--in this case, by looking up theTopicConnectionFactoryand the topic and creating theTopicConnection. The business methodpublishNewscreates aTopicSessionand aTopicPublisherand publishes the messages.The
ejbRemovemethod must deallocate the resources that were allocated by theejbCreatemethod. In this case, theejbRemovemethod closes theTopicConnection.8.1.3 Coding the Message-Driven Bean:
MessageBean.javaThe message-driven bean class,
MessageBean.java, is identical to the one in Section 7.1.2.8.1.4 Compiling the Source Files
To compile all the files in the application, go to the directory
client_ses_mdband 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 *.java8.2 Creating and Packaging the Application
Creating and packaging this application involve several steps:
- Starting the J2EE server and the deploytool*
- Creating a topic
- Creating a connection factory
- Creating the J2EE application
- Packaging the application client
- Packaging the session bean
- Packaging the message-driven bean
- Specifying the JNDI API names ("JNDI names")
Step 1, marked with an asterisk (*), is not needed if the server and deploytool are still running.
8.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:
deploytool8.2.2 Creating a Topic
In Section 4.3.4, "Creating the JMS Administered Objects," you used the
j2eeadmincommand to create a topic. This time, you will create the topic by using the deploytool. Follow these steps.
- In the deploytool, select the Tools menu.
- From the Tools menu, choose Server Configuration.
- Under the JMS folder, select Destinations.
- In the JMS Topic Destinations area, click Add.
- In the text field, enter
jms/MyTopic. (We will observe the J2EE convention of placing the topic in thejmsnamespace.)- Click OK.
- If you wish, you can verify that the topic was created:
j2eeadmin -listJmsDestination8.2.3 Creating a Connection Factory
For this application, you create a new connection factory. This application will use a durable subscriber, so you need a connection factory that has a client ID. (For more information, see Section 5.2.1, "Creating Durable Subscriptions.") Follow these steps.
- At the command line prompt, enter the following command:
j2eeadmin -addJmsFactory jms/DurableTopicCF topic -props clientID=MyID- Verify that the connection factory was created:
j2eeadmin -listJmsFactoryYou can also create connection factories by using the deploytool's Server Configuration dialog.
8.2.4 Creating the J2EE Application
Create a new J2EE application called
PubSubAppand store it in the file namedPubSubApp.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_ses_mdb.- In the File Name field, enter
PubSubApp.- Click New Application.
- Click OK.
A diamond icon labeled
PubSubAppappears in the tree view on the left side of the deploytool window. The full path name ofPubSubApp.earappears in the General tabbed pane on the right side.8.2.5 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
PubSubApp.- From the File menu, choose New -> Application Client. The wizard displays a series of dialog boxes.
8.2.5.1 Introduction Dialog Box
8.2.5.2 JAR File Contents Dialog Box
- In the combo box labeled Create Archive Within Application, select
PubSubApp.- Click the Edit button next to the Contents text area.
- In the dialog box Edit Contents of <Application Client>, choose the
client_ses_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
MyAppClient.classfile from the Available Files tree area and click Add.- Click OK.
- Click Next.
8.2.5.3 General Dialog Box
- In the Application Client combo box, select
MyAppClientin the Main Class field, and enterMyAppClientin the Display Name field.- In the Callback Handler Class combo box, verify that container-managed authentication is selected.
- Click Next.
8.2.5.4 Environment Entries Dialog Box
8.2.5.5 Enterprise Bean References Dialog Box
In this dialog box, you associate the JNDI API context name for the EJB reference in the
MyAppClient.javasource file with the home and remote interfaces of the Publisher enterprise bean. Follow these steps.
- Click Add.
- In the Coded Name column, enter
ejb/MyEjbReference.- In the Type column, select Session.
- In the Interfaces column, select Remote.
- In the Home Interface column, enter
PublisherHome.- In the Local/Remote Interface column, enter
Publisher.- In the Deployment Settings combo box, select JNDI Name. In the JNDI Name field, enter
MyPublisher.- Click Finish. You do not need to enter anything in the other dialog boxes.
8.2.6 Packaging the Session Bean
In this section, you will run the New Enterprise Bean Wizard of the deploytool to package the session bean. Follow these steps.
- In the tree view, select
PubSubApp.- From the File menu, choose New -> Enterprise Bean. The wizard displays a series of dialog boxes.
8.2.6.1 Introduction Dialog Box
8.2.6.2 EJB JAR Dialog Box
- In the combo box labeled JAR File Location, verify that Create New JAR File in Application and
PubSubAppare 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_ses_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 files
Publisher.class,PublisherBean.class, andPublisherHome.classfrom the Available Files tree area and click Add.- Click OK.
- Click Next.
8.2.6.3 General Dialog Box
- In the Bean Type combo box, select the Session radio button.
- Select the Stateless radio button.
- In the Enterprise Bean Class combo box, select
PublisherBean.- In the Enterprise Bean Name field, enter
PublisherEJB.- In the Remote Interfaces combo box, select
PublisherHomefor Remote Home Interface andPublisherfor Remote Interface. Ignore the Local Interfaces combo box.- Click Next.
8.2.6.4 Transaction Management Dialog Box
- Select the Container-Managed radio button.
- In the Transaction Attribute field opposite the
publishNewsmethod, verify that Required is selected.- Click Next.
8.2.6.5 Environment Entries Dialog Box
8.2.6.6 Enterprise Bean References Dialog Box
8.2.6.7 Resource References Dialog Box
- Click Add.
- In the Coded Name field, enter
jms/MyTopicConnectionFactory.- In the Type field, select
javax.jms.TopicConnectionFactory.- In the Authentication field, select Container.
- In the JNDI Name field, enter
jms/DurableTopicCF.- In the User Name field, enter
j2ee.- In the Password field, enter
j2ee.- Click Next.
8.2.6.8 Resource Environment References Dialog Box
- Click Add.
- In the Coded Name field, enter
jms/TopicName--the logical name referenced by thePublisherBean.- In the Type field, select
javax.jms.Topic.- In the JNDI Name field, enter
jms/MyTopic.- Click Next.
8.2.6.9 Security Dialog Box
Use the default Security Identity setting for a session or an entity bean, Use Caller ID. Click Next.
8.2.6.10 Review Settings Dialog Box
8.2.7 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.
- In the tree view, select
PubSubApp.- From the File menu, choose New -> Enterprise Bean. The wizard displays a series of dialog boxes.
8.2.7.1 Introduction Dialog Box
8.2.7.2 EJB JAR Dialog Box
- In the combo box labeled JAR File Location, verify that Create New JAR File in Application and
PubSubAppare selected.- In the JAR Display Name field, verify that the name is
Ejb2, the default display name.- Click the Edit button next to the Contents text area.
- In the dialog box Edit Contents of Ejb2, choose the
client_ses_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.
8.2.7.3 General Dialog Box
- In the Bean Type combo box, select the Message-Driven radio button.
- In the Enterprise Bean Class combo box, select
MessageBean.- In the Enterprise Bean Name field, enter
MessageBean.- Click Next.
8.2.7.4 Transaction Management Dialog Box
- Select the Container-Managed radio button.
- In the Transaction Type field opposite the
onMessagemethod, verify that Required is selected.- Click Next.
8.2.7.5 Message-Driven Bean Settings Dialog Box
- In the Destination Type combo box, select Topic.
- Check the Durable Subscriber checkbox.
- In the Subscription Name field, enter
MySub.- In the Destination field, select
jms/MyTopic.- In the Connection Factory field, select
jms/DurableTopicCF.- In the JMS Message Selector field, enter the following exactly as shown:
NewsType = 'Opinion' OR NewsType = 'Sports'This will cause the message-driven bean to receive only messages whose
NewsTypeproperty is set to one of these values.- Click Finish. You do not need to enter anything in the other dialog boxes.
8.2.8 Specifying the JNDI Names
Verify that the JNDI names are correct, and add one for the
PublisherEJBcomponent. Follow these steps.
- In the tree view, select the
PubSubAppapplication.- Select the JNDI Names tabbed pane.
- Make sure that the JNDI names appear as shown in Table 8.1 and Table 8.2. You will need to enter
MyPublisheras the JNDI name for thePublisherEJBcomponent.
Table 8.1: Application Pane
Table 8.2: References Pane
8.3 Deploying and Running the Application
Deploying and running this application involve several steps:
- Adding the server, if necessary
- Deploying the application
- Running the client
- Undeploying the application
- Removing the application and stopping the server
8.3.1 Adding the Server
Before you can deploy the application, you must make available to the deploytool the J2EE server you started in Section 8.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.8.3.2 Deploying the Application
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
PubSubAppand 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 PubSubApp is complete" message appears.
- In the tree view, expand
Serversand selectlocalhost. Verify thatPubSubAppis deployed.8.3.3 Running the Client
To run the client, perform the following steps.
- At the command line prompt, enter the following:
runclient -client PubSubApp.ear -name MyAppClient -textauth- At the login prompts, enter
j2eeas the user name andj2eeas the password.- Click OK.
The client program runs in the command window and has output that looks like this:
Binding name:'java:comp/env/ejb/MyEjbReference' Looking up EJB reference Looked up home Narrowed home Got the EJB Unbinding name:'java:comp/env/ejb/MyEjbReference'Output from the application appears in the window in which you started the J2EE server. Suppose that the last few messages from the Publisher session bean look like this:
PUBLISHER: Setting message text to: Item 13: Opinion PUBLISHER: Setting message text to: Item 14: Sports PUBLISHER: Setting message text to: Item 15: Nation/World PUBLISHER: Setting message text to: Item 16: Living/Arts PUBLISHER: Setting message text to: Item 17: OpinionBecause of the message selector, the last few messages received by the message-driven bean will look like this:
MESSAGE BEAN: Message received: Item 13: Opinion MESSAGE BEAN: Message received: Item 14: Sports MESSAGE BEAN: Message received: Item 17: Opinion8.3.4 Undeploying the Application
To undeploy the J2EE application, follow these steps.
- In the tree view, select
localhost.- Select
PubSubAppin the Deployed Objects area.- Click Undeploy.
- Answer Yes in the confirmation dialog.
8.3.5 Removing the Application and Stopping the Server
To remove the application from the deploytool, follow these steps.
To delete the topic you created, enter the following at the command line prompt:
j2eeadmin -removeJmsDestination jms/MyTopicTo delete the connection factory you created, enter the following:
j2eeadmin -removeJmsFactory jms/DurableTopicCFTo stop the J2EE server, use the following command:
j2ee -stop
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.