JMS Listener IoC
Resin 3.1

Documentation
Examples
Changes

Quercus
Database
Amber
EJB
SOA/ESB
IoC
JMS
Servlet
JMX
Hessian
Security

JMS/PHP Send
JMS/PHP Receive
JMS Listener
JMS IOC Listener
JMS Listener
JMS
Servlet

Introduces the JMS message listener configured with IoC.

Demo

Files in this tutorial

WEB-INF/web.xmlConfigures the Queue, MessageSender, MessageListener.
WEB-INF/classes/example/MyListener.javaThe message listener.
WEB-INF/classes/example/MessageServlet.javaThe message servlet

Overview

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.

Producer (MessageServlet)

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.

MessageServlet
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.

Consumer (MyListener)

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.

MyListener
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);
    }
  }
}

Configuration

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.)

Queue resin-web.xml fragment
<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.

ConnectionFactory resin-web.xml fragment
<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.

MyListener configuration resin-web.xml
<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.

MessageSender configuration resin-web.xml
<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

Demo


JMS Listener
JMS
Servlet
Copyright © 1998-2006 Caucho Technology, Inc. All rights reserved.
Resin ® is a registered trademark, and Quercustm, Ambertm, and Hessiantm are trademarks of Caucho Technology.