本文介绍一对一、一对多、持久化、非持久化消息配置方式

一、创建项目

导入jar

二、创建MQ.xml

    <!-- 配置JMS连接工厂 --><bean id="connectionFactory" class="org.apache.activemq.ActiveMQConnectionFactory"><property name="brokerURL" value="failover:(tcp://192.168.1.168:61616)" /></bean>

View Code

集群MQ时value="failover:(tcp://192.168.1.87:61616, tcp://192.168.1.87:61616,tcp://192.168.1.87:61616)

三、队列queue模式(一对一模式)

  此模式是一对一的,每条消息只能被一个人使用,类似QQ私聊,其他人看不到消息

1.监听模式

  当有消息发出时,会自动接收

①在上面创建的MQ.xml配置文件中添加

    <!-- 定义消息队列(Queue),监听一个新的队列,queue2 --><bean id="queueDestination2" class="org.apache.activemq.command.ActiveMQQueue"><!-- 设置消息队列的名字 --><constructor-arg><value>queue2</value></constructor-arg></bean><!-- 配置JMS模板(Queue),Spring提供的JMS工具类,它发送、接收消息。 --><bean id="jmsTemplate" class="org.springframework.jms.core.JmsTemplate"><property name="connectionFactory" ref="connectionFactory" /><property name="defaultDestination" ref="queueDestination2" /><property name="receiveTimeout" value="10000" /></bean><!--queue消息生产者 --><bean id="producerService" class="com.sh.test.Jms_send"><property name="jmsTemplate" ref="jmsTemplate"></property></bean><!-- 配置消息队列监听者(Queue),代码下面给出,只有一个onMessage方法 --><bean id="queueMessageListener" class="com.sh.test.Jms_jie_auto" /><!-- 消息监听容器(Queue),配置连接工厂,监听的队列是queue2,监听器是上面定义的监听器 --><bean id="jmsContainer"class="org.springframework.jms.listener.DefaultMessageListenerContainer"><property name="connectionFactory" ref="connectionFactory" /><property name="destination" ref="queueDestination2" /><property name="messageListener" ref="queueMessageListener" /></bean>

View Code

“queueMessageListener”这个class需在项目中写,实例下面有

②创建一个类Jms_jie_auto.java,添加接收消息代码

