JMS Listener
Resin 3.0

Features
Installation
Configuration
Web Applications
IOC/AOP
Resources
JSP
Quercus
Servlets and Filters
Databases
Admin (JMX)
CMP
EJB
Amber
EJB 3.0
Security
XML and XSLT
XTP
JMS
Performance
Protocols
Third-party
Troubleshooting/FAQ

Config
SwiftMQ
Tutorials

Simple Listener
JCA Listener
JMS in Quercus - Sending messages
JMS in Quercus - Receiving messages
Simple Listener
Tutorials
JMS in Quercus - Sending messages

Find this tutorial in: /usr/local/resin/webapps/resin-doc/jms/tutorial/listener
Try the Tutorial

Introduces the JMS message listener configured with JCA.

  1. Files in this tutorial
  2. Overview
  3. Producer (MessageServlet)
  4. Consumer (MyListener)
  5. Configuration

Files in this tutorial

WEB-INF/web.xml Configures the Queue, MessageSender, MessageListener.
WEB-INF/classes/example/MyListener.java The message listener.
WEB-INF/classes/example/MessageServlet.java The 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

The configuration is responsible for three things:

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

The JMS Queue and its ConnectionFactory are configured in the <resource-adapter> section. Any JMS 1.1 implementation may be used for the <connection-factory> and <destination>.

The <connection-factory> configures the MessageSender and saves it in JNDI.

The <message-listener> and <endpoint-factory> configures the MessageListener.

web.xml
<web-app xmlns="http://caucho.com/ns/resin">
  <connector>
    <type>com.caucho.jms.jca.ResourceAdapterImpl</type>

    <resource-adapter>
      <init>
        <connection-factory resin:type="com.caucho.jms.ConnectionFactoryImpl"/>

        <destination resin:type="com.caucho.jms.memory.MemoryQueue"/>
      </init>
    </resource-adapter>

    <connection-factory jndi-name="jms/sender"
                        type="com.caucho.jms.jca.MessageSenderManager"/>

    <message-listener type="com.caucho.jms.jca.MessageListenerSpec">
      <endpoint-factory type="com.caucho.jms.jca.ListenerEndpointFactory">
        <init>
          <listener resin:type="example.MyListener"/>
        </init>
      </endpoint-factory>
    </message-listener>
  </connector>
</web-app>

tagmeaning
connectortop-level configuration for the JCA connector
typeThe type of the connector.
resource-adapterconfigures the connector/queue as a whole
initBean-style initialization for each resource
connection-factoryThe JMS ConnectionFactory class
resin:typeThe class name
destinationThe JMS Queue or Topic
connection-factoryConfigures a Producer
jndi-nameThe JNDI name where the resource is stored
message-listenerConfigures a Consumer
endpoint-factoryconsumer-specific endpoint type
listenerconfigures the user's listener

classmeaning
com.caucho.jms.jca.ResourceAdapterImplResin's JCA adapter for the MessageSender and MessageListener
com.caucho.jms.ConnectionFactoryImplResin's JMS ConnectionFactory
com.caucho.jms.memory.MemoryQueueResin's in-memory JMS Queue
com.caucho.jms.jca.MessageListenerSpecConfiguration for the JCA adapter's Consumer
com.caucho.jms.jca.ListenerEndpointFactorySpecifies the configuration for a MessageListener
example.MyListenerExample application code

Try the Tutorial


Simple Listener
Tutorials
JMS in Quercus - Sending messages
Copyright © 1998-2006 Caucho Technology, Inc. All rights reserved.
Resin® is a registered trademark, and HardCoretm and Quercustm are trademarks of Caucho Technology, Inc.