jms activeMQ与spring的集成
来源:互联网 发布:数据总表按字段拆分 编辑:程序博客网 时间:2024/04/30 18:24
这几天刚学习了一下消息队列,一直苦于找不到很好很简单的上手资料,苦找几天的资料,自己终于把这一块搞的差不多理解了,下面是自己的学习小demo,仅供上手,大致理解怎么跑,方便那些和我一样苦苦找寻资料的用户吧,废话不多说,直接上代码:
1、首先引入activeMQ和spring的jar包 ,直接上图
上面的jar包第一个是activeMQ的,还有spring的commos-logging,spring综合包,里面包括了spring-jms,另外还要引入slf4j的两个包 (jar包见管理--文件中的 activeMQ所需jar包 如果不能看的,要么留下联系方式我看到了发给你们,要么网上找也好找 -.- )
2、spring配置文件
1 <?xml version="1.0" encoding="UTF-8"?> 2 3 <beans xmlns="http://www.springframework.org/schema/beans" 4 xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 5 xmlns:context="http://www.springframework.org/schema/context" 6 xsi:schemaLocation="http://www.springframework.org/schema/beans 7 http://www.springframework.org/schema/beans/spring-beans-2.5.xsd 8 http://www.springframework.org/schema/context 9 http://www.springframework.org/schema/context/spring-context-2.5.xsd">10 11 12 <!-- 配置JMS连接工厂 -->13 14 <bean id="connectionFactory" class="org.apache.activemq.spring.ActiveMQConnectionFactory">15 <property name="brokerURL" value="tcp://localhost:61616" />16 </bean>17 18 <!-- 配置JMS模版 -->19 <bean id="jmsTemplate" class="org.springframework.jms.core.JmsTemplate">20 <property name="connectionFactory" ref="connectionFactory" />21 </bean>22 23 <!-- 发送消息的目的地(一个队列) -->24 <bean id="destination" class="org.apache.activemq.command.ActiveMQQueue">25 <!-- 设置消息队列的名字 -->26 <constructor-arg index="0" value="activeMQQueue" />27 </bean>28 </beans>
2、java代码 发送者
1 package com.pis.activeMQ.test; 2 3 import org.junit.Test; 4 import org.springframework.context.ApplicationContext; 5 6 import org.springframework.context.support.ClassPathXmlApplicationContext; 7 8 import org.springframework.jms.core.JmsTemplate; 9 10 import org.springframework.jms.core.MessageCreator;11 12 import javax.jms.Destination;13 14 import javax.jms.JMSException;15 16 import javax.jms.Message;17 18 import javax.jms.Session;19 20 public class MessageSender implements MessageCreator{21 22 //这里用的是单元测试来写的,执行时需要引入junit4的jar包,如果嫌麻烦,直接改成main方法也行23 @Test24 public void send(){25 ApplicationContext ctx = new ClassPathXmlApplicationContext("/spring/spring-jms.xml");26 27 JmsTemplate template = (JmsTemplate) ctx.getBean("jmsTemplate");28 29 Destination destination = (Destination) ctx.getBean("destination");30 31 template.send(destination, this);//此处的this是一个MessageCreator的类型,实质上调用的是MessageCreator的createMessage的方法32 //也可以用内部类来写,会更清晰,就不需要实现MessageCreator33 34 /*35 template.send(destination, new MessageCreator(){36 37 @Override38 public Message createMessage(Session session) throws JMSException {39 return session.createTextMessage("sender发送消息!");40 }41 42 });*/43 System.out.println("发送JMS消息成功");44 }45 46 @Override47 public Message createMessage(Session session) throws JMSException {48 49 return session.createTextMessage("sender发送消息!");50 }51 52 53 54 55 }
3、java代码 消费者
1 package com.pis.activeMQ.test; 2 3 import org.junit.Test; 4 import org.springframework.context.ApplicationContext; 5 6 import org.springframework.context.support.ClassPathXmlApplicationContext; 7 8 import org.springframework.jms.core.JmsTemplate; 9 10 11 12 import javax.jms.Destination; 13 14 import javax.jms.JMSException; 15 16 import javax.jms.TextMessage; 17 18 public class MessageReceiver {19 20 21 //这里用的是单元测试来写的,执行时需要引入junit4的jar包,如果嫌麻烦,直接改成main方法也行22 @Test23 public void Receive() throws JMSException { 24 ApplicationContext ctx = new ClassPathXmlApplicationContext("/spring/spring-jms.xml"); 25 JmsTemplate template = (JmsTemplate) ctx.getBean("jmsTemplate");26 Destination destination = (Destination) ctx.getBean("destination"); 27 while (true) {28 TextMessage msg = (TextMessage) template.receive(destination);29 if (null != msg)30 System.out.println("收到消息内容为: " + msg.getText());31 else32 break;33 34 } 35 } 36 37 38 39 }
启动activeMQ服务,连续运行两次发送者,然后再运行一次接受者,可以从http://localhost:8161/admin/queues.jsp去查看消息队列的消息发送接受情况,如下图所示:
运行MessageSender 两次
查看activeMQ管理界面:可以看到有2条消息
运行MessageReceiver,接受两条消息
再次查看activeMQ管理界面:可以看到2条消息已经接收
OK! 上面就是最简单的activeMQ的配置了,绝对可以跑起来,跑起来的代码才是王道,大家一起学习了!
- jms activeMQ与spring的集成
- jms activeMQ与spring的集成
- (七)jms activeMQ与spring的集成
- jms activeMQ与spring的集成(转载)
- jms activeMQ与spring集成进阶篇
- jms activeMQ与spring集成进阶篇
- JMS 集成Spring 实现ActiveMQ
- Spring JMS和ActiveMQ集成
- jms activeMQ与spring集成进阶篇(转载)
- JMS之ActiveMQ(含与Spring集成使用)
- jms activeMQ与spring集成进阶篇(转载)
- Spring与ActiveMQ(JMS)的整合说明
- JMS-ActiveMq与Spring整合
- ActiveMQ(二)-Spring集成JMS
- spring学习:与jms的集成
- spring学习:与jms的集成
- ActiveMQ(实战三) 与spring的集成
- Spring 与 ActiveMq的集成配置使用
- Kestrel持久化队列服务器
- Suse Linux下内存使用率精细解读
- menuconfig---Unable to find the Ncurses libraries问题解决
- oracle数据库恢复方法及ORA-01991错误的解决
- 手机号码信息的获取
- jms activeMQ与spring的集成
- Kestrel and Storm
- 链表(创建,插入,删除和打印输出)
- CIRCOS教程翻译 4.6——Label format
- 常见错误 集
- MFC 灾难性故障
- 【设计模式】bridge模式
- 两分钟学会在GitHub托管代码
- 剑指offer面试题33把数组排成最小的数,就是确定sort规则就可以了