您的位置:首页技术文章
文章详情页

ActiveMQ整合Spring入门用法解析

【字号: 日期:2023-08-05 15:34:13浏览:3作者:猪猪

一.ActiveMQ整合Spring基础

ActiveMQ和Spring的整合,其实是把activemq的一些对象交给spring来管理,比如连接工厂,queue,top等等

二.依赖

除了activemq本身提供的jar包外,还需要两个spring整合activemq的jar:

<dependency> <groupId>org.springframework</groupId> <artifactId>spring-jms</artifactId> </dependency> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-context-support</artifactId> </dependency> <!-- activemq --> <dependency> <groupId>org.apache.activemq</groupId> <artifactId>activemq-all</artifactId> </dependency>

三.在spring配置文件中,配置activemq相关的对象

》生产者需要配置的对象:activemq原生连接工厂,spring包装过的工厂,JmsTemplate(生产者),queue,topic

<!-- 配置activemq的连接工厂 --><bean name='activemqConnectionFactory' class='org.apache.activemq.ActiveMQConnectionFactory'> <constructor-arg index='0' value='tcp://192.168.xx.xxx:61616'></constructor-arg></bean><!-- 配置spring包装的连接工厂 --><bean name='connectionFactory' class='org.springframework.jms.connection.SingleConnectionFactory'> <!-- 设置activemq的连接工厂 --> <property name='targetConnectionFactory' ref='activemqConnectionFactory'></property></bean><!-- 配置生产者对象:jmstemplate --><bean name='jmsTemplate' class='org.springframework.jms.core.JmsTemplate'> <!-- 设置工厂 --> <!-- <constructor-arg name='connectionFactory' ref='connectionFactory'></constructor-arg> --> <property name='connectionFactory' ref='connectionFactory'></property></bean><!-- 配置queue对象 --><bean name='queueDestination' class='org.apache.activemq.command.ActiveMQQueue'> <constructor-arg name='name' value='spring-active-queue'></constructor-arg></bean><!-- 配置topic对象 --><bean name='topicDestination' class='org.apache.activemq.command.ActiveMQTopic'> <constructor-arg name='name' value='spring-active-topic'></constructor-arg></bean>

测试代码:

//测试生产者发生queue @Test public void testJMSTemplateByQueue() throws Exception {//创建spring容器 ApplicationContext ioc = new ClassPathXmlApplicationContext('classpath:spring/applicationContext-activemq.xml');//获取jmstemplate JmsTemplate jmsTemplate = ioc.getBean(JmsTemplate.class);//获取目的对象 Destination queue = (Destination) ioc.getBean('queueDestination');//发送消息,并设置目的对象和消息 jmsTemplate.send(queue,new MessageCreator() { public Message createMessage(Session session) throws JMSException {TextMessage message = session.createTextMessage();message.setText('hello spring activemq');return message; } });

小结:生产者是需要在特定的时间发送指定的消息内容,因此是需要书写代码的

》消费者需要配置的对象:activemq原生连接工厂,spring包装过的工厂,监听容器(消费者),监听器,queue,topic

》之前监听器我们是直接实现匿名内部类的方式,但是在spring这就不能了,还是书写一个实现类并继承messageListener接口

<!-- 配置activemq的连接工厂 --><bean name='activemqConnectionFactory' class='org.apache.activemq.ActiveMQConnectionFactory'> <constructor-arg index='0' value='tcp://192.168.xx.xxx:61616'></constructor-arg></bean><!-- 配置spring包装的连接工厂 --><bean name='connectionFactory' class='org.springframework.jms.connection.SingleConnectionFactory'> <!-- 设置activemq的连接工厂 --> <property name='targetConnectionFactory' ref='activemqConnectionFactory'></property></bean><!-- 配置监听器 --><bean name='myMessageListener' class='cn.e3mall.search.listener.MyMessageListener'></bean><!-- 配置监听容器:消费者 --><bean name='messageListenerContainer' class='org.springframework.jms.listener.DefaultMessageListenerContainer'> <!-- 设置连接工厂 --> <property name='connectionFactory' ref='connectionFactory'></property> <!-- 设置目的对象 --> <property name='destination' ref='queueDestination'></property> <!-- 设置监听器 --> <property name='messageListener' ref='myMessageListener'></property></bean><!-- 配置queue对象 --><bean name='queueDestination' class='org.apache.activemq.command.ActiveMQQueue'> <constructor-arg name='name' value='spring-active-queue'></constructor-arg></bean><!-- 配置topic对象 --><bean name='topicDestination' class='org.apache.activemq.command.ActiveMQTopic'> <constructor-arg name='name' value='spring-active-topic'></constructor-arg></bean>

测试代码:

@Test public void testListener() throws IOException { ApplicationContext ioc = new ClassPathXmlApplicationContext('classpath:spring/applicationContext-activemq.xml'); System.in.read(); }

小结:我们的测试代码很简单,只要把容器启动了,监听器就会一致监听着。不需要写任何代码,监听器只要一直关注目的对象Destination是否有消息发送过来

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持好吧啦网。

标签: Spring
相关文章: