What is message-driven bean / message-driven bean?
A message-driven bean is a component of a lightweight enterprise application that is used to process messages in an asynchronous mode, in which the user does not always get results immediately. Each component can send these messages regardless of whether they are using J2EE technology.
The two different functions that distinguish message-driven beans from session and entity beans are that message-driven beans cannot be accessed via interfaces and they only have one bean class.
Message beans are also noted for the following characteristics:
The instances of message-driven beans do not maintain any data or conversation state for a particular client.
All instances of a message-driven bean are equivalent, so that the EJB container can assign a message to each message-driven bean instance. The container can combine these instances so that message streams can be processed at the same time.
A single message-driven bean can process messages from multiple clients.
Message-driven beans are used in asynchronous communication between enterprise application components. A message is processed as follows:
1.) When a new message arrives, the Enterprise JavaBeans container calls the onMessage method of the message-driven bean to process the message.
2.) The message is converted into a JMS message which is processed based on the business logic of the application. When the onMessage method just wants to process the message, it calls a session bean. However, when the method wants to store the message in a database, it calls an entity bean.
3.) The message is then sent to a message-driven bean so that the above operations become part of a single and complete transaction. A message is sometimes resent when message processing is rolled back.