SlideShare a Scribd company logo
1 of 15
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

Similar to Module5 enterprise java beans

Java message service
Java message serviceJava message service
Java message service
Veeramani S
 

Similar to Module5 enterprise java beans (20)

test
testtest
test
 
test
testtest
test
 
test
testtest
test
 
test
testtest
test
 
test
testtest
test
 
test
testtest
test
 
test
testtest
test
 
test
testtest
test
 
Jms using j boss
Jms using j bossJms using j boss
Jms using j boss
 
Java Message Service
Java Message ServiceJava Message Service
Java Message Service
 
enterprise java bean
enterprise java beanenterprise java bean
enterprise java bean
 
Ems
EmsEms
Ems
 
Mule execution
Mule executionMule execution
Mule execution
 
mule real world
mule real worldmule real world
mule real world
 
Jms topics
Jms topicsJms topics
Jms topics
 
Java message service
Java message serviceJava message service
Java message service
 
A detailed Tibco EMS presentation
A detailed Tibco EMS presentationA detailed Tibco EMS presentation
A detailed Tibco EMS presentation
 
JMS
JMSJMS
JMS
 
Mule Fundamentals
Mule FundamentalsMule Fundamentals
Mule Fundamentals
 
Mule jms-topics
Mule jms-topicsMule jms-topics
Mule jms-topics
 

Recently uploaded

Activity 01 - Artificial Culture (1).pdf
Activity 01 - Artificial Culture (1).pdfActivity 01 - Artificial Culture (1).pdf
Activity 01 - Artificial Culture (1).pdf
ciinovamais
 
1029 - Danh muc Sach Giao Khoa 10 . pdf
1029 -  Danh muc Sach Giao Khoa 10 . pdf1029 -  Danh muc Sach Giao Khoa 10 . pdf
1029 - Danh muc Sach Giao Khoa 10 . pdf
QucHHunhnh
 
The basics of sentences session 2pptx copy.pptx
The basics of sentences session 2pptx copy.pptxThe basics of sentences session 2pptx copy.pptx
The basics of sentences session 2pptx copy.pptx
heathfieldcps1
 
The basics of sentences session 3pptx.pptx
The basics of sentences session 3pptx.pptxThe basics of sentences session 3pptx.pptx
The basics of sentences session 3pptx.pptx
heathfieldcps1
 

Recently uploaded (20)

Activity 01 - Artificial Culture (1).pdf
Activity 01 - Artificial Culture (1).pdfActivity 01 - Artificial Culture (1).pdf
Activity 01 - Artificial Culture (1).pdf
 
PROCESS RECORDING FORMAT.docx
PROCESS      RECORDING        FORMAT.docxPROCESS      RECORDING        FORMAT.docx
PROCESS RECORDING FORMAT.docx
 
On National Teacher Day, meet the 2024-25 Kenan Fellows
On National Teacher Day, meet the 2024-25 Kenan FellowsOn National Teacher Day, meet the 2024-25 Kenan Fellows
On National Teacher Day, meet the 2024-25 Kenan Fellows
 
General Principles of Intellectual Property: Concepts of Intellectual Proper...
General Principles of Intellectual Property: Concepts of Intellectual  Proper...General Principles of Intellectual Property: Concepts of Intellectual  Proper...
General Principles of Intellectual Property: Concepts of Intellectual Proper...
 
1029 - Danh muc Sach Giao Khoa 10 . pdf
1029 -  Danh muc Sach Giao Khoa 10 . pdf1029 -  Danh muc Sach Giao Khoa 10 . pdf
1029 - Danh muc Sach Giao Khoa 10 . pdf
 
