SlideShare a Scribd company logo
Message-Driven Bean [ MDB ]
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.
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.
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.
• 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.
• 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.
• 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.
• 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.
• 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.
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
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.
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.
• 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){}
}
• 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());
}
}
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.

More Related Content

PDF
JMS - Java Messaging Service
PDF
Understanding JMS Integration Patterns
PPT
JMS Introduction
PPTX
Jms topics
PPTX
Spring JMS
PPTX
M messaging 1
PPTX
JMS Providers Overview
JMS - Java Messaging Service
Understanding JMS Integration Patterns
JMS Introduction
Jms topics
Spring JMS
M messaging 1
JMS Providers Overview

Similar to Module5 enterprise java beans (20)

PPTX
enterprise java bean
PPTX
Introduction-to-JMS-and-MDB java ejb .pptx
PDF
PPTX
PDF
Introcution to EJB
PPT
Weblogic - Introduction to configure JMS
PPT
Unite5-EJB-2019.ppt
PPT
j2ee Building components
PDF
Ejb intro
PPT
EJBDetailsFeb25.ppt
PPTX
EJB3 Basics
PDF
Enterprise Java Beans - EJB
PPTX
Java Message Service
PDF
Message Driven Beans (6)
PPTX
Java bean
PPTX
Enterprise java beans
PDF
Ejb intro
enterprise java bean
Introduction-to-JMS-and-MDB java ejb .pptx
Introcution to EJB
Weblogic - Introduction to configure JMS
Unite5-EJB-2019.ppt
j2ee Building components
Ejb intro
EJBDetailsFeb25.ppt
EJB3 Basics
Enterprise Java Beans - EJB
Java Message Service
Message Driven Beans (6)
Java bean
Enterprise java beans
Ejb intro
Ad

Recently uploaded (20)

PPTX
Introduction_to_Human_Anatomy_and_Physiology_for_B.Pharm.pptx
PDF
Pre independence Education in Inndia.pdf
PPTX
master seminar digital applications in india
PDF
Abdominal Access Techniques with Prof. Dr. R K Mishra
PDF
Microbial disease of the cardiovascular and lymphatic systems
PDF
Complications of Minimal Access Surgery at WLH
PDF
TR - Agricultural Crops Production NC III.pdf
PDF
Computing-Curriculum for Schools in Ghana
PDF
Chapter 2 Heredity, Prenatal Development, and Birth.pdf
PDF
STATICS OF THE RIGID BODIES Hibbelers.pdf
PDF
102 student loan defaulters named and shamed – Is someone you know on the list?
PDF
Physiotherapy_for_Respiratory_and_Cardiac_Problems WEBBER.pdf
PPTX
human mycosis Human fungal infections are called human mycosis..pptx
PPTX
PPT- ENG7_QUARTER1_LESSON1_WEEK1. IMAGERY -DESCRIPTIONS pptx.pptx
PDF
grade 11-chemistry_fetena_net_5883.pdf teacher guide for all student
PDF
Anesthesia in Laparoscopic Surgery in India
PDF
3rd Neelam Sanjeevareddy Memorial Lecture.pdf
PDF
2.FourierTransform-ShortQuestionswithAnswers.pdf
PPTX
Pharmacology of Heart Failure /Pharmacotherapy of CHF
PPTX
school management -TNTEU- B.Ed., Semester II Unit 1.pptx
Introduction_to_Human_Anatomy_and_Physiology_for_B.Pharm.pptx
Pre independence Education in Inndia.pdf
master seminar digital applications in india
Abdominal Access Techniques with Prof. Dr. R K Mishra
Microbial disease of the cardiovascular and lymphatic systems
Complications of Minimal Access Surgery at WLH
TR - Agricultural Crops Production NC III.pdf
Computing-Curriculum for Schools in Ghana
Chapter 2 Heredity, Prenatal Development, and Birth.pdf
STATICS OF THE RIGID BODIES Hibbelers.pdf
102 student loan defaulters named and shamed – Is someone you know on the list?
Physiotherapy_for_Respiratory_and_Cardiac_Problems WEBBER.pdf
human mycosis Human fungal infections are called human mycosis..pptx
PPT- ENG7_QUARTER1_LESSON1_WEEK1. IMAGERY -DESCRIPTIONS pptx.pptx
grade 11-chemistry_fetena_net_5883.pdf teacher guide for all student
Anesthesia in Laparoscopic Surgery in India
3rd Neelam Sanjeevareddy Memorial Lecture.pdf
2.FourierTransform-ShortQuestionswithAnswers.pdf
Pharmacology of Heart Failure /Pharmacotherapy of CHF
school management -TNTEU- B.Ed., Semester II Unit 1.pptx
Ad

Module5 enterprise java beans

  • 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.