| |||||||||||||||||||||||||||||||||||
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.
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:
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.
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.
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:
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.)
JMS also needs a configured ConnectionFactory, so the sender and listener can create JMS connections.
The MessageListener is configured and registered with Resin's MessageListenerResource. That resource instantiates the listeners and receives messages from the queue.
Finally, we configure a MessageSender. This step is optional, since the application could use the JMS ConnectionFactory with the Queue directly, if it wanted.
|