实现步骤:
1、配置发送xml,applicationContext-send.xml

  1. <?xml version="1.0" encoding="UTF-8"?>
  2. <beans xmlns="http://www.springframework.org/schema/beans"
  3. xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:context="http://www.springframework.org/schema/context"
  4. xsi:schemaLocation="http://www.springframework.org/schema/beans
  5. http://www.springframework.org/schema/beans/spring-beans-2.5.xsd
  6. http://www.springframework.org/schema/context
  7. http://www.springframework.org/schema/context/spring-context-2.5.xsd">
  8. <context:property-placeholder location="classpath:/properties/jms.properties" />
  9. <!-- 配置JMS连接工厂 -->
  10. <bean id="myConnectionFactory"
  11. class="org.springframework.jms.connection.CachingConnectionFactory">
  12. <!-- Session缓存数量 -->
  13. <property name="sessionCacheSize" value="10" />
  14. <property name="targetConnectionFactory">
  15. <bean class="org.apache.activemq.ActiveMQConnectionFactory">
  16. <!-- MQ地址 -->
  17. <property name="brokerURL" value="${brokerUrl}" />
  18. <!-- 是否异步发送 -->
  19. <property name="useAsyncSend" value="true" />
  20. </bean>
  21. </property>
  22. </bean>
  23. <!-- 发送消息的目的地(一个主题) -->
  24. <bean id="myDestination" class="org.apache.activemq.command.ActiveMQTopic">
  25. <!-- 设置消息主题的名字 -->
  26. <constructor-arg index="0" value="${send.name}" />
  27. </bean>
  28. <!-- 配置JMS模版 -->
  29. <bean id="jmsTemplate" class="org.springframework.jms.core.JmsTemplate">
  30. <property name="connectionFactory" ref="myConnectionFactory" />
  31. <property name="defaultDestination" ref="myDestination" />
  32. <!-- 订阅发布模式 -->
  33. <property name="pubSubDomain" value="true" />
  34. <property name="receiveTimeout" value="10000" />
  35. </bean>
  36. </beans>

2、编写发送java,ActiveMQsender.java

  1. package com.by.activeMQ;
  2. import javax.jms.JMSException;
  3. import javax.jms.Message;
  4. import javax.jms.Session;
  5. import javax.jms.TextMessage;
  6. import org.springframework.context.ApplicationContext;
  7. import org.springframework.context.support.ClassPathXmlApplicationContext;
  8. import org.springframework.jms.core.JmsTemplate;
  9. import org.springframework.jms.core.MessageCreator;
  10. public class ActiveMQsender {
  11. public static void main(String[] args) {
  12. @SuppressWarnings("resource")
  13. ApplicationContext ctx = new ClassPathXmlApplicationContext(
  14. "ApplicationContext/applicationContext-send.xml");
  15. JmsTemplate jmsTemplate = (JmsTemplate) ctx.getBean("jmsTemplate");
  16. jmsTemplate.send(new MessageCreator() {
  17. public Message createMessage(Session session) throws JMSException {
  18. TextMessage msg = session.createTextMessage();
  19. // 设置消息属性
  20. msg.setStringProperty("mood", "happy");
  21. // 设置消息内容
  22. msg.setText("Hello World!");
  23. return msg;
  24. }
  25. });
  26. System.out.println("send end");
  27. }
  28. }

