JMS进阶-Spring整合Active MQ
Posted
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了JMS进阶-Spring整合Active MQ相关的知识,希望对你有一定的参考价值。
只能说,Spring太流弊了,啥都能整合~~~~
First of all, start the service of Active MQ
项目目录结构如下
用到的jar包如下
activemq-client-5.13.1.jar commons-logging-1.1.3.jar geronimo-j2ee-management_1.1_spec-1.0.1.jar geronimo-jms_1.1_spec-1.1.1.jar hamcrest-core-1.3.jar junit-4.12.jar log4j-1.2.17.jar org.springframework.transaction.jar slf4j-api-1.7.5.jar slf4j-log4j12-1.7.13.jar spring-aop-4.2.4.RELEASE.jar spring-aspects-4.2.4.RELEASE.jar spring-beans-4.2.4.RELEASE.jar spring-context-4.2.4.RELEASE.jar spring-context-support-4.2.4.RELEASE.jar spring-core-4.2.4.RELEASE.jar spring-expression-4.2.4.RELEASE.jar spring-jms-4.2.4.RELEASE.jar spring-messaging-4.2.4.RELEASE.jar spring-test-4.2.4.RELEASE.jar
applicationContext.xml
<?xml version="1.0" encoding="UTF-8"?> <beans xmlns="http://www.springframework.org/schema/beans" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:context="http://www.springframework.org/schema/context" xmlns:jms="http://www.springframework.org/schema/jms" xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.0.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.0.xsd http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.0.xsd http://www.springframework.org/schema/jms http://www.springframework.org/schema/jms/spring-jms-3.0.xsd"> <context:component-scan base-package="com.spring" /> <!-- Spring提供的JMS工具类,它可以进行消息发送、接收等 --> <bean id="jmsTemplate" class="org.springframework.jms.core.JmsTemplate"> <!-- 这个connectionFactory对应的是我们定义的Spring提供的那个ConnectionFactory对象 --> <property name="connectionFactory" ref="connectionFactory" /> </bean> <!-- 真正可以产生Connection的ConnectionFactory,由对应的 JMS服务厂商提供 --> <bean id="targetConnectionFactory" class="org.apache.activemq.ActiveMQConnectionFactory"> <property name="brokerURL" value="tcp://localhost:61616" /> </bean> <!-- Spring用于管理真正的ConnectionFactory的ConnectionFactory --> <bean id="connectionFactory" class="org.springframework.jms.connection.SingleConnectionFactory"> <!-- 目标ConnectionFactory对应真实的可以产生JMS Connection的ConnectionFactory --> <property name="targetConnectionFactory" ref="targetConnectionFactory" /> </bean> <!--这个是队列目的地 --> <bean id="queueDestination" class="org.apache.activemq.command.ActiveMQQueue"> <constructor-arg> <value>queue</value> </constructor-arg> </bean> <!-- 消息监听器 --> <bean id="consumerMessageListener" class="com.spring.jms.ConsumerMessageListener" /> <!-- 消息监听容器 --> <bean id="jmsContainer" class="org.springframework.jms.listener.DefaultMessageListenerContainer"> <property name="connectionFactory" ref="connectionFactory" /> <property name="destination" ref="queueDestination" /> <property name="messageListener" ref="consumerMessageListener" /> </bean> </beans>
ProducerService接口
public interface ProducerService { public void sendMessage(Destination destination, final String message); }
ProducerServiceImpl.java
import org.apache.log4j.Logger; @Component public class ProducerServiceImpl implements ProducerService{ private static final Logger logger = Logger.getLogger(ProducerConsumerTest.class); @Resource private JmsTemplate jmsTemplate; @Override public void sendMessage(Destination destination, final String message) { System.out.println("sendMessage:" + message); jmsTemplate.send(destination, new MessageCreator() { public Message createMessage(Session session) throws JMSException { return session.createTextMessage(message); } }); } }
ConsumerMessageListener
import org.apache.log4j.Logger; public class ConsumerMessageListener implements MessageListener{ private static final Logger logger = Logger.getLogger(ProducerConsumerTest.class); @Override public void onMessage(Message message) { //这里我们知道生产者发送的就是一个纯文本消息,所以这里可以直接进行强制转换 TextMessage textMsg = (TextMessage) message; try { System.out.println("接收到消息:" + textMsg.getText()); } catch (JMSException e) { e.printStackTrace(); } } }
测试类
import org.apache.log4j.Logger; @RunWith(SpringJUnit4ClassRunner.class) @ContextConfiguration("/applicationContext.xml") public class ProducerConsumerTest { private static final Logger logger = Logger.getLogger(ProducerConsumerTest.class); @Autowired private ProducerService producerService; @Autowired @Qualifier("queueDestination") private Destination destination; @Test public void testSend() { for (int i=0; i<2; i++) { producerService.sendMessage(destination, "你好,生产者!这是消息:" + (i+1)); } } }
控制台输出如下
log4j:WARN No appenders could be found for logger (org.springframework.test.context.junit4.SpringJUnit4ClassRunner). log4j:WARN Please initialize the log4j system properly. log4j:WARN See http://logging.apache.org/log4j/1.2/faq.html#noconfig for more info. sendMessage:你好,生产者!这是消息:1 接收到消息:你好,生产者!这是消息:1 sendMessage:你好,生产者!这是消息:2 接收到消息:你好,生产者!这是消息:2
本文出自 “优赛工作室” 博客,请务必保留此出处http://shamrock.blog.51cto.com/2079212/1747817
以上是关于JMS进阶-Spring整合Active MQ的主要内容,如果未能解决你的问题,请参考以下文章