TỔNG ÔN TẬP THI VÀO LỚP 10 MÔN TIẾNG ANH NĂM HỌC 2023 - 2024 CÓ ĐÁP ÁN (NGỮ Â...
TỔNG ÔN TẬP THI VÀO LỚP 10 MÔN TIẾNG ANH NĂM HỌC 2023 - 2024 CÓ ĐÁP ÁN (NGỮ Â...TỔNG ÔN TẬP THI VÀO LỚP 10 MÔN TIẾNG ANH NĂM HỌC 2023 - 2024 CÓ ĐÁP ÁN (NGỮ Â...
TỔNG ÔN TẬP THI VÀO LỚP 10 MÔN TIẾNG ANH NĂM HỌC 2023 - 2024 CÓ ĐÁP ÁN (NGỮ Â...
 
2024-NATIONAL-LEARNING-CAMP-AND-OTHER.pptx
2024-NATIONAL-LEARNING-CAMP-AND-OTHER.pptx2024-NATIONAL-LEARNING-CAMP-AND-OTHER.pptx
2024-NATIONAL-LEARNING-CAMP-AND-OTHER.pptx
 
The basics of sentences session 2pptx copy.pptx
The basics of sentences session 2pptx copy.pptxThe basics of sentences session 2pptx copy.pptx
The basics of sentences session 2pptx copy.pptx
 
Unit-IV; Professional Sales Representative (PSR).pptx
Unit-IV; Professional Sales Representative (PSR).pptxUnit-IV; Professional Sales Representative (PSR).pptx
Unit-IV; Professional Sales Representative (PSR).pptx
 
The basics of sentences session 3pptx.pptx
The basics of sentences session 3pptx.pptxThe basics of sentences session 3pptx.pptx
The basics of sentences session 3pptx.pptx
 
Holdier Curriculum Vitae (April 2024).pdf
Holdier Curriculum Vitae (April 2024).pdfHoldier Curriculum Vitae (April 2024).pdf
Holdier Curriculum Vitae (April 2024).pdf
 
Introduction to Nonprofit Accounting: The Basics
Introduction to Nonprofit Accounting: The BasicsIntroduction to Nonprofit Accounting: The Basics
Introduction to Nonprofit Accounting: The Basics
 
Python Notes for mca i year students osmania university.docx
Python Notes for mca i year students osmania university.docxPython Notes for mca i year students osmania university.docx
Python Notes for mca i year students osmania university.docx
 
Mixin Classes in Odoo 17 How to Extend Models Using Mixin Classes
Mixin Classes in Odoo 17  How to Extend Models Using Mixin ClassesMixin Classes in Odoo 17  How to Extend Models Using Mixin Classes
Mixin Classes in Odoo 17 How to Extend Models Using Mixin Classes
 
How to Give a Domain for a Field in Odoo 17
How to Give a Domain for a Field in Odoo 17How to Give a Domain for a Field in Odoo 17
How to Give a Domain for a Field in Odoo 17
 
ICT role in 21st century education and it's challenges.
ICT role in 21st century education and it's challenges.ICT role in 21st century education and it's challenges.
ICT role in 21st century education and it's challenges.
 
Food Chain and Food Web (Ecosystem) EVS, B. Pharmacy 1st Year, Sem-II
Food Chain and Food Web (Ecosystem) EVS, B. Pharmacy 1st Year, Sem-IIFood Chain and Food Web (Ecosystem) EVS, B. Pharmacy 1st Year, Sem-II
Food Chain and Food Web (Ecosystem) EVS, B. Pharmacy 1st Year, Sem-II
 
Measures of Central Tendency: Mean, Median and Mode
Measures of Central Tendency: Mean, Median and ModeMeasures of Central Tendency: Mean, Median and Mode
Measures of Central Tendency: Mean, Median and Mode
 
Mehran University Newsletter Vol-X, Issue-I, 2024
Mehran University Newsletter Vol-X, Issue-I, 2024Mehran University Newsletter Vol-X, Issue-I, 2024
Mehran University Newsletter Vol-X, Issue-I, 2024
 
Unit-IV- Pharma. Marketing Channels.pptx
Unit-IV- Pharma. Marketing Channels.pptxUnit-IV- Pharma. Marketing Channels.pptx
Unit-IV- Pharma. Marketing Channels.pptx
 

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.