resin-ee JMS Listener IoC

Introduces the JMS message listener configured with IoC.

tutorial send
Configures the Queue, MessageSender, MessageListener. The message listener. The message servlet

Messaging lets a servlet delegate processing to a batch process either on the same machine or on a separate machine. The servlet creates a message and sends it to a queue. The servlet immediately completes and when the batch process is ready, it processes the message.

Messaging is therefore comprised of three main components:

  • A Producer creates messages and sends them to a Queue. The Producer could be something like a Servlet.
  • A Queue stores the messages from the Produces and provides them to a Consumer when ready. The Queue is implemented by the messaging provider.
  • A Consumer processes messages as they become available in the Queue. The Consumer is typically a bean implementing the MessageListener interface.

In this example, the Producer is a Servlet which sends a simple message. The Producer uses a MessageSender configured in the web.xml to send the message.

String message = "sample message"; MessageSender sender = ...; // JNDI lookup sender.send(null, message);

In this configuration, the MessageSender is a com.caucho.services.message.MessageSender. It's also possible to use the full JMS MessageProducer which is more verbose. The MessageSender is an interface available in the open source Hessian distribution, so it can be used in other application servers as a convenient facade.

The send method completes as soon as the message is stored in the queue. Laster, when a thread is available, the Queue will send the message to the Consumer.

The Queue delivers message to the Consumer one by one. When the Consumer finishes processing a message the Queue will deliver the next available message.

The Consumer implements javax.jms.MessageListener, and will therefore be identical code in any application server. The Consumer might even be on a different server or use a different application server.

In this example, the Consumer just logs the message.

package example; import java.util.logging.Logger; import java.util.logging.Level; import javax.jms.Message; import javax.jms.TextMessage; import javax.jms.MessageListener; public class MyListener implements MessageListener { private static final Logger log = Logger.getLogger(MyListener.class.getName()); public void onMessage(Message message) { try { TextMessage textMessage = (TextMessage) message; log.info("received: " + textMessage.getText()); _lastMessage = textMessage.getText(); } catch (Throwable e) { log.log(Level.WARNING, e.toString(), e); } } }

Since Resin is an inversion of control container (IoC), it can configure the JMS resources in the standard Resin configuration file.

The configuration is responsible for three things:

  • Configuring the Queue
  • Configuring the MessageSender
  • Configuring the MessageListener

The Queue is configured directly in a <resource> tag, together with any configuration setters. This example uses a local JDBC store.

Because the listener and sender need a reference to the queue, the resource stores it in the "queue" variable. (It could also have used jndi-name to store the queue in JNDI.)

<resource var="queue" type="com.caucho.jms.jdbc.JdbcQueue"> <init> <queue-name>resin</queue-name> <data-source>jdbc/resin</data-source> </init> </resource>

JMS also needs a configured ConnectionFactory, so the sender and listener can create JMS connections.

<resource var="jmsFactory" type="com.caucho.jms.ConnectionFactoryImpl"> </resource>

The MessageListener is configured and registered with Resin's MessageListenerResource. That resource instantiates the listeners and receives messages from the queue.

<resource type="com.caucho.jms.resource.ListenerResource"> <init> <connection-factory>\${jmsFactory}</connection-factory> <destination>\${queue}</destination> <listener type="example.MyListener"/> </init> </resource>

Finally, we configure a MessageSender. This step is optional, since the application could use the JMS ConnectionFactory with the Queue directly, if it wanted.

<resource jndi-name="jms/sender" type="com.caucho.jms.resource.MessageSenderResource"> <init> <connection-factory>\${jmsFactory}</connection-factory> <destination>\${queue}</destination> </init> </resource> tagmeaning resourceConfigures a bean using Resin's IoC configuration typeThe type of a resource to be configured