3、配置接收xml,applicationContext-receive.xml

  1. <?xml version="1.0" encoding="UTF-8"?>
  2. <beans xmlns="http://www.springframework.org/schema/beans"
  3. xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:context="http://www.springframework.org/schema/context"
  4. xsi:schemaLocation="http://www.springframework.org/schema/beans
  5. http://www.springframework.org/schema/beans/spring-beans-2.5.xsd
  6. http://www.springframework.org/schema/context
  7. http://www.springframework.org/schema/context/spring-context-2.5.xsd">
  8. <context:property-placeholder location="classpath:/properties/jms.properties" />
  9. <!-- 第一个接收者 -->
  10. <!-- 配置JMS连接工厂 -->
  11. <bean id="myConnectionFactory"
  12. class="org.springframework.jms.connection.CachingConnectionFactory">
  13. <!-- Session缓存数量 -->
  14. <property name="sessionCacheSize" value="10" />
  15. <!-- 接收者ID -->
  16. <property name="clientId" value="${topic.clientId}" />
  17. <property name="targetConnectionFactory">
  18. <bean class="org.apache.activemq.ActiveMQConnectionFactory">
  19. <!-- MQ地址 -->
  20. <property name="brokerURL" value="${brokerUrl}" />
  21. </bean>
  22. </property>
  23. </bean>
  24. <!-- 发送消息的目的地(一个主题) -->
  25. <bean id="myDestination" class="org.apache.activemq.command.ActiveMQTopic">
  26. <!-- 设置消息主题的名字 -->
  27. <constructor-arg index="0" value="${topic.name}" />
  28. </bean>
  29. <!-- 生产消息配置 (自己定义)-->
  30. <bean id="myTopicConsumer" class="com.by.activeMQ.ActiveMQreceiver" />
  31. <!-- 消息监听器 -->
  32. <bean id="myTopicListener"
  33. class="org.springframework.jms.listener.adapter.MessageListenerAdapter">
  34. <constructor-arg ref="myTopicConsumer" />
  35. <!-- 接收消息的方法名称 -->
  36. <property name="defaultListenerMethod" value="receive" />
  37. <!-- 不进行消息转换 -->
  38. <property name="messageConverter"><null/></property>
  39. </bean>
  40. <!-- 消息监听容器 -->
  41. <bean id="myListenerContainer"
  42. class="org.springframework.jms.listener.DefaultMessageListenerContainer">
  43. <property name="connectionFactory" ref="myConnectionFactory" />
  44. <!-- 发布订阅模式 -->
  45. <property name="pubSubDomain" value="true"/>
  46. <!-- 消息持久化 -->
  47. <property name="subscriptionDurable" value="true"/>
  48. <property name="receiveTimeout" value="10"/>
  49. <!-- 接收者ID -->
  50. <property name="clientId" value="${topic.clientId}" />
  51. <property name="durableSubscriptionName" value="${topic.clientId}"/>
  52. <property name="destination" ref="myDestination" />
  53. <property name="messageListener" ref="myTopicListener" />
  54. </bean>
  55. <!-- 第二个接收者 -->
  56. <!-- 配置JMS连接工厂 -->
  57. <bean id="myConnectionFactory2"
  58. class="org.springframework.jms.connection.CachingConnectionFactory">
  59. <!-- Session缓存数量 -->
  60. <property name="sessionCacheSize" value="10" />
  61. <!-- 接收者ID -->
  62. <property name="clientId" value="${topic2.clientId}" />
  63. <property name="targetConnectionFactory">
  64. <bean class="org.apache.activemq.ActiveMQConnectionFactory">
  65. <!-- MQ地址 -->
  66. <property name="brokerURL" value="${brokerUrl}" />
  67. </bean>
  68. </property>
  69. </bean>
  70. <!-- 发送消息的目的地(一个主题) -->
  71. <bean id="myDestination2" class="org.apache.activemq.command.ActiveMQTopic">
  72. <!-- 设置消息主题的名字 -->
  73. <constructor-arg index="0" value="${topic2.name}" />
  74. </bean>
  75. <!-- 生产消息配置 (自己定义)-->
  76. <bean id="myTopicConsumer2" class="com.by.activeMQ.ActiveMQreceiver2" />
  77. <!-- 消息监听器 -->
  78. <bean id="myTopicListener2"
  79. class="org.springframework.jms.listener.adapter.MessageListenerAdapter">
  80. <constructor-arg ref="myTopicConsumer2" />
  81. <!-- 接收消息的方法名称 -->
  82. <property name="defaultListenerMethod" value="receive" />
  83. <!-- 不进行消息转换 -->
  84. <property name="messageConverter"><null/></property>
  85. </bean>
  86. <!-- 消息监听容器 -->
  87. <bean id="myListenerContainer2"
  88. class="org.springframework.jms.listener.DefaultMessageListenerContainer">
  89. <property name="connectionFactory" ref="myConnectionFactory2" />
  90. <!-- 发布订阅模式 -->
  91. <property name="pubSubDomain" value="true"/>
  92. <!-- 消息持久化 -->
  93. <property name="subscriptionDurable" value="true"/>
  94. <property name="receiveTimeout" value="10"/>
  95. <!-- 接收者ID -->
  96. <property name="clientId" value="${topic2.clientId}" />
  97. <property name="durableSubscriptionName" value="${topic2.clientId}"/>
  98. <property name="destination" ref="myDestination2" />
  99. <property name="messageListener" ref="myTopicListener2" />
  100. </bean>
  101. </beans>

4、编写接收java,ActiveMQreceiver.java

  1. package com.by.activeMQ;
  2. import javax.jms.JMSException;
  3. import javax.jms.TextMessage;
  4. import org.springframework.jms.JmsException;
  5. public class ActiveMQreceiver {
  6. public void receive(TextMessage message) throws JmsException, JMSException {
  7. String info = "this is receiver, "
  8. + " mood is " + message.getStringProperty("mood") + ","
  9. + "say " + message.getText();
  10. System.out.println(info);
  11. }
  12. }

