博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
JMS-mq-发布/订阅
阅读量:5058 次
发布时间:2019-06-12

本文共 5915 字,大约阅读时间需要 19 分钟。

1,Tomcat配置

  

2,发布/订阅

发布:http://localhost:8080/Mq/Publisher

Publisher send:ActiveMQTextMessage {commandId = 0, responseRequired = false, messageId = ID:WWH-PC-64244-1488884593844-1:3:1:1:1, originalDestination = null, originalTransactionId = null, producerId = null, destination = topic://TestTopic, transactionId = null, expiration = 0, timestamp = 1488884667978, arrival = 0, brokerInTime = 0, brokerOutTime = 0, correlationId = null, replyTo = null, persistent = false, type = null, priority = 4, groupID = null, groupSequence = 0, targetConsumerId = null, compressed = false, userID = null, content = null, marshalledProperties = null, dataStructure = null, redeliveryCounter = 0, size = 0, properties = null, readOnlyProperties = false, readOnlyBody = false, droppable = false, jmsXGroupFirstForConsumer = false, text = 2017?3?7?}

订阅:http://localhost:8080/Mq/Subscriber

Subscriber:ActiveMQTextMessage {commandId = 5, responseRequired = false, messageId = ID:WWH-PC-64244-1488884593844-1:3:1:1:1, originalDestination = null, originalTransactionId = null, producerId = ID:WWH-PC-64244-1488884593844-1:3:1:1, destination = topic://TestTopic, transactionId = null, expiration = 0, timestamp = 1488884667978, arrival = 0, brokerInTime = 1488884667978, brokerOutTime = 1488884667982, correlationId = null, replyTo = null, persistent = false, type = null, priority = 4, groupID = null, groupSequence = 0, targetConsumerId = null, compressed = false, userID = null, content = org.apache.activemq.util.ByteSequence@2dbd83ba, marshalledProperties = null, dataStructure = null, redeliveryCounter = 0, size = 0, properties = null, readOnlyProperties = true, readOnlyBody = true, droppable = false, jmsXGroupFirstForConsumer = false, text = 2017?3?7?}

 

3,代码

【1】消息发布

package com.ma.publish;import java.io.IOException;import java.io.PrintWriter;import javax.jms.DeliveryMode;import javax.jms.Session;import javax.jms.TextMessage;import javax.jms.Topic;import javax.jms.TopicConnection;import javax.jms.TopicConnectionFactory;import javax.jms.TopicPublisher;import javax.jms.TopicSession;import javax.naming.InitialContext;import javax.servlet.ServletException;import javax.servlet.annotation.WebServlet;import javax.servlet.http.HttpServlet;import javax.servlet.http.HttpServletRequest;import javax.servlet.http.HttpServletResponse;@WebServlet("/Publisher")public class Publisher extends HttpServlet{        /**     * 消息-订阅/发布     */    private static final long serialVersionUID = -4470119218802259551L;    public Publisher(){        super();    }    @Override    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {                PrintWriter out = resp.getWriter();        try{                        InitialContext context = new InitialContext();            Topic topic = (Topic) context.lookup("java:comp/env/topic/topic0");            TopicConnectionFactory tConnectionFactory = (TopicConnectionFactory) context.lookup("java:comp/env/topic/connectionFactory");                        TopicConnection tConnection = tConnectionFactory.createTopicConnection();            TopicSession topicSession = tConnection.createTopicSession(false, Session.AUTO_ACKNOWLEDGE);            TopicPublisher tpPublisher = topicSession.createPublisher(topic);            tpPublisher.setDeliveryMode(DeliveryMode.NON_PERSISTENT);                        TextMessage txMessage = topicSession.createTextMessage();            txMessage.setText("2017年3月7日");            tpPublisher.publish(txMessage);                        out.write("Publisher send:" +txMessage);            tConnection.close();                    }catch(Throwable e){            e.printStackTrace();        }                    }    @Override    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {    }        }

 

【2】消息订阅

package com.ma.publish;import java.io.IOException;import java.io.PrintWriter;import javax.jms.Session;import javax.jms.TextMessage;import javax.jms.Topic;import javax.jms.TopicConnection;import javax.jms.TopicConnectionFactory;import javax.jms.TopicSession;import javax.jms.TopicSubscriber;import javax.naming.InitialContext;import javax.servlet.ServletException;import javax.servlet.annotation.WebServlet;import javax.servlet.http.HttpServlet;import javax.servlet.http.HttpServletRequest;import javax.servlet.http.HttpServletResponse;@WebServlet("/Subscriber")public class Subscriber extends HttpServlet {    /**     * 消息-发布/订阅     */    private static final long serialVersionUID = 6058649540492572496L;    public Subscriber(){        super();    }    @Override    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {                PrintWriter out = resp.getWriter();        try{                        InitialContext context = new InitialContext();            Topic topic = (Topic) context.lookup("java:comp/env/topic/topic0");            TopicConnectionFactory tConnectionFactory = (TopicConnectionFactory) context.lookup("java:comp/env/topic/connectionFactory");                        TopicConnection tConnection = tConnectionFactory.createTopicConnection();            TopicSession topicSession = tConnection.createTopicSession(false, Session.AUTO_ACKNOWLEDGE);            TopicSubscriber tpSubscriber = topicSession.createSubscriber(topic);                        tConnection.start();            TextMessage txMessage = (TextMessage) tpSubscriber.receive();                        out.write("Subscriber:" +txMessage);            tConnection.close();                    }catch(Throwable e){            e.printStackTrace();        }                    }    @Override    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {            }        }

 

转载于:https://www.cnblogs.com/wanhua-wu/p/6516237.html

你可能感兴趣的文章
shell统计特征数量
查看>>
复习文件操作
查看>>
C#Hashtable与Dictionary性能
查看>>
10个让你忘记 Flash 的 HTML5 应用演示
查看>>
8个Python面试必考的题目,小编也被坑过 ToT
查看>>
SQL Server 使用作业设置定时任务之一(转载)
查看>>
centos 图形界面和命令行界面切换(转载)
查看>>
Maven启用代理访问
查看>>
Primary definition
查看>>
第二阶段冲刺-01
查看>>
BZOJ1045 HAOI2008 糖果传递
查看>>
发送请求时params和data的区别
查看>>
JavaScript 克隆数组
查看>>
eggs
查看>>
一步步学习微软InfoPath2010和SP2010--第七章节--从SP列表和业务数据连接接收数据(4)--外部项目选取器和业务数据连接...
查看>>
如何增强你的SharePoint 团队网站首页
查看>>
FZU 1914 Funny Positive Sequence(线性算法)
查看>>
oracle 报错ORA-12514: TNS:listener does not currently know of service requested in connec
查看>>
基于grunt构建的前端集成开发环境
查看>>
MySQL服务读取参数文件my.cnf的规律研究探索
查看>>