淘淘商城系列——Spring与ActiveMQ的整合及用JmsTemplate发送消息
来源:互联网 发布:淘宝申请定向计划 编辑:程序博客网 时间:2024/06/05 10:36
之前我们一直都是在学习如何使用Junit测试ActiveMQ的发送/接收消息。本文我们将一起学习下ActiveMQ如何与Spring进行整合。
首先,我们要在taotao-manager-service工程的pom.xml文件中添加以下两个依赖(由于我们搭建这个工程的时候就已经添加了对这两个jar的依赖,因此我们不用做这步了)。
<dependency> <groupId>org.springframework</groupId> <artifactId>spring-jms</artifactId></dependency><dependency> <groupId>org.springframework</groupId> <artifactId>spring-context-support</artifactId></dependency>
接着,我们需要在taotao-manager-service工程下新建一个关于ActiveMQ的配置文件,如下图所示。
为方便大家复制,现将applicationContext-activemq.xml配置文件的内容给出。
<beans xmlns="http://www.springframework.org/schema/beans" xmlns:context="http://www.springframework.org/schema/context" xmlns:p="http://www.springframework.org/schema/p" xmlns:aop="http://www.springframework.org/schema/aop" xmlns:tx="http://www.springframework.org/schema/tx" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-4.2.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.2.xsd http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-4.2.xsd http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-4.2.xsd http://www.springframework.org/schema/util http://www.springframework.org/schema/util/spring-util-4.2.xsd"> <!-- 真正可以产生Connection的ConnectionFactory,由对应的JMS服务厂商提供 --> <bean id="targetConnectionFactory" class="org.apache.activemq.ActiveMQConnectionFactory"> <property name="brokerURL" value="tcp://192.168.25.129:61616" /> </bean> <!-- Spring用于管理真正的ConnectionFactory的ConnectionFactory --> <bean id="connectionFactory" class="org.springframework.jms.connection.SingleConnectionFactory"> <!-- 目标ConnectionFactory对应真实的可以产生JMS Connection的ConnectionFactory --> <property name="targetConnectionFactory" ref="targetConnectionFactory" /> </bean> <!-- 配置生产者 --> <!-- Spring提供的JMS工具类,它可以进行消息发送、接收等 --> <bean id="jmsTemplate" class="org.springframework.jms.core.JmsTemplate"> <!-- 这个connectionFactory对应的是我们定义的Spring提供的那个ConnectionFactory对象 --> <property name="connectionFactory" ref="connectionFactory" /> </bean> <!--这个是队列目的地,点对点的 --> <bean id="queueDestination" class="org.apache.activemq.command.ActiveMQQueue"> <constructor-arg> <value>spring-queue</value> </constructor-arg> </bean> <!--这个是主题目的地,一对多的 --> <bean id="topicDestination" class="org.apache.activemq.command.ActiveMQTopic"> <constructor-arg value="spring-topic" /> </bean></beans>
这样我们就把Spring与ActiveMQ整合完毕了,下面我们便一起来测试下使用JmsTemplate来发送消息。
为方便大家复制,现将TestSpringActiveMQ单元测试类的代码给出。
public class TestSpringActiveMQ { @Test public void testQueueProducer() { // 初始化Spring容器 ApplicationContext applicationContext = new ClassPathXmlApplicationContext("classpath:spring/applicationContext-activemq.xml"); // 从容器中获得JMSTemplate对象 JmsTemplate jmsTemplate = applicationContext.getBean(JmsTemplate.class); // 从容器中获得Destination对象 Queue queue = applicationContext.getBean(Queue.class); // 第一个参数:指定发送的目的地 // 第二个参数:消息的构造器对象,就是创建一个消息 jmsTemplate.send(queue, new MessageCreator() { @Override public Message createMessage(Session session) throws JMSException { TextMessage textMessage = session.createTextMessage("使用Spring和ActiveMQ整合发送queue消息"); return textMessage; } }); }}
运行上面的testQueueProducer方法,我们可以在activemq的后台管理系统页面看到我们刚才发送的消息,如下图所示。
阅读全文
0 0
- 淘淘商城系列——Spring与ActiveMQ的整合及用JmsTemplate发送消息
- 学习淘淘商城第六十课(Spring与Activemq的整合及用JmsTemplate发送消息)
- Spring与ActiveMQ整合及用JmsTemplate发送消息
- 淘淘商城系列——ActiveMQ与Spring整合接收消息
- 淘淘商城系列——ActiveMQ发送queue消息
- 淘淘商城系列——spring与freemarker的整合
- 淘淘商城系列——ActiveMQ发送topic消息和接收topic消息
- ActiveMQ——4.Spring-JmsTemplate之发送与接收
- 学习淘淘商城第六十一课(Activemq与Spring整合接收消息)
- 淘淘商城系列——ActiveMQ的介绍
- 淘淘商城系列——ActiveMQ的安装
- ActiveMQ使用spring JmsTemplate发送消息(一)
- ActiveMQ使用spring JmsTemplate发送消息(一)
- activemq的连接池,通过spring的JmsTemplate发送消息到指定的Destination
- activemq的连接池,通过spring的JmsTemplate发送消息到指定的Destination
- ActiveMQ——5.Spring-JmsTemplate之浏览与执行
- 淘淘商城系列——接收Queue消息
- 学习淘淘商城第五十八课(ActiveMQ发送topic消息和接收topic消息)
- FreeMarker的几个重要作用
- Oracle 启动过程
- Android 通过GET请求利用pull解析器获取XML格式数据在ListView控件显示
- 过滤字符串 判断字符 数字判断
- C++/MFC-静态链接库
- 淘淘商城系列——Spring与ActiveMQ的整合及用JmsTemplate发送消息
- Android中读写文件
- 死锁
- Java学习路线图
- String 字符串常量 StringBuffer 字符串变量(线程安全) StringBuilder 字符串变量(非线程安全)
- 超详细注释的基于opencv3的Optical flow(光流点追踪)
- Java中的内部类03-内部类的作用
- QT5音乐播放器
- 136. Single Number