5、编写另一个接收java,ActiveMQreceiver.java

  1. package com.by.activeMQ;
  2. import javax.jms.JMSException;
  3. import javax.jms.TextMessage;
  4. import org.springframework.jms.JmsException;
  5. public class ActiveMQreceiver2 {
  6. public void receive(TextMessage message) throws JmsException, JMSException {
  7. String info = "this is receiver2,"
  8. + " mood is " + message.getStringProperty("mood") + ","
  9. + "say " + message.getText();
  10. System.out.println(info);
  11. }
  12. }

6、编写一个main,开启接收监听,openReceive.java

  1. package com.by.activeMQ;
  2. import org.springframework.context.ApplicationContext;
  3. import org.springframework.context.support.ClassPathXmlApplicationContext;
  4. public class openReceive {
  5. public static void main(String[] args) {
  6. @SuppressWarnings({ "unused", "resource" })
  7. ApplicationContext ctx = new ClassPathXmlApplicationContext("ApplicationContext/applicationContext-receive.xml");
  8. while(true) {
  9. }
  10. }
  11. }

7、编写一个配置文件,jms.properties

  1. #send
  2. send.name=Topic_Mood
  3. #receive
  4. topic.name=Topic_Mood
  5. topic.clientId=client_LiLei
  6. topic2.name=Topic_Mood
  7. topic2.clientId=client_HanMei
  8. #url
  9. brokerUrl=failover:(tcp://10.0.0.232:61616)?initialReconnectDelay=1000

8、pom里面添加activeMQ的依赖

  1. <dependency>
  2. <groupId>org.apache.activemq</groupId>
  3. <artifactId>activemq-pool</artifactId>
  4. <version>5.11.1</version>
  5. </dependency>
  6. <dependency>
  7. <groupId>org.apache.commons</groupId>
  8. <artifactId>commons-pool2</artifactId>
  9. <version>2.3</version>
  10. </dependency>
  11. <dependency>
  12. <groupId>org.springframework</groupId>
  13. <artifactId>spring-jms</artifactId>
  14. <version>4.0.0.RELEASE</version>
  15. </dependency>
  16. <dependency>
  17. <groupId>org.apache.activemq</groupId>
  18. <artifactId>activemq-all</artifactId>
  19. <version>5.11.1</version>
  20. </dependency>

耶,运行就ok了。
发送消息的输出是这样的:

  1. 2016-08-05 11:27:19 [ main:0 ] - [ INFO ] Refreshing org.springframework.context.support.ClassPathXmlApplicationContext@16011db4: startup date [Fri Aug 05 11:27:19 CST 2016]; root of context hierarchy
  2. 2016-08-05 11:27:19 [ main:31 ] - [ INFO ] Loading XML bean definitions from class path resource [ApplicationContext/applicationContext-send.xml]
  3. 2016-08-05 11:27:19 [ main:187 ] - [ INFO ] Loading properties file from class path resource [properties/jms.properties]
  4. 2016-08-05 11:27:19 [ main:392 ] - [ INFO ] Established shared JMS Connection: ActiveMQConnection {id=ID:MDG42V9PSU28IKA-60542-1470367639797-1:1,clientId=null,started=false}
  5. 2016-08-05 11:27:19 [ ActiveMQ Task-1:467 ] - [ INFO ] Successfully connected to tcp://10.0.0.232:61616
  6. send end

接收消息的输出是这样的:

  1. 2016-08-05 11:28:04 [ ActiveMQ Task-1:490 ] - [ INFO ] Successfully connected to tcp://10.0.0.232:61616
  2. 2016-08-05 11:28:04 [ main:498 ] - [ INFO ] Established shared JMS Connection: ActiveMQConnection {id=ID:MDG42V9PSU28IKA-60544-1470367684739-1:1,clientId=client_LiLei,started=false}
  3. 2016-08-05 11:28:04 [ ActiveMQ Task-1:504 ] - [ INFO ] Successfully connected to tcp://10.0.0.232:61616
  4. 2016-08-05 11:28:04 [ main:509 ] - [ INFO ] Established shared JMS Connection: ActiveMQConnection {id=ID:MDG42V9PSU28IKA-60544-1470367684739-3:1,clientId=client_HanMei,started=false}
  5. this is receiver2, mood is happy,say Hello World!
  6. this is receiver,  mood is happy,say Hello World!

配置另一个接收者就是,把第一个接收者的配置复制,然后添加个2,再把接收类复制,添加个2,就搞定了。这种方式也适用于mongodb啊这种配置。在一个工程里面操作两个mongodb数据库。

ActiveMQ订阅模式持久化实现的更多相关文章

  1. ACtiveMQ中间件-发布订阅模式

    前言:ActiveMQ学习心得 1.MQ是什么 MQ全称为Message Queue, 消息队列(MQ)是一种应用程序对应用程序的通信方法.应用程序通过读写出入队列的消息(针对应用程序的数据)来通信, ...

  2. ActiveMQ的p2p模式与发布订阅模式

    1.消息中间件:采用异步通讯防止,支持点对点以及发布订阅模式,可以解决高并发问题        传统调用接口,可能发生阻塞,重复提交,超时等等问题,可以利用消息中间件发送异步通讯请求          ...

  3. ActiveMQ发布订阅模式

    ActiveMQ的另一种模式就SUB/HUB即发布订阅模式,是SUB/hub就是一拖N的USB分线器的意思.意思就是一个来源分到N个出口.还是上节的例子,当一个订单产生后,后台N个系统需要联动,但有一 ...

  4. ActiveMQ发布订阅模式(转)

    ActiveMQ的另一种模式就SUB/HUB即发布订阅模式,是SUB/hub就是一拖N的USB分线器的意思.意思就是一个来源分到N个出口.还是上节的例子,当一个订单产生后,后台N个系统需要联动,但有一 ...

  5. ActiveMQ (二)—发布订阅模式

    ActiveMQ的另一种模式就SUB/HUB即发布订阅模式,是SUB/hub就是一拖N的USB分线器的意思.意思就是一个来源分到N个出口.还是上节的例子,当一个订单产生后,后台N个系统需要联动,但有一 ...

  6. ActiveMQ发布订阅模式 转发 https://www.cnblogs.com/madyina/p/4127144.html

    ActiveMQ的另一种模式就SUB/HUB即发布订阅模式,是SUB/hub就是一拖N的USB分线器的意思.意思就是一个来源分到N个出口.还是上节的例子,当一个订单产生后,后台N个系统需要联动,但有一 ...

  7. SpringBoot2.0之整合ActiveMQ(发布订阅模式)

    发布订阅模式与前面的点对点模式很类似,简直一毛一样 注意:发布订阅模式 先启动消费者 公用pom: <project xmlns="http://maven.apache.org/PO ...

  8. ActiveMQ入门系列三:发布/订阅模式

    在上一篇<ActiveMQ入门系列二:入门代码实例(点对点模式)>中提到了ActiveMQ中的两种模式:点对点模式(PTP)和发布/订阅模式(Pub & Sub),详细介绍了点对点 ...

  9. redis实现消息队列&发布/订阅模式使用

    在项目中用到了redis作为缓存,再学习了ActiveMq之后想着用redis实现简单的消息队列,下面做记录.   Redis的列表类型键可以用来实现队列,并且支持阻塞式读取,可以很容易的实现一个高性 ...

随机推荐

  1. ng配置301及反向代理示例

    server { listen 80; server_name fpb.com; return 301 http://www.fpb.com$request_uri; } server { liste ...

  2. C++使用第三方静态库的方法

    以jasoncpp为例 首先在github下载最新的的jasoncpp包并解压 找到类似makefiles的文件编译,一般是打开sln文件使用vs编译(生成解决方案),在build文件中可以找到对应的 ...

  3. vs2005 QT4.7.1编译 详细

    http://blog.csdn.net/debugconsole/article/details/8230683 网上一搜有QT+2005编译的很多文章,但是都不详细,很多都编不过,特别的在conf ...

  4. HDU 6467.简单数学题-数学题 (“字节跳动-文远知行杯”广东工业大学第十四届程序设计竞赛)

    简单数学题 Time Limit: 4000/2000 MS (Java/Others)    Memory Limit: 65536/65536 K (Java/Others)Total Submi ...

  5. 从InitialContext获取数据源

    概述 本文介绍如何从javax.naming.InitialContext中获取web容器配置的数据源. 在web开发中,常见的获取数据源的方式是把数据源定义为spring的bean,其他类通过spr ...

  6. Bzoj3837 [Pa2013]Filary(随机化)

    题面 权限题 题解 这题有一个很好的性质,就是一定有$k>\frac n2$.接着考虑怎么做. 我们随机选取一个数$x$,然后将所有数与它作差,那么只需要找出$k$个差值使得他们的最大公因数大于 ...

  7. Python开发基础-Day2-流程控制、数字和字符串处理

    流程控制 条件判断 if单分支:当一个“条件”成立时执行相应的操作. 语法结构: if 条件: command 流程图: 示例:如果3大于2,那么输出字符串"very good" ...

  8. [NOIP2017]时间复杂度(模拟)

    sscanf读入数字,getline(cin,string)读一整行,其余暴力模拟即可. #include<cstdio> #include<string> #include& ...

  9. [LOJ2541]猎人杀

    好题== 先转化一下,每个人被杀死后给他打标记,以后随机杀人时选到有标记的人就继续随机,这样并不会改变每个人被杀死的概率 直接算很难算,考虑容斥,我们枚举$S$表示在$1$死后才死的人至少在集合$S$ ...

  10. 新浪微博(t.sina)简单授权代码及API测试

    http://www.eoeandroid.com/thread-53701-1-1.html package mobson.weiboku.view; import java.util.ArrayL ...