SCBCD Notes: Message-Driven Bean
Published November 28th, 2006 in Java.Message-driven beans are specially designed to consume JMS messages. It is not advisable to “force” a session bean into this role. They are stateless, server-side, transaction-aware components for processing JMS messages. The EJB container manages transactions, security, resources, concurrency and message acknowledgment for MDBs.
All EJB 3.0 containers must support JMS provider and other providers through JCA (for Connector-based MDBs). In order to send a JMS message, we need a connection to the JMS provider and a destination address for the message. Using a connection factory javax.jms.ConnectionFactory similar to javax.sql.DataSource, you can create a connection to the JMS provider.

JMS provides two types of messaging models: push-and-subscribe and point-to-point. JMS messaging is asynchronous and therefore well suited to communicate with other applications. When the message is sent out, the transactions and security context of the sender are not propagated to the other applications as receivers. However, distributed transaction may still be possible depending on the JMS provider.
MDBs are identified using the @javax.ejb.MessageDriven or alternatively as described in an EJB deployment descriptor. MDBs do not have remote, local or endpoint interfaces. They also have javax.ejb.MessageDrivenContext similar to session bean’s javax.ejb.SessionContext which can be injected using @javax.annotation.Resource.
MDBs usually implement the javax.jms.MessageListener interface which defines the void onMessage() method. The method is responsible for any action upon receiving a message. MDBs can also integrate with other messaging systems that define a different interface contract.
EJB 3.0 allows you to describe the proprietary properties that different messaging providers may have using @MessageDriven.activationConfig array. The compulsory properties are:
acknowledgeMode- acknowledge the receipt of a message to the JMS provider. Two values can be specified:Auto-acknowledgeandDups-ok-acknowledge.Auto-acknowledgetells the EJB container to acknowledge immediately after the message is processed whileDups-ok-acknowledgeallows flexible time up to a duplicate message is sent out. TheacknowledgeModeis only respected in bean-managed transactions or container-managed transaction attributeNotSupported. For all other cases, acknowledgment takes place within the transaction context.messageSelector- specify to process messages only with particularjavax.jms.Messageproperties. A subset of the SQL-92 conditional expression (used in WHERE clause in SQL) can be used to construct complex criteria.destinationType- eitherjavax.jms.Topicorjavax.jms.QueuesubscriptionDurability- specify whether the message is stored at the provider and forwarded later to the container in the case of lost connection. Valid values includeDurable: store-and-forward andNonDurable. This is only valid for message destination of typejavax.jms.Topicand notjavax.jms.Queue.
The lifecycle of message-drive beans are similar to stateless session beans with two states - Does Not Exist and Method-Ready Pool. Refer to SCBCD Notes: Stateless Session Bean for a description of MDBs lifecycle.


0 Responses to “SCBCD Notes: Message-Driven Bean”
Please Wait
Leave a Reply