JMS Listener IoC
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
Tutorials
Tutorials
JCA Listener

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

Introduces the JMS message listener configured with IoC.

  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

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

Try the Tutorial


Tutorials
Tutorials
JCA Listener
Copyright © 1998-2006 Caucho Technology, Inc. All rights reserved.
Resin® is a registered trademark, and HardCoretm and Quercustm are trademarks of Caucho Technology, Inc.