package com.sh.test;import javax.jms.JMSException;
import javax.jms.Message;
import javax.jms.MessageListener;
import javax.jms.TextMessage;public class Jms_jie_auto implements MessageListener {//当收到消息时,自动调用该方法。public void onMessage(Message message) {TextMessage tm = (TextMessage) message;try {System.out.println("ConsumerMessageListener收到了文本消息:\t"+ tm.getText());} catch (JMSException e) {e.printStackTrace();}}}

View Code

③创建一个类Jms_send.java添加发送消息代码

package com.sh.test;import javax.jms.Destination;
import javax.jms.JMSException;
import javax.jms.Message;
import javax.jms.Session;
import javax.jms.TextMessage;import org.springframework.jms.core.JmsTemplate;
import org.springframework.jms.core.MessageCreator;public class Jms_send implements ProducerService{private JmsTemplate jmsTemplate;public void setJmsTemplate(JmsTemplate jmsTemplate) {this.jmsTemplate = jmsTemplate;}/*** 向指定队列发送消息*/public void sendMessage(Destination destination, final String msg) {System.out.println("向队列" + destination.toString() + "发送了消息------------" + msg);jmsTemplate.send(destination, new MessageCreator() {public Message createMessage(Session session) throws JMSException {return session.createTextMessage(msg);}});}/*** 向默认队列发送消息*/public void sendMessage(final String msg) {String destination =  jmsTemplate.getDefaultDestination().toString();System.out.println("向队列" +destination+ "发送了消息------------" + msg);jmsTemplate.send(new MessageCreator() {public Message createMessage(Session session) throws JMSException {return session.createTextMessage(msg);}});}public void sendMessage(Destination destination, final String msg, final Destination response) {System.out.println("ProducerService向队列" + destination + "发送了消息:\t" + msg);jmsTemplate.send(destination, new MessageCreator() {public Message createMessage(Session session) throws JMSException {TextMessage textMessage = session.createTextMessage(msg);textMessage.setJMSReplyTo(response);return textMessage;}});}}

View Code

④创建ProducerService.java,发送消息实体类

package com.sh.test;import javax.jms.Destination;public interface ProducerService {/*** 发消息,向默认的 destination* * @param msg String 消息内容*/public void sendMessage(String msg);/*** 发消息,向指定的 destination* * @param destination 目的地* @param msg String 消息内容*/public void sendMessage(Destination destination, String msg);/*** 发消息,向指定的 destination* * @param destination 目的地* @param msg String 消息内容*//*** 向指定的destination发送消息,消费者接受消息后,把回复的消息写到response队列* * @param destination 目的地* @param msg String 消息内容* @param response 回复消息的队列*/public void sendMessage(Destination destination, String msg, Destination response);}

View Code

⑤创建Jms_test.java,发送消息测试方法

package com.sh.test;import javax.jms.Destination;
import javax.jms.JMSException;
import javax.jms.Message;
import javax.jms.Session;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.jms.core.JmsTemplate;
import org.springframework.jms.core.MessageCreator;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.servlet.ModelAndView;@Controller
public class Jms_test {/*** 队列名queue2-监听模式队列*/@Autowiredprivate Destination queueDestination2;/*** 队列消息生产者*/@Autowired@Qualifier("producerService")private ProducerService producer;/*** 测试生产者向queue1发送消息*/@RequestMapping(value="/shengchanzhe",method=RequestMethod.GET)public ModelAndView testProduce(HttpServletRequest request, HttpServletResponse response) {String msg = "Hello world!";producer.sendMessage(queueDestination2, msg+":auto");//监听模式队列,发送消息后在jms_jie_auto中自动出发事件return null;}}

View Code

执行结果

2.非监听模式

此模式当有消息进入指定队列时,需调用方法接收消息

①在上面创建的MQ.xml配置文件中添加

  注意:如果是在上面配置的基础上添加,只需添加下面代码中的queueDestination和consumerService

<!-- 定义消息队列(Queue) --><bean id="queueDestination" class="org.apache.activemq.command.ActiveMQQueue"><!-- 设置消息队列的名字 --><constructor-arg><value>queue1</value></constructor-arg></bean><!-- 配置JMS模板(Queue),Spring提供的JMS工具类,它发送、接收消息。 --><bean id="jmsTemplate" class="org.springframework.jms.core.JmsTemplate"><property name="connectionFactory" ref="connectionFactory" /><property name="defaultDestination" ref="queueDestination" /><property name="receiveTimeout" value="10000" /></bean><!--queue消息生产者 --><bean id="producerService" class="com.sh.test.Jms_send"><property name="jmsTemplate" ref="jmsTemplate"></property></bean><!--queue消息消费者 --><bean id="consumerService" class="com.sh.test.Jms_jie_notauto"><property name="jmsTemplate" ref="jmsTemplate"></property></bean>

View Code

②添加Jms_jie_notauto.java,接收消息代码

package com.sh.test;
import javax.jms.Destination;import javax.jms.JMSException;
import javax.jms.TextMessage;import org.springframework.jms.core.JmsTemplate;
/*** 接收jms消息,非监听模式* @author Administrator**/
public class Jms_jie_notauto implements ConsumerService {private JmsTemplate jmsTemplate;public void setJmsTemplate(JmsTemplate jmsTemplate) {this.jmsTemplate = jmsTemplate;}/*** 接受消息*/public void receive(Destination destination) {TextMessage tm = (TextMessage) jmsTemplate.receive(destination);try {System.out.println("从队列" + destination.toString() + "收到了消息:\t"+ tm.getText());} catch (JMSException e) {e.printStackTrace();}}}

View Code

③添加ConsumerService.java,消费消息的类

package com.sh.test;import javax.jms.Destination;public interface ConsumerService {public void receive(Destination queueDestination);
}

View Code

④发送消息测试方法,在上面Jms_test.java 中添加

    /*** 测试生产者向queue1发送消息*/@RequestMapping(value="/shengchanzhe",method=RequestMethod.GET)public ModelAndView testProduce(HttpServletRequest request, HttpServletResponse response) {String msg = "Hello world!";producer.sendMessage(queueDestination, msg); //非监听模式队列,发送消息后需调用testConsume()方法接收return null;}

View Code

⑤接收消息测试方法,在上面Jms_test.java 中添加,分别执行shengchanzhe,fjt_jieshouzhe,即可看到结果

    /*** 队列消息接收者*/@Autowired@Qualifier("consumerService")private ConsumerService consumer;/*** 队列名queue1-非监听模式队列*/@Autowiredprivate Destination queueDestination;/*** 非监听模式,测试消费者从queue1接受消息*/@RequestMapping(value="/fjt_jieshouzhe",method=RequestMethod.GET)public ModelAndView testConsume(HttpServletRequest request, HttpServletResponse response) {consumer.receive(queueDestination);return null;}

View Code

四、订阅topic模式(一对多)

  此模式是一对多的,每条消息能被多个人使用,类似QQ群聊

①在上面创建的MQ.xml配置文件中添加

<!-- 定义消息主题(Topic) --><bean id="topicDestination" class="org.apache.activemq.command.ActiveMQTopic"><constructor-arg><value>topic_name</value></constructor-arg></bean><!-- 配置JMS模板(Topic),pubSubDomain="true"--><bean id="topicJmsTemplate" class="org.springframework.jms.core.JmsTemplate"><property name="connectionFactory" ref="connectionFactory" /><property name="defaultDestination" ref="topicDestination" /><property name="pubSubDomain" value="true" /><!-- 此项关了就变成了队列模式 --><property name="receiveTimeout" value="10000" /></bean><!--topic消息发布者 --><bean id="topicProvider" class="com.sh.test.Jms_topic_send"><property name="topicJmsTemplate" ref="topicJmsTemplate"></property></bean><!-- 消息主题监听者 和 主题监听容器 可以配置多个,即多个订阅者 --><!-- 消息主题监听者(Topic) --><bean id="topicMessageListener" class="com.sh.test.Jms_topic_jie" /><!-- 主题监听容器 (Topic) --><bean id="topicJmsContainer"class="org.springframework.jms.listener.DefaultMessageListenerContainer"><property name="connectionFactory" ref="connectionFactory" /><property name="destination" ref="topicDestination" /><property name="messageListener" ref="topicMessageListener" /></bean>

View Code

②添加Jms_topic_jie.java 接收信息代码

package com.sh.test;
import javax.jms.JMSException;
import javax.jms.Message;
import javax.jms.MessageListener;
import javax.jms.TextMessage;/*** 订阅者监听端,代码和队列监听一样,需要把这个类配置到xml配置到订阅配置中* @author Administrator**/
public class Jms_topic_jie implements MessageListener {public void onMessage(Message message) {TextMessage tm = (TextMessage) message;try {System.out.println("TopicMessageListener \t" + tm.getText());} catch (JMSException e) {e.printStackTrace();}}}

View Code

③添加Jms_topic_send.java,发送代码

package com.sh.test;
import javax.jms.Destination;
import javax.jms.JMSException;
import javax.jms.Message;
import javax.jms.Session;import org.springframework.jms.core.JmsTemplate;
import org.springframework.jms.core.MessageCreator;/*** 发布订阅消息* @author Administrator**/
public class Jms_topic_send {private JmsTemplate topicJmsTemplate;/*** 向指定的topic发布消息* * @param topic* @param msg*/public void publish(final Destination topic, final String msg) {topicJmsTemplate.send(topic, new MessageCreator() {public Message createMessage(Session session) throws JMSException {System.out.println("topic name 是" + topic.toString()+ ",发布消息内容为:\t" + msg);return session.createTextMessage(msg);}});}public void setTopicJmsTemplate(JmsTemplate topicJmsTemplate) {this.topicJmsTemplate = topicJmsTemplate;}}

View Code

④发送消息测试方法,在上面Jms_test.java 中添加

/*** 订阅队列 topic_name*/@Autowired@Qualifier("topicDestination")private Destination topic;/*** 订阅消息发布者*/@Autowiredprivate Jms_topic_send topicProvider;/*** 发布订阅消息,发布后自动在jms_topic_jie中接收*/@RequestMapping(value="/sendDy",method=RequestMethod.GET)public ModelAndView sendDingYue(HttpServletRequest request, HttpServletResponse response){for(int i=0;i<11;i++){topicProvider.publish(topic, "订阅发布"+i);}return null;}

View Code

以上配置是非持久化订阅,既发送发在接收方服务器关闭情况下发送消息,接收方启动后是无法收到的,下面是持久化订阅

替换上面xml中对应配置即可

   <!-- 配置JMS模板(Topic),pubSubDomain="true"--><bean id="topicJmsTemplate" class="org.springframework.jms.core.JmsTemplate"><property name="connectionFactory" ref="connectionFactory" /><property name="defaultDestination" ref="topicDestination" /><property name="pubSubDomain" value="true" /><!-- 此项关了就变成了队列模式 --><property name="receiveTimeout" value="10000" /><!--设置持久化:1,非持久化;2,持久化--><property name="deliveryMode" value="2" /><!-- deliveryMode, priority, timeToLive 的开关,要生效,必须配置为true,默认false --><property name="explicitQosEnabled" value="true" /> </bean><!-- 主题监听容器 (Topic) --><bean id="topicJmsContainer"class="org.springframework.jms.listener.DefaultMessageListenerContainer"><property name="connectionFactory" ref="connectionFactory" /><property name="destination" ref="topicDestination" /><property name="messageListener" ref="topicMessageListener" /><!-- 持久化订阅 start --><property name="subscriptionDurable" value="true" /> <property name="pubSubDomain" value="true" /> <property name="clientId" value="clientId_001" /> <!-- id唯一 --> <property name="durableSubscriptionName" value="clientId_001" /><!-- 持久化订阅 end --> </bean>

View Code

有问题(BUG)请反馈,谢谢

转载于:https://www.cnblogs.com/buggou/p/8124843.html

ActiveMQ配置文档相关推荐

  1. Java日志框架-Logback手册中文版以及官方配置文档教程

    Logback手册中文版:(链接: https://pan.baidu.com/s/1bpMyasR 密码: 6u5c),虽然版本有点旧,但是大体意思差不多,先用中文版了解个大概,然后一切最新的配置以 ...

  2. nagios配置文档

    nagios配置文档 关于nagios  Nagios是一款用于系统和网络监控的应用程序.它可以在你设定的条件下对主机和服务进行监控,在状态变差和 变好的时候给出告警信息. Nagios最初被设计为在 ...

  3. Nginx配置文档具体解释

    Nginx的配置文档具体解释.在这儿做个总结,以便以后使用的时间查看. 下面大部分自己整理.部分来自參考 #设置用户 #user  nobody; #启动进程数(一般和server的CPU同样) #能 ...

  4. 我的Debian 8.0 (jessie)配置文档

    2019独角兽企业重金招聘Python工程师标准>>> 我的Debian  8.0 (jessie)配置文档 1,添加软件源  su到root用户 vi  /etc/apt/sou ...

  5. Postfix配置文档

    环境:RedHat Linux+Postfix+Cyrus-sasl+Dovecot+Stunnel 在安装Linux时,如果选择全部安装的话,Postfix与Dovecot以及Stunnel将会自动 ...

  6. Linux keypad 设备树,SC7731客户配置文档.pdf

    SC7731客户配置文档 SC7731 客户配置文档 2014-07-09 目录 02 Add your texts here SC7731 0101 03 SC7731 软件架构简介 02 Pinm ...

  7. Java代码规范、格式化和checkstyle检查配置文档

    为便于规范各位开发人员代码.提高代码质量,研发中心需要启动代码评审机制.为了加快代码评审的速度,减少不必要的时间,可以加入一些代码评审的静态检查工具,另外需要为研发中心配置统一的编码模板和代码格式化模 ...

  8. rsync 服务与配置文档

    rsync 服务与配置文档 1 某项目rsync配置文档(关键字已处理) $ cat /etc/rsyncd.conf #全局配置 log file = /var/log/rsyncd.log     ...

  9. 陆续放出各种安装及配置文档

    这几天根据拓扑图, 陆续放出各种安装及配置文档.敬请期待.... 转载于:https://blog.51cto.com/shubao/763318

最新文章

  1. AI录音笔一战成名!搜狗以语言AI为核心重点突破多点开花
  2. mysql实习生笔试题_2014阿里实习生面试题MySQL如何实现索引的
  3. zabbix添加对web页面url的状态监控
  4. ioctl中的ifconf ifreg 结构
  5. python shutil模块用法实例分析_Python shutil模块用法实例分析
  6. WPF效果第一百七十八篇ItemsControl旋转
  7. Spring IOC容器-注解的方式【更简化】
  8. Fiddler工具杂记-存储特定的数据包保存到文件(CustomRules.js基本使用)
  9. ×××须避开的高薪杀手
  10. eclipse集群tomcat
  11. javascript网页自动填表_javascript实现自动填写表单实例简析
  12. 赫伯特·西蒙前半生泡妞打架,后半生拿图灵奖、诺贝尔奖,成人工智能大神
  13. JAVA后端调用微信支付“统一下单”接口实现微信二维码扫码支付
  14. Mapgis技巧篇1
  15. 高级软件工程(2022春)课程总结
  16. RS485,uart串口加瑞芬68协议
  17. Vue Vant点赞效果
  18. 综述笔记-多无人机多目标任务分配1
  19. S-function入门及案例详解(2)——S-function基本案例介绍
  20. npm install 报错 - node篇

热门文章

  1. 解决:Google代码achartengine曲线代码报错问题(转)
  2. 经典算法系列三----堆排序
  3. java类与对象(属性,方法)的使用
  4. WinForm 修改App.config不起作用(但是调试没有异常)
  5. .net framework4与其client profile版本的区别
  6. hdu4825 字典树 XOR
  7. 【转】通过身边小事解释机器学习是什么?
  8. Openwrt之移动硬盘ext3/ext4格式化工具
  9. 银行科技管理工作优化提升之我见
  10. DX10 Shadow Volumn Sample Code的Bug修正