Spring+ActiveMQ实现消息收发和订阅
来源:互联网 发布:吉林大学ei数据库 编辑:程序博客网 时间:2024/04/19 17:03
1、去apach网站下载ActiveMQ,地址为http://activemq.apache.org/download.html;
ActiveMQ类似tomcat可以独立启动,相当于开了消息服务,包括连接客户端管理、消息存储设置,对于复杂的系统还可以设置双机热备、负载均衡等;
ActiveMQ有个管理界面,可以查看客户端连接情况,消息队列的发送和接收情况等,也可以直接新建消息;
启动bin/activemq.bat即可启动ActiveMQ服务;
conf/activemq.xml是配置文件,可以配置相关性能参数,消息存储形式和大小等;
2、MQ的spring配置
ActimveMQ的spring配置文件如下:
<bean id="connectionFactory" class="org.apache.activemq.ActiveMQConnectionFactory"><!-- property name="brokerURL" value="tcp://127.0.0.1:61616" / --><property name="brokerURL" value="failover://(tcp://127.0.0.1:61616)?initialReconnectDelay=100" /> <property name="useAsyncSend" value="true" /><property name="maxThreadPoolSize" value="50" /></bean><!-- <bean id="pooledConnectionFactory" class="org.apache.activemq.pool.PooledConnectionFactory"><property name="connectionFactory" ref="connectionFactory" /></bean> --><bean id="singleConnectionFactory" class="org.springframework.jms.connection.SingleConnectionFactory"><property name="targetConnectionFactory"><ref local="connectionFactory" /></property></bean><bean id="destinationQueue" class="org.apache.activemq.command.ActiveMQQueue"> <!-- 设置消息队列的名字 --> <constructor-arg index="0" value="myqueue0"/> </bean> <!-- 发送消息的目的地(一个队列) --> <bean id="vehPassTopic" class="org.apache.activemq.command.ActiveMQTopic"> <!-- 设置消息队列的名字 --> <constructor-arg index="0" value="vehPass.messages" /> </bean> <bean id="vehAlarmTopic" class="org.apache.activemq.command.ActiveMQTopic"> <!-- 设置消息队列的名字 --> <constructor-arg index="0" value="vehAlarm.messages" /> </bean> <bean id="jmsTemplate" class="org.springframework.jms.core.JmsTemplate"><!-- <property name="connectionFactory" ref="connectionFactory"/> --><property name="connectionFactory" ref="singleConnectionFactory" /><!-- <property name="defaultDestination" ref="destination"/> --><!-- 区别它采用的模式为false是p2p为true是订阅 --> <!-- <property name="pubSubDomain" value="true" /> --></bean>
注意这行很关键
<property name="brokerURL" value="failover://(tcp://127.0.0.1:61616)?initialReconnectDelay=100" />
是为了保证断线自动重连;如果没有设置,程序与ActiveMQ服务连接后期间出现过网络异常,再程序无法与ActiveMQ正常通讯;
3、测试程序
发送消息程序:
public class MessageSender{protected Logger log = Logger.getLogger(MessageReceiver.class.getName());private static JmsTemplate jmsTemplate = (JmsTemplate)MyApplicationContextUtil.context.getBean("jmsTemplate");private static Destination vehAlarmTopic = (Destination)MyApplicationContextUtil.context.getBean("vehAlarmTopic");private static Destination vehPassTopic = (Destination)MyApplicationContextUtil.context.getBean("vehPassTopic");static MyMessageCreator mc = null;/* * */public static boolean sendLiveMessage(Object msg){boolean isSend = false;mc = new MyMessageCreator(msg);jmsTemplate.send(vehPassTopic, mc);isSend = true;return isSend;}public static boolean sendBKMessage(Object msg){boolean isSend = false;MyMessageCreator mc = new MyMessageCreator(msg);jmsTemplate.send(vehAlarmTopic, mc);isSend = true;return isSend;}public static void main(String args[]) throws Exception {ApplicationContext context = new ClassPathXmlApplicationContext( new String[] { "applicationContext-mq.xml" }); JmsTemplate jmsTemplate = (JmsTemplate) context.getBean("jmsTemplate"); Destination destinationTopic = (Destination) context.getBean("destinationTopic"); for (int i = 1; i < 10; i++) { MyMessageCreator mc = new MyMessageCreator("test");//生成消息 jmsTemplate.send(destinationTopic, mc); Thread.sleep(1000);//1秒后发送下一条消息 } }}接收消息程序:
public class MessageReceiver {protected Logger log = Logger.getLogger(MessageReceiver.class.getName());private static JmsTemplate jmsTemplate = (JmsTemplate)MyApplicationContextUtil.context.getBean("jmsTemplate");private static Destination vehPassTopic = (Destination)MyApplicationContextUtil.context.getBean("vehPassTopic");/** * * @return */public static List<Message> receiveMessage(){List<Message> msgList = null;Message message = null; for (int i = 0; i < 100;) {//每次接收100条记录 message = jmsTemplate.receive(vehPassTopic); if(message != null){ msgList.add(message); /*if (message instanceof TextMessage) { i++; msg = (TextMessage)message; returnMsg = msg.getText(); log.debug("收到消息 :" + msg.getText()); }*/ }else{ break; } }return msgList;}public static void main(String args[]) throws Exception {ApplicationContext context = new ClassPathXmlApplicationContext( new String[] { "applicationContext-mq.xml" }); JmsTemplate jmsTemplate = (JmsTemplate) context.getBean("jmsTemplate"); Destination destinationTopic = (Destination) context.getBean("destinationTopic"); TextMessage msg = null; for (int i = 0; i < 10; i ++) { msg = (TextMessage) jmsTemplate.receive(destinationTopic); System.out.println("收到消息 :" + msg.getText()); Thread.sleep(1000); } }}
消息具体发送和接收情况都可在ActiveMQ的管理界面查看。
管理界面如下:访问地址:http://127.0.0.1:8161/admin/
- Spring+ActiveMQ实现消息收发和订阅
- Spring Boot教程(十四)Spring Boot整合ActiveQ实现消息收发和订阅
- Spring Boot系列(十二)Spring Boot整合ActiveQ实现消息收发和订阅
- ActiveMQ和spring整合,订阅主题和消息消费
- ActiveMQ使用spring JmsTemplate生成和订阅消息(二)
- ActiveMQ发布消息和订阅消息
- JMS模拟ActiveMq代理服务器,实现消息收发
- ActiveMQ + AJAX 实现web端收发消息
- spring整合JMS一同步收发消息(基于ActiveMQ的实现)
- ActiveMQ实战(六)--Spring整合ActiveMQ实现队列和主题发布订阅通信
- activemq 收发消息
- spring+stomp+webSocket+SockJS 实现简单的订阅广播消息和订阅个人消息
- spring整合ActiveMQ订阅模式(对象消息发送)
- Spring+ActiveMQ消息持久化,Topic持久化订阅
- activeMQ消息中间件之点对点与发布订阅模式实现
- activemq订阅模式以及消息时长和确认机制
- ActiveMQ Topic发布订阅消息
- spring整合activeMQ-摘要(订阅者和发布者)
- 正则表达式
- B/S和C/S
- 图片分屏加载的插件
- android 图片 圆角
- 获取浏览器高度宽度信息
- Spring+ActiveMQ实现消息收发和订阅
- 【js学习笔记-060】子类VS组合
- 找工作总结
- android 中Xml里面的id重名问题
- IKExpression
- 怎么解决修改“HTTP响应拆分漏洞”?怎么修改ASP,down.asp
- CentOS修改DNS、IP地址、网关
- java 从零开始,学习笔记之基础入门<类_方法_构造器>(三)
- SVN