Spring与ActiveMQ的整合
来源:互联网 发布:php网站模板源码 编辑:程序博客网 时间:2024/04/29 19:59
1.首先我们需将所需要的依赖写入pom.xml
<dependency><groupId>junit</groupId><artifactId>junit</artifactId><version>4.12</version><scope>test</scope></dependency><!-- spring基础 --><dependency><groupId>org.springframework</groupId><artifactId>spring-context</artifactId><version>4.1.6.RELEASE</version></dependency><dependency><groupId>org.springframework</groupId><artifactId>spring-jms</artifactId><version>4.1.6.RELEASE</version></dependency><dependency><groupId>org.springframework</groupId><artifactId>spring-web</artifactId><version>4.1.6.RELEASE</version></dependency><dependency><groupId>org.springframework</groupId><artifactId>spring-test</artifactId><version>4.1.6.RELEASE</version></dependency><!-- aspectj的依赖 --><dependency><groupId>org.aspectj</groupId><artifactId>aspectjweaver</artifactId><version>1.8.9</version><scope>runtime</scope></dependency><dependency><groupId>javax.annotation</groupId><artifactId>jsr250-api</artifactId><version>1.0</version></dependency><dependency><groupId>org.apache.activemq</groupId><artifactId>activemq-all</artifactId><version>5.9.0</version></dependency>
<!-- spring容器生成配制 --><context-param><param-name>contextConfigLocation</param-name><!-- spring配制文件所在的位置, *表示任何长度的任意字符 --><param-value>classpath:spring*.xml</param-value></context-param><listener><listener-class>org.springframework.web.context.ContextLoaderListener</listener-class></listener>
配置连接工厂
<!-- 配置ConnectionFactory,这里的只是spring用于管理ConnectionFactory的,真正产生到JMS服务器连接的工厂是由JMS服务厂商提供的--><bean id="targetConnectionFactory" class="org.apache.activemq.ActiveMQConnectionFactory"><property name="brokerURL" value="tcp://localhost:61616" /></bean><!-- 真正可以产生Connection的ConnectionFactory,由对应的 JMS服务厂商提供 --><bean id="connectionFactory"class="org.springframework.jms.connection.SingleConnectionFactory"><property name="targetConnectionFactory" ref="targetConnectionFactory" /></bean>
配置消息目的地,即Destination
<!--这个是队列目的地,点对点的 queue --><bean id="queueDestination" class="org.apache.activemq.command.ActiveMQQueue"><constructor-arg><value>queue</value></constructor-arg></bean><!--这个是主题目的地,一对多的 topic --><bean id="topicDestination" class="org.apache.activemq.command.ActiveMQTopic"><constructor-arg value="topic" /></bean>
配置JmsTemplate
<!-- Spring提供的JMS工具类,它可以进行消息发送、接收等 生产者角色--><bean id="jmsTemplate" class="org.springframework.jms.core.JmsTemplate"><!-- 这个connectionFactory对应的是我们定义的Spring提供的那个ConnectionFactory对象 --><property name="connectionFactory" ref="connectionFactory" /></bean>
编写Producer生产者类
package com.yc.services.impl;import javax.jms.Destination;import javax.jms.JMSException;import javax.jms.Message;import javax.jms.Session;import org.springframework.beans.factory.annotation.Autowired;import org.springframework.jms.core.JmsTemplate;import org.springframework.jms.core.MessageCreator;import org.springframework.stereotype.Component;import com.yc.services.ProducerService;@Component("producerService")public class ProducerServiceImpl{@Autowiredprivate JmsTemplate jmsTemplate;//发送消息public void sendMessage(Destination destination, final String message) {System.out.println("生产者发了一个消息:" + message); jmsTemplate.send(destination, new MessageCreator(){ public Message createMessage(Session session) throws JMSException { return session.createTextMessage(message); } });}}到这里我们的生产者其实已经配置完毕,那么完成一个消息的流程我们还需要消费者的存在,接下来我们来实现消费者的代码,这里只是写了接收信息的方法,即是消息监听器,消费者的实现我们依旧交给spring来完成
package com.yc.services.impl;import javax.jms.JMSException;import javax.jms.Message;import javax.jms.MessageListener;import javax.jms.TextMessage;public class ConsumerImpl implements MessageListener{public void onMessage(Message message) {TextMessage content=(TextMessage) message;try {System.out.println("接收到的消息为:"+content.getText());} catch (JMSException e) {e.printStackTrace();}}}
转到spring.xml,配置消息监听器
<!-- 消息监听器 --><bean id="consumerMessageListener" class="com.yc.services.impl.ConsumerImpl" />
配置消息监听容器,即消费者,所必须给的参数有三个,连接工厂,目的地以及处理消息的监听
<!-- 消息监听容器,消费者 --><bean id="jmsContainer" class="org.springframework.jms.listener.DefaultMessageListenerContainer"><!-- 连接工厂 --><property name="connectionFactory" ref="connectionFactory" /><!-- 消息目的地,这里是queue --><property name="destination" ref="queueDestination" /><!-- 消息监听,消费者 --><property name="messageListener" ref="consumerMessageListener" /></bean>
4.OK,至此我们的配置已经可以完成ActiveMQ的基本操作了,我们来用junit测试一下所写的代码
package com.yc.test;import javax.jms.Destination;import org.junit.Test;import org.junit.runner.RunWith;import org.springframework.beans.factory.annotation.Autowired;import org.springframework.beans.factory.annotation.Qualifier;import org.springframework.test.context.ContextConfiguration;import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;import com.yc.services.ProducerService;@RunWith(SpringJUnit4ClassRunner.class)@ContextConfiguration("classpath:spring.xml")public class ProducerServiceImplTest { @Autowired private ProducerServiceImpl producerService; @Autowired @Qualifier("queueDestination") //使得自动注入从bytype变为byname private Destination destination; @Test public void testSend() { for (int i = 0; i < 2; i++) { producerService.sendMessage(destination, "你好,ActiveMQ" ); } } }junit testSend()方法
成功
0 0
- Spring与ActiveMQ的整合
- Spring与ActiveMQ的整合
- activemq与spring的整合
- Spring与ActiveMQ整合
- Spring与ActiveMQ整合
- ActiveMQ 与spring整合
- Spring与ActiveMQ整合
- activemq与spring整合
- ActiveMQ与Spring整合
- Spring整合ActiveMQ与Quartz的实例
- Spring与ActiveMQ(JMS)的整合说明
- ActiveMQ与Spring的整合使用
- activemq使用系列: spring与activemq的整合
- JMS-ActiveMq与Spring整合
- ActiveMQ 与spring整合使用
- ActiveMQ安装与Spring整合
- Spring Boot与ActiveMQ整合
- activeMQ和spring的整合
- ListIterator listIterator():List集合特有的迭代器
- SQL Server 怎样生成序列号(虚拟数字辅助表)
- LeetCode#169 Majority Element
- Android中ViewPgae中的Fragment如何确认当前页面可见的问题
- 嘟嘟说晓波:CSDN学院的晓波老师是何许人也!
- Spring与ActiveMQ的整合
- 我有一个集合,如下,请问,我想判断里面有没有"world"这个元素,如果有,我就添加一个"javaee"元素,请写代码实现。
- 基础总结篇之四:Service完全解析
- Vue 单引号问题
- 数据结构实验之串三:KMP应用
- 正则表达式
- VB程序学习代码记录20160805
- sim900芯片—GPRS模块接收短信应用程序
- 关于空指针NULL、野指针、通用指针