2. What Makes Message-Driven Beans
Different from Session and Entity Beans?
The most visible difference between
message-driven beans and session and
entity beans is that clients do not access
message-driven beans through interfaces.
3. When to Use Message-Driven Beans
• Session beans and entity beans allow you to send JMS
messages and to receive them synchronously, but not
asynchronously.
• To avoid tying up server resources, you may prefer not
to use blocking synchronous receives in a server-side
component.
• To receive messages asynchronously, use a message-
driven bean.
4. Message-Driven Bean
• MDB is designed for clients to invoke server-side
business logic using asynchronous communication,
which is a special case of a stateless session bean.
• Note: There isn’t any home interface or remote
interface for a MDB.
• MDB monitors Java Message Service communications
and reacts to messages sent by clients.
• Clients don’t directly access a MDB.
5. • The MDB intercedes and processes requests anonymously.
This is possible because MDB is stateless.
• The EJB container can begin a new instance of the MDB when
the message is received or use an existing instance of the MDB
from the instance pool.
Note: Unlike a session bean or entity bean, a client does not
control the life of an MDB.
• The EJB container handles the responsibility for creating and
removing an MDB.
6. • Requests from clients are sent via JMS.
• The EJB container listens for messages in the JMS
service that MDBs are registered to receive.
• The developer must provide all the logic to process a
message in the onMessage() method.
• The JMS service enables the client and MDB to work
independently and without having to wait until the
other is finished processing.
7. • An MDB processes a client request by using business
methods defined in the MDB and by invoking
business methods defined in other EJBs.
• An advantage of using an MDB is that the EJB
container does most of the work.
• The EJB container acts as the intermediary between
JMS service and the MDB.
8. • The EJB container makes a connection to the JMS service and
registers to receive JMS messages that have a particular JMS
Topic or that are on a particular JMS message Queue,
depending on if a client uses JMS topics or a JMS message
queue to send a request for service.
• The EJB container then creates a TopicSubsriber object or
QueueReceiver object, depending on how JMS messages are
handled by the JMS server.
• The TopicSubscriber object or QueueReceive object is then
used to receive JMS messages from clients.
9. • The EJB container knows which receiver object to create
because the receiver object is specified in the MDB deployment
descriptor.
• The EJB container also registers the message listener and sets
the message acknowledge mode to respond to the JMS server
when a JMS message is received by the EJB container.
• NOTE: All this occurs behind the scenes without the MDB
developer needing to write additional code in the MDB.
10. The Life Cycle of a Message-Driven Bean
Figure: illustrates the stages in the life cycle of a message-driven bean.
The EJB container usually creates a pool of message-driven bean instances.
For each instance, the EJB container instantiates the bean and performs
these tasks:
1. It calls the setMessageDrivenContext method to pass the context object to
the instance.
2. It calls the instance's ejbCreate method.
F Life Cycle of a Message-Driven
Bean
11. Creating an MDB
An MDB must define four methods:
1. ejbCreate() : is called when the MDB is created by the
container first invoked by the EJB container, but not when a
JMS message is received from a client.
2. ejbRemove(): is called by the EJB container when the
container terminates the instance of the MDB.
3. setMessageDrivenContext(): creates the context for the
MDB, which is similar to the session and entity context
classes.
12. 4. onMessage(): is called each time the EJB container
receives a JMS message from a client.
The onMessage() method is where the MDB processes
messages received indirectly from a client. Any type
of stateless process can be included in this method.
13. • A skeleton of an MDB
import javax.ejb.MessageDrivenBean;
import javax.ejb.MessageDrivenContext;
import javax.jms.MessageListener;
import javax.jms.Message;
public class myMDB implements MessageListener, MessageDrivenBean
{
public void setMessageDrivenContext(MessageDrivenContext mdc)
{}
public void ejbCreate(){}
public void ejbRemove(){}
public void onMessage(Message clientMessage){}
}
14. • An onMessage() method displays the message received from
a client
public void onMessage(Message clientMessage){
TextMessage tmpMesg = null;
try{
if(clientMessage instanceof TextMessage)
{ tmpMesg = (TextMessage) clientMessage;
System.out.println(“Incoming Message:”+tmpMesg.getText());}
else
{System.out.println(“Incorrect
Message:”+clientMessage.getClass().getName());}
}
catch(JMSException error)
{
System.out.println(“Error:”+error.getMessage());
}
}
15. The JAR File:
o EJB classes & related files are packaged together into a Java
Archive (JAR) file for deployment.
o The JAR file is a compressed file format that was originally
designed to reduce the size of software so it could be easily be
transported.
o The JAR file used to package an EJB must contain the following:
o EJB classes
o Dependent classes
o Remote interface
o Home interface
o Dependent interfaces
o Primary key class
o Deployment descriptor
NOTE: In addition, the
deployment descriptor
must be located in the
META-INF/ejb-jar.xml
path.