首页 > 代码库 > ActiveMQ(06):ActiveMQ结合Spring开发--第二种方式
ActiveMQ(06):ActiveMQ结合Spring开发--第二种方式
一、pom.xml与mq.properties
Spring提供了对JMS的支持,需要添加Spring支持jms的包,如下:
<dependency> <groupId>org.springframework</groupId> <artifactId>spring-jms</artifactId> <version>4.1.7.RELEASE</version> </dependency>
添加ActiveMQ的pool包,如下:
<dependency> <groupId>org.apache.activemq</groupId> <artifactId>activemq-pool</artifactId> <version>5.11.1</version> </dependency>
添加xbean的标签配置,如下:
<dependency> <groupId>org.apache.xbean</groupId> <artifactId>xbean-spring</artifactId> <version>3.16</version> </dependency>
mq.properties:
activemq.brokerURL=tcp://192.168.91.8:61616 activemq.userName=liuy activemq.password=123456 activemq.maxConnections=100 activemq.destination.name=spring-queue activemq.destinationTopic.name=spring-topic
二、mq.xml配置
<?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.0.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.0.xsd http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-4.0.xsd http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-4.0.xsd http://www.springframework.org/schema/util http://www.springframework.org/schema/util/spring-util-4.0.xsd "> <bean id="jmsFactory" class="org.apache.activemq.pool.PooledConnectionFactory" destroy-method="stop"> <property name="connectionFactory"> <bean class="org.apache.activemq.ActiveMQConnectionFactory"> <property name="brokerURL"> <value>${activemq.brokerURL}</value> </property> <property name="userName" value="http://www.mamicode.com/${activemq.userName}"></property> <property name="password" value="http://www.mamicode.com/${activemq.password}"></property> </bean> </property> <property name="maxConnections" value="http://www.mamicode.com/${activemq.maxConnections}"></property> </bean> <bean id="jmsTemplate" class="org.springframework.jms.core.JmsTemplate"> <property name="connectionFactory" ref="jmsFactory" /> <!-- 设置默认的目的地 --> <property name="defaultDestination" ref="destinationTopic" /> <property name="messageConverter"> <bean class="org.springframework.jms.support.converter.SimpleMessageConverter" /> </property> </bean> <!-- 目的地:Queue --> <bean id="destination" class="org.apache.activemq.command.ActiveMQQueue"> <constructor-arg name="name" value="http://www.mamicode.com/${activemq.destination.name}" /> </bean> <!-- 目的地:Topic,非持久化 --> <bean id="destinationTopic" class="org.apache.activemq.command.ActiveMQTopic"> <constructor-arg name="name" value="http://www.mamicode.com/${activemq.destinationTopic.name}" /> </bean> </beans>
三、消息发送与接收
package com.liuy.spring; import javax.jms.JMSException; import javax.jms.Message; import javax.jms.Session; import javax.jms.TextMessage; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.jms.core.JmsTemplate; import org.springframework.jms.core.MessageCreator; import org.springframework.stereotype.Service; /** * @description 描述 * @author liuyu * @version 1.0 * @date:2017年4月11日下午8:32:46 */ @Service public class MQService { @Autowired private JmsTemplate jt; /**发送*/ public void send(String message) { jt.send(new MessageCreator() { public Message createMessage(Session s) throws JMSException { TextMessage msg = s.createTextMessage(message); return msg; } }); } /**接收*/ public void receiver() { String msg = (String)jt.receiveAndConvert(); System.out.println("msg==="+msg); } }
四、监听器
如果想要在Spring中配置消费者的话,就不需要再启动接收的客户端了,配置如下:
<!-- 消费者监听器 --> <bean id="jmsContainer" class="org.springframework.jms.listener.DefaultMessageListenerContainer"> <property name="connectionFactory" ref="jmsFactory" /> <!-- 监听的目的地 --> <property name="destination" ref="destinationTopic" /> <property name="messageListener" ref="messageListener" /> </bean> <bean id="messageListener" class="com.liuy.spring.b.listener.MyMessageListener"></bean>
package com.liuy.spring.b.listener; import javax.jms.JMSException; import javax.jms.Message; import javax.jms.MessageListener; import javax.jms.TextMessage; /** * @description 描述 * @author liuyu * @version 1.0 * @date:2017年4月15日上午11:00:23 */ public class MyMessageListener implements MessageListener { @Override public void onMessage(Message message) { TextMessage msg = (TextMessage) message; try { System.out.println("receive txt msg===" + msg.getText()); } catch (JMSException e) { e.printStackTrace(); } } }
本文出自 “我爱大金子” 博客,请务必保留此出处http://1754966750.blog.51cto.com/7455444/1916381
ActiveMQ(06):ActiveMQ结合Spring开发--第二种方式
声明:以上内容来自用户投稿及互联网公开渠道收集整理发布,本网站不拥有所有权,未作人工编辑处理,也不承担相关法律责任,若内容有误或涉及侵权可进行投诉: 投诉/举报 工作人员会在5个工作日内联系你,一经查实,本站将立刻删除涉嫌侵权内容。