background image

Coding the Message-Driven Bean

<< Coding the Publisher Session Bean | Running the Client >>
<< Coding the Publisher Session Bean | Running the Client >>

Coding the Message-Driven Bean

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, "A Message-Driven Bean Example."
However, the
@MessageDriven
annotation 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 properties messageSelector, subscriptionDurability, clientId,
and subscriptionName, 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 Resources for the clientsessionmdb
Example
This example uses the topic named jms/Topic and the connection factory
jms/ConnectionFactory
, which you created in
"Creating JMS Administered Objects for the
Synchronous Receive Example" on page 915
. If you deleted the connection factory or topic, you
can create them again using targets in the build.xml file for this example. Use the following
commands to create the resources:
ant create-cf
ant create-topic
Building, Deploying, and Running the
clientsessionmdb
Example Using NetBeans IDE
To build, deploy, and run the application using NetBeans IDE, do the following:
1. Start the Application Server, if it is not already running.
2. In NetBeans IDE, choose Open Project from the File menu.
A Java EE Application That Uses the JMS API with a Session Bean
Chapter 32 · Java EE Examples Using the JMS API
967