A Java EE Application That Uses the JMS API with a Session Bean
This section explains how to write, compile, package, deploy, and run a Java EE application that uses the JMS API in conjunction with a session bean. The application contains the following components:
The section covers the following topics:
You will find the source files for this section in the directory
<INSTALL>/javaeetutorial5/examples/jms/clientsessionmdb/. Path names in this section are relative to this directory.Writing 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 23. Figure 33-1 illustrates the structure of this application.
![]()
Figure 33-1 A Java EE 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.
The application client in the example injects the Publisher enterprise bean's remote home interface and then calls the 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 the components of the application involves the following:
Coding the Application Client: MyAppClient.java
The application client program,
clientsessionmdb-app-client/src/java/MyAppClient.java, performs no JMS API operations and so is simpler than the client program in Chapter 23. The program uses dependency injection to obtain the Publisher enterprise bean's business interface:The program then calls the bean's business method twice.
Coding the Publisher Session Bean
The Publisher bean is a stateless session bean that has one business method. The Publisher bean uses a remote interface rather than a local interface because it is accessed from the application client.
The remote interface,
clientsessionmdb-ejb/src/java/sb/PublisherRemote.java, declares a single business method,publishNews.The bean class,
clientsessionmdb-ejb/src/java/sb/PublisherBean.java, implements thepublishNewsmethod and its helper methodchooseType. The bean class also injectsSessionContext,ConnectionFactory, andTopicresources and implements@PostConstructand@PreDestroycallback methods. The bean class begins as follows:@Stateless @Remote({PublisherRemote.class}) public class PublisherBean implements PublisherRemote { @Resource private SessionContext sc; @Resource(mappedName="jms/ConnectionFactory") private ConnectionFactory connectionFactory; @Resource(mappedName="jms/Topic") private Topic topic; ...The
@PostConstructcallback method of the bean class,makeConnection, creates theConnectionused by the bean. The business methodpublishNewscreates aSessionand aMessageProducerand publishes the messages.The
@PreDestroycallback method,endConnection, deallocates the resources that were allocated by the@PostConstructcallback method. In this case, the method closes theConnection.Coding the Message-Driven Bean: MessageBean.java
The message-driven bean class,
clientsessionmdb-ejb/src/java/mdb/MessageBean.java, is almost identical to the one in Chapter 23. However, the@MessageDrivenannotation is different, because instead of a queue the bean is using a topic with a durable subscription, and it is also using a message selector. Therefore, the annotation sets the activation config propertiesmessageSelector,subscriptionDurability,clientId, andsubscriptionName, as follows:@MessageDriven(mappedName="jms/Topic", activationConfig= { @ActivationConfigProperty(propertyName="messageSelector", propertyValue="NewsType = 'Sports' OR NewsType = 'Opinion'"), @ActivationConfigProperty( propertyName="subscriptionDurability", propertyValue="Durable"), @ActivationConfigProperty(propertyName="clientId", propertyValue="MyID"), @ActivationConfigProperty(propertyName="subscriptionName", propertyValue="MySub") })The JMS resource adapter uses these properties to create a connection factory for the message-driven bean that allows the bean to use a durable subscriber.
Creating and Packaging the Application
This example uses the topic named
jms/Topicand the connection factoryjms/ConnectionFactory,which you created in Creating JMS Administered Objects (page 1052). To package the application, do the following:The
antcommand creates the following:
- An application client JAR file that contains the client class file and the session bean's remote interface, along with a manifest file that specifies the main class
- An EJB JAR file that contains both the session bean and the message-driven bean
- An application EAR file that contains the two JAR files along with an
application.xmlfileThe
clientsessionmdb.earfile is created in theclientsessionmdb/distdirectory.If you deleted the connection factory or topic, you can create them again using targets in the
build.xmlfile for this example. Use the following commands to create the resources:Deploying the Application
To deploy the application, use the following command:
Ignore the message that states that the application is deployed at a URL.
To return a client JAR file, use the following command:
This command returns a JAR file named
clientsessionmdbClient.jarin theclient-jardirectory.Running the Application Client
After you deploy the application, you run the client as follows:
The output from the enterprise beans appears in the server log (
<JAVAEE_HOME>/domains/domain1/logs/server.log), wrapped in logging information. The Publisher session bean sends two sets of 18 messages numbered 0 through 17. Because of the message selector, the message-driven bean receives only the messages whoseNewsTypeproperty isSportsorOpinion.To undeploy the application after you finish running the client, use the following command:
To remove the generated files, use the following command: