10 Annotations you can use on your MDBs

User Rating: 5 / 5

Star ActiveStar ActiveStar ActiveStar ActiveStar Active
 

Message Driven Beans can be configured through a wide list of Properties which has further increased in JMS 2.0. Most of them are activation configuration properties which are name-value pairs that are passed to the MDB container when an MDB is deployed. The properties can be declared in either an ejb-jar.xml deployment descriptor or by using the @ActivationConfigProperty annotation on the MDB bean class. Let's see in detail all the possible annotations you can apply.

subscriptionDurability

By default, messages published to a topic are delivered only to subscribers which are active. Durable Subscriptions have been introduced in JMS 2.0 to make it possible for a subscriber to receive JMS messages which are published while the subscriber application was away. This implies another subtle difference with Non Durable subscriptions: a Durable subscription can recover a server stop/crash provided that its messages are configured to be Persistent. On the other hand, Non durable subscriptions never recover from a server crash:

+----------------------------+        +-----------------------------+
|                            |        |                             |
|    Durable Subscriptions   |        |  Non Durable Subscription   |
+----------------------------+        +-----------------------------+
|                            |        |                             |
| Receives messages also     |        | Receives messages when the  |
| when the subscriber is     |        | subscriber is active        |
| not active                 |        |                             |
|                            |        | Never survive server restart|
| Can survive server restart |        |                             |
|                            |        |                             |
+----------------------------+        +-----------------------------+

Here are the key annotations of it:

@ActivationConfigProperty(propertyName = "subscriptionDurability", propertyValue = "Durable"),
@ActivationConfigProperty(propertyName = "subscriptionDurability", propertyValue = "NonDurable")

destinationType

This property can let you discriminate if you are consuming messages from a Queue or Topic. No further explanation needed.

@ActivationConfigProperty(propertyName = "destinationType", propertyValue = "javax.jms.Topic")
@ActivationConfigProperty(propertyName = "destinationType", propertyValue = "javax.jms.Queue")

connectionFactoryLookup

This property is essential if you want to use a non default Connection factory such as a Remotely defined Factory:

@ActivationConfigProperty(propertyName = "connectionFactoryLookup", propertyValue = "RemoteConnectionFactory")

shareSubscriptions

Since JMS 2.0 it is possible to use Shared Subscription, which means that messages from a Topic can be shared by multiple consumers. This can improve the scalability of your JMS system. If you are using MDBs as consumers, the effect is that the message will be delivered to only one of the shared consumers:

@ActivationConfigProperty(propertyName = "subscriptionName", propertyValue = "mytopic"),
@ActivationConfigProperty(propertyName = "shareSubscriptions", propertyValue="true"),

destinationLookup

This is the JNDI name of the queue/topic that will be used by the MDB. The Queue/Topic needs obviously to be defined in your server configuration.

@ActivationConfigProperty(propertyName = "destinationLookup", propertyValue = "topic/ExampleTopic"),

messageSelector

A message selector allows a JMS consumer filter messages that it receives from a particular topic or queue. A message selector uses message properties and headers as criteria in conditional expressions. These expressions use Boolean logic to declare which messages should be delivered to a client.

For example, if you want to get only those messages whit order id > 100:

@ActivationConfigProperty(propertyName = "messageSelector", propertyValue = "orderid > 100")

Sender example:

Message message = session.createMessage();
message.setObjectProperty("orderid", "15");
producer.send(message);

maxSession

This property determines the maximum number of JMS sessions that the JCA Resource Adapter can concurrently deliver messages to the MDB Pool. (See next to learn more about the MDB Pool).

+----------------------------------------------------+
|                                                    |
| +--------------+                                   |
| |              |                                   |
| |    JCA       |            +------------------+   |
| |              |            |                  |   |
| |    JMS       |            |                  |   |
| |  Sessions    +--------------->  MDB Pool     |   |
| |              |            |                  |   |
| |  maxSession  |            |                  |   |
| |              |            |                  |   |
| |              |            |                  |   |
| |              |            +------------------+   |
| |              |                                   |
| |              |                                   |
| +--------------+                                   |
|                                                    |
+----------------------------------------------------+
@ActivationConfigProperty( propertyName = "maxSession", propertyValue = "someNumber")

Pool size

The MDB pool size governs the amount of MDB instances that are available for processing the inflow Session created by the Resource Adapter.The org.jboss.ejb3.annotation.Pool can be used to reference your pool configuration:

@org.jboss.ejb3.annotation.Pool(value="mdb-strict-max-pool") 

acknowledgeMode

The acknowledgment protocol allows the JMS provider to monitor the progress of a message so that it knows whether the message was successfully produced and consumed. With AUTO_ACKNOWLEDGE mode the acknowledgment happens implicitly after the onMessage() handler returns. The use of CLIENT_ACKNOWLEDGE allows the application to control when the acknowledgment is sent.

@ActivationConfigProperty(propertyName = "acknowledgeMode", propertyValue = "Auto-acknowledge")

Mind it: If you're configured to use PERSISTENT messages, any exceptions in onMessage() will persist the message for re-delivery based on broker and destination settings. If you're using NON_PERSISTENT messages, any exception in onMessage() typically discards the message.

Resource Adapter

This annotation can let you use a Resource Adapter to connect to a non-default JMS Broker:

@ResourceAdapter("activemq-ra.rar")  

See the following tutorial for a complete example of it: Integrate ActiveMQ with WildFly


Advertisement

Related articles available on mastertheboss.com

How to configure a Queue in JBoss ?

This article has been moved here: JBoss JMS configuration

How to create a Queue with Jmx Console ?

  Bring up the JMX Console in your browser and look for the sect

JBoss JMS Queue example

The following article shows how to create a simple JMS Queue Prod

JBoss JMS Topic example

The following article shows how to create a simple JMS Topic Publ

JBoss HornetQ simple tutorial

HornetQ is an open source project to build a multi-protocol, embe

How do I configure a Queue/Topic to work in a cluster?

  JBoss AS 5 Just set the Clustered attribute to "true" in your