ActiveMQ简单使用(二):整合Spring
来源:互联网 发布:数据字典模型作用 编辑:程序博客网 时间:2024/06/06 00:54
ActiveMQ简单使用(一):未整合Spring情况下的介绍使用
上面一篇介绍了关于ActiveMQ的基本概述以及在未整合Spring框架的时候进行的原生态的书写.但是作为实际的开发人员,一般都会整合相关的框架进行集成的开发.此篇就是关于整合Spring进行相关开发.
与Spring进行相关整合,无非就是将相关对象的创建等交由Spring进行管理.因此相关的步骤也就几步:
1.导入相关jar包
2.Producer和Customer相关配置文件的书写
我们以Maven为例来进行相关的测试代码书写.
1.导入jar包:
我们使用JMS进行相关的整合,因此需要导入jms的相关jar包(以下依赖jar包仅为ActiveMQ相较于其他所需的)
<dependency><groupId>org.springframework</groupId><artifactId>spring-jms</artifactId></dependency><dependency><groupId>org.springframework</groupId><artifactId>spring-context-support</artifactId></dependency>2.配置ActiveMQ整合Spring
进行整合的时候,我们需要注意几处配置:
1.我们要产生一个到JMS服务器链接的ConnectionFactory,这个ConnectionFactory和Spring提供的不是同一个,这个是JMS服务商提供的,我们需要将此ConnectionFactory注入到Spring的ConnectionFactory中
<!-- 真正可以产生Connection的ConnectionFactory,由对应的 JMS服务厂商提供 --><bean id="targetConnectionFactory" class="org.apache.activemq.ActiveMQConnectionFactory"><property name="brokerURL" value="tcp://IP:61616" /></bean>
2.将步骤一创建的ConnectionFactory注入到Spring中
<!-- Spring用于管理真正的ConnectionFactory的ConnectionFactory --><bean id="connectionFactory"class="org.springframework.jms.connection.SingleConnectionFactory"><!-- 目标ConnectionFactory对应真实的可以产生JMS Connection的ConnectionFactory --><property name="targetConnectionFactory" ref="targetConnectionFactory" /></bean>注意,在这里我们使用的是SingleConnectionFactory这个类,在Spring中提供给我们的还有CachingConnectionFactory.这两个类各有不同,大家可以自行查阅,这里使用的是SingleConnectionFactory这个类
3.配置生产者
<!-- 配置生产者 --><!-- Spring提供的JMS工具类,它可以进行消息发送、接收等 --><bean id="jmsTemplate" class="org.springframework.jms.core.JmsTemplate"><!-- 这个connectionFactory对应的是我们定义的Spring提供的那个ConnectionFactory对象 --><property name="connectionFactory" ref="connectionFactory" /></bean>
4.配置消息传递类型,是点对点还是发布订阅模式
<!--这个是队列目的地,点对点的 --><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>
5.接收消息,配置监听器
<!-- 接收消息 --><!-- 配置监听器 --><bean id="myMessageListener" class="com.monkey.search.listener.MyMessageListener" /><!-- 消息监听容器 --><bean class="org.springframework.jms.listener.DefaultMessageListenerContainer"><property name="connectionFactory" ref="connectionFactory" /><property name="destination" ref="queueDestination" /><property name="messageListener" ref="myMessageListener" /></bean>
以上配置文件就全部配置完毕了.完整的配置文件如下:
<?xml version="1.0" encoding="UTF-8"?><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.xsdhttp://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.2.xsdhttp://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.xsdhttp://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.154.100: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> --><!-- 接收消息 --><!-- 配置监听器 --><bean id="myMessageListener" class="com.monkey.search.listener.MyMessageListener" /><!-- 消息监听容器 --><bean class="org.springframework.jms.listener.DefaultMessageListenerContainer"><property name="connectionFactory" ref="connectionFactory" /><property name="destination" ref="queueDestination" /><property name="messageListener" ref="myMessageListener" /></bean></beans>
配置文件书写好了,接下来就是JAVA的代码Demo:
import javax.jms.JMSException;import javax.jms.Message;import javax.jms.Queue;import javax.jms.Session;import javax.jms.TextMessage;import org.junit.Test;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 TestSpringMQ {/** * 使用Spring整合 */@Testpublic void testQueueProducer() throws Exception{//1.初始化Spring容器ApplicationContext applicationContext = new ClassPathXmlApplicationContext("classpath:spring/applicationContext-activemq.xml");//2.从容器中获取JMSTemplate对象JmsTemplate jmsTemplate = applicationContext.getBean(JmsTemplate.class);//3.从容器中获取Destination对象Queue query = applicationContext.getBean(Queue.class);//使用jms对象发送消息jmsTemplate.send(query, new MessageCreator() {@Overridepublic Message createMessage(Session session) throws JMSException {TextMessage textMessage = session.createTextMessage("这是第一次与Spring进行整合的activemq");return textMessage;}});}}
import org.junit.Test;import org.springframework.context.ApplicationContext;import org.springframework.context.support.ClassPathXmlApplicationContext;public class TestSpringQueue {@Testpublic void testQueueConsumer() throws Exception{ApplicationContext context = new ClassPathXmlApplicationContext("classpath:spring/applicationContext-activemq.xml");System.in.read();}}
以上关于ActiveMQ的基本使用就介绍完毕.
ActiveMQ简单使用(一):未整合Spring情况下的介绍使用
阅读全文
1 0
- ActiveMQ简单使用(二):整合Spring
- ActiveMQ的入门案例以及整合Spring的简单使用
- ActiveMQ 与spring整合使用
- ActiveMQ简单使用介绍(一):未整合Spring情况下的介绍使用
- ActiveMQ与Spring整合:(1)基本使用
- ActiveMQ与Spring的整合使用
- activemq使用系列: spring与activemq的整合
- ActiveMQ(四):Spring ActiveMQ 整合
- ActiveMQ和spring整合
- Spring整合ActiveMQ
- SPRING+ACTIVEMQ+TOMCAT整合
- Spring整合activeMQ
- ActiveMQ、Spring整合学习
- activeMq整合spring
- ActiveMQ、Spring整合学习
- spring activemq quartz 整合
- SPRING JMS 整合ACTIVEMQ
- ActiveMQ整合Spring
- selenium---unittest框架测试用例函数执行顺序 优先级
- Redis-Service.Stack.Redis对Redis基本数据类型String,List,Hash,Set,ZSet的操作
- JSTL语言整合JS脚本(JSTL控制JS脚本;JSTL控制在页面中的JS脚本语句)
- [spark论文翻译] RDD:基于内存的集群计算容错抽象
- Linux之eval
- ActiveMQ简单使用(二):整合Spring
- MP4的几个概念——神文,看懂这篇就够了
- 关于莫名其妙的400和500错误
- 6.2-全栈Java笔记:异常处理机制的分类
- iOS自动化测试之smallapple 使用翻译
- 使用CA自签名证书搭建HTTPS网站
- 使用config.gradle统一管理项目的依赖库
- ios端ijkplayer编译
- IP地址无分类编址