ActiveMQ订阅模式持久化实现
实现步骤:
1、配置发送xml,applicationContext-send.xml
- <?xml version="1.0" encoding="UTF-8"?>
- <beans xmlns="http://www.springframework.org/schema/beans"
- xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:context="http://www.springframework.org/schema/context"
- xsi:schemaLocation="http://www.springframework.org/schema/beans
- http://www.springframework.org/schema/beans/spring-beans-2.5.xsd
- http://www.springframework.org/schema/context
- http://www.springframework.org/schema/context/spring-context-2.5.xsd">
- <context:property-placeholder location="classpath:/properties/jms.properties" />
- <!-- 配置JMS连接工厂 -->
- <bean id="myConnectionFactory"
- class="org.springframework.jms.connection.CachingConnectionFactory">
- <!-- Session缓存数量 -->
- <property name="sessionCacheSize" value="10" />
- <property name="targetConnectionFactory">
- <bean class="org.apache.activemq.ActiveMQConnectionFactory">
- <!-- MQ地址 -->
- <property name="brokerURL" value="${brokerUrl}" />
- <!-- 是否异步发送 -->
- <property name="useAsyncSend" value="true" />
- </bean>
- </property>
- </bean>
- <!-- 发送消息的目的地(一个主题) -->
- <bean id="myDestination" class="org.apache.activemq.command.ActiveMQTopic">
- <!-- 设置消息主题的名字 -->
- <constructor-arg index="0" value="${send.name}" />
- </bean>
- <!-- 配置JMS模版 -->
- <bean id="jmsTemplate" class="org.springframework.jms.core.JmsTemplate">
- <property name="connectionFactory" ref="myConnectionFactory" />
- <property name="defaultDestination" ref="myDestination" />
- <!-- 订阅发布模式 -->
- <property name="pubSubDomain" value="true" />
- <property name="receiveTimeout" value="10000" />
- </bean>
- </beans>
2、编写发送java,ActiveMQsender.java
- package com.by.activeMQ;
- import javax.jms.JMSException;
- import javax.jms.Message;
- import javax.jms.Session;
- import javax.jms.TextMessage;
- import org.springframework.context.ApplicationContext;
- import org.springframework.context.support.ClassPathXmlApplicationContext;
- import org.springframework.jms.core.JmsTemplate;
- import org.springframework.jms.core.MessageCreator;
- public class ActiveMQsender {
- public static void main(String[] args) {
- @SuppressWarnings("resource")
- ApplicationContext ctx = new ClassPathXmlApplicationContext(
- "ApplicationContext/applicationContext-send.xml");
- JmsTemplate jmsTemplate = (JmsTemplate) ctx.getBean("jmsTemplate");
- jmsTemplate.send(new MessageCreator() {
- public Message createMessage(Session session) throws JMSException {
- TextMessage msg = session.createTextMessage();
- // 设置消息属性
- msg.setStringProperty("mood", "happy");
- // 设置消息内容
- msg.setText("Hello World!");
- return msg;
- }
- });
- System.out.println("send end");
- }
- }
3、配置接收xml,applicationContext-receive.xml
- <?xml version="1.0" encoding="UTF-8"?>
- <beans xmlns="http://www.springframework.org/schema/beans"
- xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:context="http://www.springframework.org/schema/context"
- xsi:schemaLocation="http://www.springframework.org/schema/beans
- http://www.springframework.org/schema/beans/spring-beans-2.5.xsd
- http://www.springframework.org/schema/context
- http://www.springframework.org/schema/context/spring-context-2.5.xsd">
- <context:property-placeholder location="classpath:/properties/jms.properties" />
- <!-- 第一个接收者 -->
- <!-- 配置JMS连接工厂 -->
- <bean id="myConnectionFactory"
- class="org.springframework.jms.connection.CachingConnectionFactory">
- <!-- Session缓存数量 -->
- <property name="sessionCacheSize" value="10" />
- <!-- 接收者ID -->
- <property name="clientId" value="${topic.clientId}" />
- <property name="targetConnectionFactory">
- <bean class="org.apache.activemq.ActiveMQConnectionFactory">
- <!-- MQ地址 -->
- <property name="brokerURL" value="${brokerUrl}" />
- </bean>
- </property>
- </bean>
- <!-- 发送消息的目的地(一个主题) -->
- <bean id="myDestination" class="org.apache.activemq.command.ActiveMQTopic">
- <!-- 设置消息主题的名字 -->
- <constructor-arg index="0" value="${topic.name}" />
- </bean>
- <!-- 生产消息配置 (自己定义)-->
- <bean id="myTopicConsumer" class="com.by.activeMQ.ActiveMQreceiver" />
- <!-- 消息监听器 -->
- <bean id="myTopicListener"
- class="org.springframework.jms.listener.adapter.MessageListenerAdapter">
- <constructor-arg ref="myTopicConsumer" />
- <!-- 接收消息的方法名称 -->
- <property name="defaultListenerMethod" value="receive" />
- <!-- 不进行消息转换 -->
- <property name="messageConverter"><null/></property>
- </bean>
- <!-- 消息监听容器 -->
- <bean id="myListenerContainer"
- class="org.springframework.jms.listener.DefaultMessageListenerContainer">
- <property name="connectionFactory" ref="myConnectionFactory" />
- <!-- 发布订阅模式 -->
- <property name="pubSubDomain" value="true"/>
- <!-- 消息持久化 -->
- <property name="subscriptionDurable" value="true"/>
- <property name="receiveTimeout" value="10"/>
- <!-- 接收者ID -->
- <property name="clientId" value="${topic.clientId}" />
- <property name="durableSubscriptionName" value="${topic.clientId}"/>
- <property name="destination" ref="myDestination" />
- <property name="messageListener" ref="myTopicListener" />
- </bean>
- <!-- 第二个接收者 -->
- <!-- 配置JMS连接工厂 -->
- <bean id="myConnectionFactory2"
- class="org.springframework.jms.connection.CachingConnectionFactory">
- <!-- Session缓存数量 -->
- <property name="sessionCacheSize" value="10" />
- <!-- 接收者ID -->
- <property name="clientId" value="${topic2.clientId}" />
- <property name="targetConnectionFactory">
- <bean class="org.apache.activemq.ActiveMQConnectionFactory">
- <!-- MQ地址 -->
- <property name="brokerURL" value="${brokerUrl}" />
- </bean>
- </property>
- </bean>
- <!-- 发送消息的目的地(一个主题) -->
- <bean id="myDestination2" class="org.apache.activemq.command.ActiveMQTopic">
- <!-- 设置消息主题的名字 -->
- <constructor-arg index="0" value="${topic2.name}" />
- </bean>
- <!-- 生产消息配置 (自己定义)-->
- <bean id="myTopicConsumer2" class="com.by.activeMQ.ActiveMQreceiver2" />
- <!-- 消息监听器 -->
- <bean id="myTopicListener2"
- class="org.springframework.jms.listener.adapter.MessageListenerAdapter">
- <constructor-arg ref="myTopicConsumer2" />
- <!-- 接收消息的方法名称 -->
- <property name="defaultListenerMethod" value="receive" />
- <!-- 不进行消息转换 -->
- <property name="messageConverter"><null/></property>
- </bean>
- <!-- 消息监听容器 -->
- <bean id="myListenerContainer2"
- class="org.springframework.jms.listener.DefaultMessageListenerContainer">
- <property name="connectionFactory" ref="myConnectionFactory2" />
- <!-- 发布订阅模式 -->
- <property name="pubSubDomain" value="true"/>
- <!-- 消息持久化 -->
- <property name="subscriptionDurable" value="true"/>
- <property name="receiveTimeout" value="10"/>
- <!-- 接收者ID -->
- <property name="clientId" value="${topic2.clientId}" />
- <property name="durableSubscriptionName" value="${topic2.clientId}"/>
- <property name="destination" ref="myDestination2" />
- <property name="messageListener" ref="myTopicListener2" />
- </bean>
- </beans>
4、编写接收java,ActiveMQreceiver.java
- package com.by.activeMQ;
- import javax.jms.JMSException;
- import javax.jms.TextMessage;
- import org.springframework.jms.JmsException;
- public class ActiveMQreceiver {
- public void receive(TextMessage message) throws JmsException, JMSException {
- String info = "this is receiver, "
- + " mood is " + message.getStringProperty("mood") + ","
- + "say " + message.getText();
- System.out.println(info);
- }
- }
5、编写另一个接收java,ActiveMQreceiver.java
- package com.by.activeMQ;
- import javax.jms.JMSException;
- import javax.jms.TextMessage;
- import org.springframework.jms.JmsException;
- public class ActiveMQreceiver2 {
- public void receive(TextMessage message) throws JmsException, JMSException {
- String info = "this is receiver2,"
- + " mood is " + message.getStringProperty("mood") + ","
- + "say " + message.getText();
- System.out.println(info);
- }
- }
6、编写一个main,开启接收监听,openReceive.java
- package com.by.activeMQ;
- import org.springframework.context.ApplicationContext;
- import org.springframework.context.support.ClassPathXmlApplicationContext;
- public class openReceive {
- public static void main(String[] args) {
- @SuppressWarnings({ "unused", "resource" })
- ApplicationContext ctx = new ClassPathXmlApplicationContext("ApplicationContext/applicationContext-receive.xml");
- while(true) {
- }
- }
- }
7、编写一个配置文件,jms.properties
- #send
- send.name=Topic_Mood
- #receive
- topic.name=Topic_Mood
- topic.clientId=client_LiLei
- topic2.name=Topic_Mood
- topic2.clientId=client_HanMei
- #url
- brokerUrl=failover:(tcp://10.0.0.232:61616)?initialReconnectDelay=1000
8、pom里面添加activeMQ的依赖
- <dependency>
- <groupId>org.apache.activemq</groupId>
- <artifactId>activemq-pool</artifactId>
- <version>5.11.1</version>
- </dependency>
- <dependency>
- <groupId>org.apache.commons</groupId>
- <artifactId>commons-pool2</artifactId>
- <version>2.3</version>
- </dependency>
- <dependency>
- <groupId>org.springframework</groupId>
- <artifactId>spring-jms</artifactId>
- <version>4.0.0.RELEASE</version>
- </dependency>
- <dependency>
- <groupId>org.apache.activemq</groupId>
- <artifactId>activemq-all</artifactId>
- <version>5.11.1</version>
- </dependency>
耶,运行就ok了。
发送消息的输出是这样的:
- 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
- 2016-08-05 11:27:19 [ main:31 ] - [ INFO ] Loading XML bean definitions from class path resource [ApplicationContext/applicationContext-send.xml]
- 2016-08-05 11:27:19 [ main:187 ] - [ INFO ] Loading properties file from class path resource [properties/jms.properties]
- 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}
- 2016-08-05 11:27:19 [ ActiveMQ Task-1:467 ] - [ INFO ] Successfully connected to tcp://10.0.0.232:61616
- send end
接收消息的输出是这样的:
- 2016-08-05 11:28:04 [ ActiveMQ Task-1:490 ] - [ INFO ] Successfully connected to tcp://10.0.0.232:61616
- 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}
- 2016-08-05 11:28:04 [ ActiveMQ Task-1:504 ] - [ INFO ] Successfully connected to tcp://10.0.0.232:61616
- 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}
- this is receiver2, mood is happy,say Hello World!
- this is receiver, mood is happy,say Hello World!
配置另一个接收者就是,把第一个接收者的配置复制,然后添加个2,再把接收类复制,添加个2,就搞定了。这种方式也适用于mongodb啊这种配置。在一个工程里面操作两个mongodb数据库。
ActiveMQ订阅模式持久化实现的更多相关文章
- ACtiveMQ中间件-发布订阅模式
前言:ActiveMQ学习心得 1.MQ是什么 MQ全称为Message Queue, 消息队列(MQ)是一种应用程序对应用程序的通信方法.应用程序通过读写出入队列的消息(针对应用程序的数据)来通信, ...
- ActiveMQ的p2p模式与发布订阅模式
1.消息中间件:采用异步通讯防止,支持点对点以及发布订阅模式,可以解决高并发问题 传统调用接口,可能发生阻塞,重复提交,超时等等问题,可以利用消息中间件发送异步通讯请求 ...
- ActiveMQ发布订阅模式
ActiveMQ的另一种模式就SUB/HUB即发布订阅模式,是SUB/hub就是一拖N的USB分线器的意思.意思就是一个来源分到N个出口.还是上节的例子,当一个订单产生后,后台N个系统需要联动,但有一 ...
- ActiveMQ发布订阅模式(转)
ActiveMQ的另一种模式就SUB/HUB即发布订阅模式,是SUB/hub就是一拖N的USB分线器的意思.意思就是一个来源分到N个出口.还是上节的例子,当一个订单产生后,后台N个系统需要联动,但有一 ...
- ActiveMQ (二)—发布订阅模式
ActiveMQ的另一种模式就SUB/HUB即发布订阅模式,是SUB/hub就是一拖N的USB分线器的意思.意思就是一个来源分到N个出口.还是上节的例子,当一个订单产生后,后台N个系统需要联动,但有一 ...
- ActiveMQ发布订阅模式 转发 https://www.cnblogs.com/madyina/p/4127144.html
ActiveMQ的另一种模式就SUB/HUB即发布订阅模式,是SUB/hub就是一拖N的USB分线器的意思.意思就是一个来源分到N个出口.还是上节的例子,当一个订单产生后,后台N个系统需要联动,但有一 ...
- SpringBoot2.0之整合ActiveMQ(发布订阅模式)
发布订阅模式与前面的点对点模式很类似,简直一毛一样 注意:发布订阅模式 先启动消费者 公用pom: <project xmlns="http://maven.apache.org/PO ...
- ActiveMQ入门系列三:发布/订阅模式
在上一篇<ActiveMQ入门系列二:入门代码实例(点对点模式)>中提到了ActiveMQ中的两种模式:点对点模式(PTP)和发布/订阅模式(Pub & Sub),详细介绍了点对点 ...
- redis实现消息队列&发布/订阅模式使用
在项目中用到了redis作为缓存,再学习了ActiveMq之后想着用redis实现简单的消息队列,下面做记录. Redis的列表类型键可以用来实现队列,并且支持阻塞式读取,可以很容易的实现一个高性 ...
随机推荐
- CentOS 7.4 下面安装 jdk 10 的一点总结
CentOS 7.4 下面安装 jdk 10 的一点总结 一.前期工作 1.检验系统原版本 [root@zck ~]# java -version java version "1.7.0_& ...
- ubuntu要安装新软件,已有deb安装包
如果ubuntu要安装新软件,已有deb安装包(例如:iptux.deb),但是无法登录到桌面环境.那该怎么安装?答案是:使用dpkg命令.dpkg命令常用格式如下:sudo dpkg -I iptu ...
- Aras Innovator DB备份与还原
错误信息 确认到该问题是因为孤立帐号的问题,在解决孤立帐号之前,可以通过语句查看,另外,还原了DB后,系统不会自动创建原来的登陆帐号的,需要手动新增登陆帐号 #查看孤立帐号列表exec sp_chan ...
- 【转载】AsyncTask源码分析
原文地址:https://github.com/white37/AndroidSdkSourceAnalysis/blob/master/article/AsyncTask%E5%92%8CAsync ...
- django-BBS(2)
昨天设计了数据库和数据表,今天来进行页面前端的设计, 1.首先去bootstarp上,下载相应的模板和配置文件,添加到对应的位置 2.在templates中添加许多许多的html页面 如下 并 ...
- 树形DP题目集合
[树形DP](https://cn.vjudge.net/contest/123963#overview) #include<cstdio> #include<string> ...
- 最大子数组(LintCode)
最大子数组 给定一个整数数组,找到一个具有最大和的子数组,返回其最大和. 样例 给出数组[−2,2,−3,4,−1,2,1,−5,3],符合要求的子数组为[4,−1,2,1],其最大和为6 注意 子数 ...
- 27、Flask实战第27天:cms后台登录
cms后台登录界面 后台登录页面,我们不用自己写,只需要去Bootstrap中文网去找一个模板改一下就行 这里使用的模板是:https://v3.bootcss.com/examples/signin ...
- 【POJ 2409】 Let it Bead(置换、burnside引理)
Let it Bead "Let it Bead" company is located upstairs at 700 Cannery Row in Monterey, CA. ...
- Windows下安装Memcached服务及安装PHP的Memcached扩展
Memcached是一个自由开源的,高性能,分布式内存对象缓存系统. Memcached是一种基于内存的key-value存储,用来存储小块的任意数据(字符串.对象).这些数据可以是数据库调用.API ...