WEBINAR:
On-Demand
Application Security Testing: An Integral Part of DevOps
MDBs: EJB and JMS Interfaces in One
Message-Driven Beans do not follow the typical pattern for EJB interface implementation, because an MDB is a cross between a stateless session EJB and a JMS client. Rather than defining EJB component interfaces, MDBs implement two interfaces: one is an EJB interface and the other is a JMS interface. The following code is a sample of an MDB class signature:
public class MessageBean implements MessageDrivenBean,
MessageListener
The first interface, javax.ejb.MessageDrivenBean, is the bean's interface to the container. It allows the EJB container to manage the MDB, just as the container manages session and entity beans. The second interface, javax.jms.MessageListener, allows the bean to register with a JMS-enabled MOM (messaging-oriented middleware) server as a JMS listener.
As full-fledged JMS clients, MDBs can both send and receive messages asynchronously via a MOM server. As enterprise beans, MDBs are managed by the container and declaratively configured by an EJB deployment descriptor:
<ejb-jar>
<enterprise-beans>
<message-driven>
<ejb-name>MyMessageBean</ejb-name>
<ejb-class>com.gabhart.ejb.messaging.MessageBean</ejb-class>
<transaction-type>Container</transaction-type>
<message-driven-destination>
<destination-type>javax.jms.Queue</destination-type>
</message-driven-destination>
...
</message-driven>
....
</enterprise-beans>
</ejb-jar>
All of these element names should look familiar except for two:
<message-driven> This element represents the bean type. It is a direct parallel for the elements <session> and <entity>.
<message-driven-destination> This element indicates the JMS destination type (topic or queue) to which the MDB will bind.