![]() |
| |||||||||||||||||||||
| 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 |
Introduces the JMS message listener configured with IoC. Files in this tutorial
OverviewMessaging 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:
Producer (MessageServlet)In this example, the Producer is a Servlet which sends a simple message.
The Producer uses a String message = "sample message"; MessageSender sender = ...; // JNDI lookup sender.send(null, message); In this configuration, the The 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 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);
}
}
}
ConfigurationSince 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 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>
| |||||||||||||||||||||