📄 resin-messaging.xtp
字号:
<document> <header> <product>resin</product> <title>Resin Messaging</title> <description> <p>Configuration for Resin's JMS provider implementation. The JDBC Queues and Topics provide a persistent messaging store. The Memory Queues and Topics provide a low-overhead memory-based store.</p> </description> </header> <body> <localtoc/><s1 title="Messaging hello, world"><p>Resin's messaging is build around JMS, the Javamessaging service API and EJB message driven beans. A simple messagingapplication can use the <code>BlockingQueue</code> API to send messagesand implement a <code>MessageListener</code> to receive messages.</p><p>Because messaging is integrated with the <a href="resin-ioc.xtp">Resin IoCcontainer</a>, applications can use standard WebBeans injection toobtain the queues, avoiding code dependencies and improving testing.</p><p>The following example sends a "hello, world" messagefrom <code>MySendingServlet</code> and processes itin <code>MyListener</code>. The servlet does not wait for the listener,it completes immediately. The <code>MyListener</code> message beanwill receive the message when it's available.</p><p>The <code>@Named</code> WebBeans annotation tells Resin to lookfor a configured <code>BlockingQueue</code> named "myQueue" and injectit into the <code>_queue</code> variable when the servlet is initialized.The <code>offer</code> method sends the message to the JMS queue usinga JMS <code>ObjectMessage</code>.</p><example title="demo/MySendingServlet.java">package demo;import java.io.*;import javax.servlet.*;import java.util.BlockingQueue;import javax.webbeans.Named;public MySendingServlet extends GenericServlet{ @Named("myQueue") private BlockingQueue _queue; public void service(ServletRequest req, ServletResponse res) throws IOException, ServletException { String msg = "hello, world"; _queue.offer(msg); System.out.println("Sent: " + msg); }}</example><p>The EJB message bean service will receive the message and pass italong to <code>MyListener</code>. Since Resin's <code>BlockingQueue</code>API automatically wraps the object in a JMS <code>ObjectMessage</code>,<code>MyListener</code> needs to unwrap it.</p><example title="demo/MyListener.java">package demo;import javax.jms.*;public MyListener implements MessageListener{ public void onMessage(Message message) { ObjectMessage oMsg = (ObjectMessage) message; System.out.println("Received: " + oMsg.getObject()); }}</example><p>Now that the code's written, we just need to configure it in the<code>WEB-INF/resin-web.xml</code>. The <jms-connection-factory>configures Resin as the JMS provider, the <jms-queue> configuresa memory-based queue as the implementation, and the <ejb-message-bean>configures our listener.</p><example title="WEB-INF/resin-web.xml"><web-app xmlns="http://caucho.com/ns/resin"> <jms-connection-factory uri="resin:"/> <jms-queue name="myQueue" uri="memory:"/> <ejb-message-bean class="demo.MyListener"> <destination>#{myQueue}</destination> </ejb-message-bean> <servlet-mapping url-pattern="/test" servlet-class="demo.MySendingServlet"/></web-app></example></s1><s1 title="JMS Queues"><example title="resin-web.xml - queue configuration"><web-app xmlns="http://caucho.com/ns/resin"> <jms-queue name="my-queue" uri="memory:"/> <jms-connection-factory uri="resin:"/></web-app></example><s2 title="Memory Queue"><p>Resin's memory queue is a basic, non-persistent queue suitablefor testing and for cases where losing the queue contents at a servercrash is acceptable. Like Resin's other queues, you can use the<code>BlockingQueue</code> API to send messages, and use a simplelistener to receive messages.</p><example title="resin-web.xml - Memory queue and listener"><web-app xmlns="http://caucho.com/ns/resin"> <jms-connection-factory uri="resin:"/> <jms-queue name="myQueue" uri="memory:"/> <ejb-message-bean class="demo.MyListener"> <destination>#{myQueue}</destination> </ejb-message-bean></web-app></example></s2><s2 title="File Queue"><p>The file queue backs messages on the local filesystem, allowingfor recovery in case of system crash. The saved file is efficient,using the same backing store as Resin's proxy caching andpersistent sessions.</p><p>The file queue configuration requires an additional 'path' parameterto specify a directory for the backing files.</p><example title="resin-web.xml - file queue and listener"><web-app xmlns="http://caucho.com/ns/resin"> <jms-queue name="myQueue" uri="file:path=WEB-INF/messaging"/> <jms-connection-factory uri="resin:"/> <ejb-message-bean class="demo.MyListener"> <destination>#{myQueue}</destination> </ejb-message-bean></web-app></example></s2><s2 title="Cluster Server Queue"><p>The cluster server queue is a file queue which can also receivemessages from the local cluster. On the local machine, it acts exactlylike the file queue. When used with the cluster client queue, clientscan distribute messages to any server queue in the cluster, allowingfor load balancing.</p><p>Like the file queue, the cluster server queue requires a 'path' attributeto specify the location of the backing file.</p><example title="resin-web.xml - cluster server queue and listener"><web-app xmlns="http://caucho.com/ns/resin"> <jms-queue name="myQueue" uri="server:"> <init> <name>my-queue</name> <path>WEB-INF/jms</path> </init> </jms-queue> <jms-connection-factory uri="resin:"/> <ejb-message-bean class="demo.MyListener"> <destination>#{myQueue}</destination> </ejb-message-bean></web-app></example></s2><s2 title="Cluster Client Queue"><p>The client queue distributes messages to server queues in acluster. Normally, only the sending methods are used for the clientqueue; the receiving message beans are handled by the server queues.</p><p>The client queue needs to configure the cluster of the server queues.The cluster can be different from the client's own cluster.</p><example title="resin-web.xml - client queue"><web-app xmlns="http://caucho.com/ns/resin"> <jms-queue name="myQueue" uri="client:cluster=message-tier"/> <jms-connection-factory uri="resin:"/></web-app></example></s2></s1><s1 title="JMS Topics"><example title="resin-web.xml - topic configuration"><web-app xmlns="http://caucho.com/ns/resin"> <jms-topic name="my-topic" uri="memory:"/> <jms-connection-factory uri="resin:"/></web-app></example><s2 title="Memory Topic"><p>Resin's memory topic is a basic, non-persistent topic suitablefor testing and for cases where losing the topic contents at a servercrash is acceptable. Like Resin's other topics, you can use the<code>BlockingQueue</code> API to send messages, and use a simplelistener to receive messages.</p><example title="resin-web.xml - Memory topic and listener"><web-app xmlns="http://caucho.com/ns/resin"> <jms-connection-factory uri="resin:"/> <jms-topic name="myTopic" uri="memory:"/> <ejb-message-bean class="demo.MyListener"> <destination>#{myTopic}</destination> </ejb-message-bean></web-app></example></s2><s2 title="File Topic"><p>The file topic backs messages on the local filesystem forpersistent subscriptions. Non-persistent subscriptions usethe memory topic interface. The saved file is efficient,using the same backing store as Resin's proxy caching andpersistent sessions.</p><p>The file topic configuration requires an additional 'path' parameterto specify a directory for the backing files.</p><example title="resin-web.xml - file topic and listener"><web-app xmlns="http://caucho.com/ns/resin"> <jms-topic name="myTopic" uri="file:path=WEB-INF/messaging"/> <jms-connection-factory uri="resin:"/> <ejb-message-bean class="demo.MyListener"> <destination>#{myTopic}</destination> </ejb-message-bean></web-app></example></s2><!--<s2 title="Cluster Server Topic"><p>The cluster server topic is a file topic that serves as a hub formessages from the local cluster. On the local machine, it acts exactlylike the file topic. When used with the cluster client topic, clientscan distribute messages to any server topic in the cluster, allowingfor load balancing.</p><p>Like the file topic, the cluster server topic requires a 'path' attributeto specify the location of the backing file.</p><example title="resin-web.xml - cluster server topic and listener"><web-app xmlns="http://caucho.com/ns/resin"> <jms-topic name="myTopic" uri="server:"> <init> <name>my-topic</name> <path>WEB-INF/jms</path> </init> </jms-topic> <jms-connection-factory uri="resin:"/> <ejb-message-bean class="demo.MyListener"> <destination>#{myTopic}</destination>
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -