日期:2014-05-20 浏览次数:21252 次
package com.flvcd.servlet; import java.io.IOException; import java.io.PrintWriter; import javax.jms.Connection; import javax.jms.ConnectionFactory; import javax.jms.DeliveryMode; import javax.jms.Destination; import javax.jms.JMSException; import javax.jms.Message; import javax.jms.MessageListener; import javax.jms.MessageProducer; import javax.jms.Session; import javax.naming.Context; import javax.naming.InitialContext; import javax.naming.NamingException; import javax.servlet.ServletException; import javax.servlet.http.HttpServlet; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; public class MyPublish extends HttpServlet implements MessageListener { private InitialContext initCtx; private Context envContext; private ConnectionFactory connectionFactory; private Connection connection; private Session jmsSession; private MessageProducer producer; public void onMessage(Message message) { } public MyPublish(){ super(); } public void destroy(){ super.destroy(); } public void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { doPost(request, response); } public void doPost(HttpServletRequest request, HttpServletResponse response)throws ServletException, IOException{ String content=request.getParameter("content"); //设置持久方式 try { producer.setDeliveryMode(DeliveryMode.PERSISTENT); Message testMessage = jmsSession.createMessage(); // 发布刷新文章消息 testMessage.setStringProperty("RefreshArticleId", content); producer.send(testMessage); // 发布刷新帖子消息 testMessage.clearProperties(); testMessage.setStringProperty("RefreshThreadId", content); producer.send(testMessage); } catch (Exception e) { e.printStackTrace(); } } public void init() throws ServletException { // Put your code here try { initCtx = new InitialContext(); envContext = (Context) initCtx.lookup("java:comp/env"); connectionFactory = (ConnectionFactory) envContext.lookup("jms/NormalConnectionFactory"); connection = connectionFactory.createConnection(); jmsSession = connection.createSession(false,Session.AUTO_ACKNOWLEDGE); producer = jmsSession.createProducer((Destination) envContext.lookup("jms/topic/MyTopic")); } catch (NamingException e) { e.printStackTrace(); } catch (JMSException e) { e.printStackTrace(); } } }
package com.flvcd.servlet; import java.io.*; import javax.servlet.*; import javax.servlet.http.*; import javax.naming.*; import javax.jms.*; import org.apache.activemq.ActiveMQConnectionFactory; public class JMSListener extends HttpServlet implements MessageListener{ /** 初始化jms连接,创建topic监听器 */ public void init(ServletConfig config) throws ServletException{ try { InitialContext initCtx = new InitialContext(); Context envContext = (Context) initCtx.lookup("java:comp/env"); ConnectionFactory connectionFactory = (ConnectionFactory) envContext.lookup("jms/FailoverConnectionFactory"); Connection connection = connectionFactory.createConnection(); connection.setClientID("MyClient"); Session jmsSession = connection.createSession(false, Session.AUTO_ACKNOWLEDGE); //普通消息订阅者,无法接收持久消息 //MessageConsumer consumer = jmsSession.createConsumer((Destination) envContext.lookup("jms/topic/MyTopic")); //基于Topic创建持久的消息订阅者,前提:Connection必须指定一个唯一的clientId,当前为MyClient TopicSubscriber consumer = jmsSession.createDurableSubscriber((Topic) envContext.lookup("jms/topic/MyTopic"), "MyClient"); consumer.setMessageListener(this); connection.start(); } catch (NamingException e) { e.printStackTrace(); } catch (JMSException e) { e.printStackTrace(); } } /** 接收消息,做对应处理 */ public void onMessage(Message message) { if (checkText(message, "RefreshArticleId") != null) { String articleId = checkText(message, "RefreshArticleId"); System.out.println("接收刷新文章消息,开始刷新文章ID=" + articleId); } else if (checkText(message, "RefreshThreadId") != null) { String threadId = checkText(message, "RefreshThreadId"); System.out.println("接收刷新论坛帖子消息,开始刷新帖子ID=" + threadId); } else { System.out.println("接收普通消息,不做任何处理!"); } } private static String checkText(Message m, String s) { try { return m.getStringProperty(s); } catch (JMSException e) { e.printStackTrace(System.out); return null; } } }
consumer.setMessageListener(this);
<servlet> <servlet-name>jms-listener</servlet-name> <servlet-class> com.flvcd.servlet.JMSListener</servlet-class> <load-on-startup>1</load-on-startup> </servlet>