5.13. Message Driven Beans

A message-driven bean is an enterprise bean that allows J2EE applications to process messages asynchronously. It acts as a JMS message listener, which is similar to an event listener except that it receives messages instead of events. The messages may be sent by any J2EE component--an application client, another enterprise bean, or a Web component--or by a JMS application or system that does not use J2EE technology. This definition is from http://java.sun.com/j2ee/tutorial/1_3-fcs/doc/EJBConcepts5.html , and you can read more about message driven beans (MDB) from there.
You can specify MDBs in a deployment descriptor or using annotations.
Using a descriptor
<enterprise-beans>
  <message-driven>
    <ejb-name>MDBExample</ejb-name>
    <destination-jndi-name>queue/@QUEUE_NAME@</destination-jndi-name>
  </message-driven>
</enterprise-beans>
Using an annotation
@MessageDriven(mappedName="jms/Queue")
public class SimpleMessageBean implements MessageListener {
    @Resource
    private MessageDrivenContext mdc;
    ...
You configure MDBs using properties divided into those specified by the JCA specification, and those available as JBoss extensions.

Important

MDB properties listed in Table 5.1, “MDB Properties Provided by the JCA Specification” are not mandatory, unless explicitly called out in the Remarks column.

Table 5.1. MDB Properties Provided by the JCA Specification

Name Type Default value Remarks
destination java.lang.String none
This property is Mandatory
The JNDI name of the Queue or Topic.
destinationType java.lang.String none
The type of destination valid values are javax.jms.Queue or javax.jms.Topic
messageSelector java.lang.String none
The message selector of the subscription
acknowledgeMode int AUTO_ ACKNOWLEDGE
The type of acknowledgement when not using transacted jms - valid values AUTO_ ACKNOWLEDGE or DUPS_OK_ ACKNOWLEDGE
clientID java.lang.String
The client id of the connection
subscriptionDurability String NonDurable
Whether topic subscriptions are durable. Valid values are Durable or NonDurable
subscriptionName String none
The subscription name of the topic subscription

Important

MDB properties listed in Table 5.2, “MDB Properties Provided as JBoss Extensions” are not mandatory, unless explicitly called out in the Remarks column.

Table 5.2. MDB Properties Provided as JBoss Extensions

Name Type Default value Remarks
isTopic boolean false
Sets the destinationType
providerAdapterJNDI java.lang.String DefaultJMSProvider The JNDI name of the JMS provider.
user java.lang.String none The user ID used to connect to the JMS server
pass java.lang.String none The password of the user
maxMessages int 1
Read this number of messages before delivering messages to the MDB.
Each message is delivered individually on the same thread in an attempt to avoid context excessive context switching
minSession int 1 The minimum number of JMS sessions that are available to concurrently deliver messages to this mdb
maxSession int 15 The maximum number of JMS sessions that are available to concurrently deliver messages to this mdb
reconnectInterval long 10 seconds The length of time in seconds between attempts to (re-)connect to the JMS provider
keepAlive long 60 seconds The length of time in milliseconds that sessions over the minimum are kept alive
sessionTransacted boolean true Whether the sessions are transacted
useDLQ boolean true Whether to use a Dead Letter Queue (DLQ) handler.
dLQJNDIName java.lang.String queue/DLQ The JNDI name of the DLQ
dLQHandler java.lang.String
org.jboss.resource. adapter.jms.inflow.dlq. GenericDLQHandler
The org.jboss.resource. adapter.jms.inflow. DLQHandler implementation class name.
dLQUser java.lang.String none The user id used to make the dlq connection to the JMS server
dLQPassword java.lang.String none The password of the dLQUser
dLQClientID java.lang.String none The client id of the DLQ connection
dLQMaxResent int 5 The maximum number of times a message is redelivered before it is sent to the DLQ.
redeliverUnspecified boolean true Whether to attempt to redeliver a message in an unspecified transaction context
transactionTimeout int Default is the timeout set for the resource manager Time in seconds for the transaction timeout
DeliveryActive boolean true Whether the MDB should make the subscription at initial deployment or wait for start() or stopDelivery() on the corresponding MBean. You can set this to false if you want to prevent messages from being delivered to the MDB (which is still starting) during server start up.
Configuring default MDB properties

You can configure MDBs to have default properties using the @org.jboss.ejb3.annotation.DefaultActivationSpecs annotations.