Message-Driven Beans (MDB) are used to support asynchronous communication within an application. Typically, they are used in conjunction with a queue. A client will send a message to a queue and the MDB will then process the message in the queue. The client does not call the MDB directly, but instead communicates through messages. The MDB never returns a value to the client.
Java Message Service (JMS) is the basis for communication between a client and the MDB. Fortunately, many of the details needed to use JMS are hidden thus making the job of the EJB developer easier.
In this recipe we show how to create the MDB. In the next recipe, we will modify the SalutationServlet
developed in the second recipe to send a message to our MDB.
Open the SalutationApplication
and add the SalutationMessageBean
to the SalutationApplication-ejb
module and the packt
package.
@MessageDriven(mappedName = "jms/SalutationQueue", activationConfig = { @ActivationConfigProperty(propertyName = "acknowledgeMode", propertyValue = "Auto-acknowledge"), @ActivationConfigProperty(propertyName = "destinationType", propertyValue = "javax.jms.Queue") }) public class SalutationMessageBean implements MessageListener { public SalutationMessageBean() { } @Override public void onMessage(Message message) { try { String name = message.getStringProperty("name"); Logger.getLogger("SalutationLog").log(Level.INFO, "Salutation processed", ""); } catch (JMSException e) { throw new RuntimeException(e); } } }
The next recipe will demonstrate the use of this MDB.
The @MessageDriven annotation is more complex than most annotations but understanding it is by no means insurmountable. The annotation has five possible attributes. For our SalutationMessageBean
we used only two of them, mappedName
and activationConfig
.
The mappedName
attribute is the simplest one. It is a vendor-specific name and maps the MDB to a JMS queue. When a message appears in the queue, it is sent to the MDB's onMessage
method. We used the name jms/SalutationQueue
. This is the name configured by the server and represents the queue we want to use. Most servers provide a way of creating and naming JMS resources, such as a queue.
mappedName = "jms/SalutationQueue",
The activationConfig
attribute is concerned with how the MDB works in its environment. This can include issues such as how messages are acknowledged and the type of destination used. We addressed these two issues in our MDB. Nested within the @MessageDriven annotation were two @ActivationConfigProperty annotations. These were used to specify the acknowledgement mode and the destination type.
The @ActivationConfigProperty annotation has a propertyName
attribute and a propertyValue
attribute. These are used together to specify the name and value of a property. For our MDB the acknowledgement mode was set to "Auto-acknowledge" and the destination type was the javax.jms.Queue
interface.
@MessageDriven(mappedName = "jms/SalutationQueue", activationConfig = { @ActivationConfigProperty(propertyName = "acknowledgeMode", propertyValue = "Auto-acknowledge"), @ActivationConfigProperty(propertyName = "destinationType", propertyValue = "javax.jms.Queue") })
When a message arrives at the message queue it is sent to the onMessage
method of the MDB. This method has a single parameter, a javax.jms.Message
object. Depending on the type of message sent, various methods can be applied against this object to return information needed by the bean. In the earlier code sequence, the value of a name
property was returned and used as part of the logging operation.
There is a lot more to MDBs than what we have seen here. But let's not spoil the fun; many useful MDB recipes are found in Chapter 3,
18.118.2.240