📄 index.xtp
字号:
<document> <header> <product>resin-ee</product> <title>JMS Listener</title> <description> <p>Introduces the JMS message listener configured with JCA.</p> </description> <type>tutorial</type> <tutorial-startpage>send</tutorial-startpage> </header><body><summary/><s1 title="Files in this tutorial"><deftable><tr><td><viewfile-link file="WEB-INF/web.xml"/> </td><td>Configures the Queue, MessageSender, MessageListener.</td></tr><tr><td><viewfile-link file="WEB-INF/classes/example/MyListener.java"/> </td><td>The message listener.</td></tr><tr><td><viewfile-link file="WEB-INF/classes/example/MessageServlet.java"/> </td><td>The message servlet</td></tr></deftable></s1><s1 title="Overview"><p>Messaging lets a servlet delegate processing to a batch process eitheron the same machine or on a separate machine. The servlet creates a messageand sends it to a queue. The servlet immediately completes and when thebatch process is ready, it processes the message.</p><p>Messaging is therefore comprised of three main components:</p><ul><li>A <var>Producer</var> creates messages and sends them toa <var>Queue</var>. The Producer could be something like a Servlet.</li><li>A <var>Queue</var> stores the messages from the Produces and providesthem to a Consumer when ready. The Queue is implemented by themessaging provider.</li><li>A <var>Consumer</var> processes messages as they becomeavailable in the Queue. The <var>Consumer</var> is typically a beanimplementing the <code>MessageListener</code> interface.</li></ul></s1><s1 title="Producer (MessageServlet)"><p>In this example, the Producer is a Servlet which sends a simple message.The Producer uses a <code>MessageSender</code> configured in the web.xmlto send the message.</p><example title="MessageServlet">String message = "sample message";MessageSender sender = ...; // JNDI lookupsender.send(null, message);</example><p>In this configuration, the <code>MessageSender</code> is a<code>com.caucho.services.message.MessageSender</code>. It's also possibleto use the full JMS <code>MessageProducer</code> which is more verbose.The <code>MessageSender</code> is an interface available in the opensource <a href="http://www.caucho.com/hessian">Hessian distribution</a>,so it can be used in other application servers as a convenient facade.</p><p>The <code>send</code> method completes as soon asthe message is stored in the queue. Laster, when a thread is available, theQueue will send the message to the Consumer.</p></s1><s1 title="Consumer (MyListener)"><p>The Queue delivers message to the Consumer one by one. When theConsumer finishes processing a message the Queue will deliver the nextavailable message.</p><p>The Consumer implements <code>javax.jms.MessageListener</code>, andwill therefore be identical code in any application server. TheConsumer might even be on a different server or use a differentapplication server.</p><p>In this example, the Consumer just logs the message.</p><example title="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); } }}</example></s1><s1 title="Configuration"><p>The configuration is responsible for three things:</p><ul><li>Configuring the Queue</li><li>Configuring the MessageSender</li><li>Configuring the MessageListener</li></ul><p>The JMS <code>Queue</code> and its <code>ConnectionFactory</code>are configured in the <resource-adapter> section. Any JMS 1.1implementation may be used for the <connection-factory> and<destination>.</p><p>The <connection-factory> configures the MessageSender and savesit in JNDI.</p><p>The <message-listener> and <endpoint-factory> configuresthe MessageListener.</p><example title="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></example><deftable><tr><th>tag</th><th>meaning</th></tr><tr><td>connector</td><td>top-level configuration for the JCA connector</td></tr><tr><td>type</td><td>The type of the connector.</td></tr><tr><td>resource-adapter</td><td>configures the connector/queue as a whole</td></tr><tr><td>init</td><td>Bean-style initialization for each resource</td></tr><tr><td>connection-factory</td><td>The JMS ConnectionFactory class</td></tr><tr><td>resin:type</td><td>The class name</td></tr><tr><td>destination</td><td>The JMS Queue or Topic</td></tr><tr><td>connection-factory</td><td>Configures a Producer</td></tr><tr><td>jndi-name</td><td>The JNDI name where the resource is stored</td></tr><tr><td>message-listener</td><td>Configures a Consumer</td></tr><tr><td>endpoint-factory</td><td>consumer-specific endpoint type</td></tr><tr><td>listener</td><td>configures the user's listener</td></tr></deftable><deftable><tr><th>class</th><th>meaning</th></tr><tr><td>com.caucho.jms.jca.ResourceAdapterImpl</td><td>Resin's JCA adapterfor the MessageSender and MessageListener</td></tr><tr><td>com.caucho.jms.ConnectionFactoryImpl</td><td>Resin's JMS ConnectionFactory</td></tr><tr><td>com.caucho.jms.memory.MemoryQueue</td><td>Resin's in-memory JMS Queue</td></tr><tr><td>com.caucho.jms.jca.MessageListenerSpec</td><td>Configuration forthe JCA adapter's Consumer</td></tr><tr><td>com.caucho.jms.jca.ListenerEndpointFactory</td><td>Specifies theconfiguration for a MessageListener</td></tr><tr><td>example.MyListener</td><td>Example application code</td></tr></deftable></s1></body></document>
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -