Sending a message to a message-driven bean

Message-Driven Beans (MDB) are used in an asynchronous fashion. A client will create a message and then send it to a queue for processing. The MDB will effectively remove the message from the queue and act on it.

In this recipe, we will use the SalutationServlet to send a message to the queue each time a salutation is processed.

Getting ready

The process consists of two steps:

  1. Adding declarations for a queue factory and a message
  2. Adding code to actually send the message

    Review the SalutationApplication project as developed in the Accessing a session bean using dependency injection recipe. We will be modifying the SalutationServlet.

How to do it...

Start by adding declarations for a queue factory and a message queue as instance variables in the SalutationServlet.

@Resource(mappedName = "jms/SalutationQueueFactory")
private QueueConnectionFactory queueConnectionFactory;
@Resource(mappedName = "jms/SalutationQueue")
private Queue queue;

Next, we need to add code to send the message. This code is placed in front of the servlet's try block. This code can be used regardless of whether the salutation object is instantiated using DI or JNDI. In this code we will create a connection to a server-based queue. Once we have a connection we create a session which serves to facilitate communication. The session creates a message producer which sends the message. And of course, we need to handle exceptions.

This might sound kind of involved but don't worry; most of the individual steps are simple.

try {
String message = "Salutation generated";
Connection connection = queueConnectionFactory.createConnection();
Session session = connection.createSession(false, Session.AUTO_ACKNOWLEDGE);
MessageProducer messageProducer = (MessageProducer) session.createProducer(queue);
TextMessage textMessage = session.createTextMessage();
textMessage.setText(message);
messageProducer.send(textMessage);
Logger.getLogger("SalutationLog").log(Level.WARNING,
"Message sent successfully", "Message sent successfully2");
} catch (JMSException ex) {
Logger.getLogger("SalutationLog").log(Level.WARNING, "JMSException in SalutationServlet", "JMSException in SalutationServlet");
}

How it works...

First, we needed to create a connection to the queue. This step used a connection factory. As you are probably aware, the software factory pattern is used to, among other things, hide the details of how an object is created.

In addition to a connection factory, a destination queue was created. Most IDEs will assist in the creation of these resources. However, it may be necessary to use the server's administration console to create these resources. The connection factory was created by the server, GlassFish in this case, with the name jms/SalutationQueueFactory which is of type QueueConnectionFactory. The @Resource annotation was used to inject it into the servlet.

@Resource(mappedName = "jms/SalutationQueueFactory")
private QueueConnectionFactory queueConnectionFactory;

The queue is of type javax.jms.Queue and used the name jms/SalutationQueue as defined within the server. The @Resource annotation was used again to inject this resource.

Resource(mappedName = "jms/SalutationQueue")
private Queue queue;

A try block was needed to catch and handle any JMS exception thrown. Within the try block a message string was defined. This string was sent to the queue. Next, the QueueConnectionFactory object is used to create a QueueConnection. The QueueConnection represents a connection between two point-to-point JMS elements. Any exceptions were handled in the catch block.

try {
String message = "Salutation generated";
Connection connection = queueConnectionFactory.createConnection();
...
} catch (JMSException ex) {
Logger.getLogger("SalutationLog").log(Level.WARNING, "JMSException in SalutationServlet", "JMSException in SalutationServlet");
}

The next step created a Session object used for sending and receiving messages. The Connection object's createSession method uses two parameters. The first one indicates that the session is part of a transaction. The second argument specifies the type of acknowledgment to be made. Using Session.AUTO_ACKNOWLEDGE meant the session automatically acknowledged the receipt of the message.

Session session = connection.createSession(false,

The Session object was used to create a MessageProducer. The MessageProducer object was used later in this code sequence to send a message to the destination queue. The queue was used as the argument in the createProducer method.

MessageProducer messageProducer = (MessageProducer)

Next, a TextMessage was created using the createTextMessage method. A TextMessage is one of several interfaces derived from the Message interface. These JMS interfaces represent different types of messages used within JMS. The setText method assigned a string to be sent.

TextMessage textMessage = session.createTextMessage();

The messageProducer then sent the message.

messageProducer.send(textMessage);

The output was displayed in the log file used by the SalutationMessageBean.

INFO: Salutation processed

See also

The previous recipe explains the MDB used in this recipe.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
3.139.240.119