---
title: "Spring整合JMS——基于ActiveMQ实现"
author: "windpoplar"
date: "2017-02-02T19:39:51Z"
tags: []
categories: ["互联网"]
slug: "spring-jms-activemq"
draft: false
lastmod: "2017-02-02T19:39:51Z"
id: 2355126
url: "https://windpoplar.iteye.com/blog/2355126"
---

## 1.1     JMS简介

       JMS的全称是Java Message Service，即Java消息服务。它主要用于在生产者和消费者之间进行消息传递，生产者负责产生消息，而消费者负责接收消息。把它应用到实际的业务需求中的话我们可以在特定的时候利用生产者生成一消息，并进行发送，对应的消费者在接收到对应的消息后去完成对应的业务逻辑。对于消息的传递有两种类型，一种是点对点的，即一个生产者和一个消费者一一对应；另一种是发布/订阅模式，即一个生产者产生消息并进行发送后，可以由多个消费者进行接收。

## []()1.2     Spring整合JMS

       对JMS做了一个简要介绍之后，接下来就讲一下Spring整合JMS的具体过程。JMS只是一个标准，真正在使用它的时候我们需要有它的具体实现，这里我们就使用Apache的activeMQ来作为它的实现。所使用的依赖利用Maven来进行管理，具体依赖如下：

 

Xml代码  [![收藏代码](http://elim.iteye.com/images/icon_star.png)]()

01. &lt;dependencies&gt;
02. &lt;dependency&gt;
03. &lt;groupId&gt;junit&lt;/groupId&gt;
04. &lt;artifactId&gt;junit&lt;/artifactId&gt;
05. &lt;version&gt;4.10&lt;/version&gt;
06. &lt;scope&gt;test&lt;/scope&gt;
07. &lt;/dependency&gt;
08. &lt;dependency&gt;
09. &lt;groupId&gt;org.springframework&lt;/groupId&gt;
10. &lt;artifactId&gt;spring-context&lt;/artifactId&gt;
11. &lt;version&gt;${spring-version}&lt;/version&gt;
12. &lt;/dependency&gt;
13. &lt;dependency&gt;
14. &lt;groupId&gt;org.springframework&lt;/groupId&gt;
15. &lt;artifactId&gt;spring-jms&lt;/artifactId&gt;
16. &lt;version&gt;${spring-version}&lt;/version&gt;
17. &lt;/dependency&gt;
18. &lt;dependency&gt;
19. &lt;groupId&gt;org.springframework&lt;/groupId&gt;
20. &lt;artifactId&gt;spring-test&lt;/artifactId&gt;
21. &lt;version&gt;${spring-version}&lt;/version&gt;
22. &lt;/dependency&gt;
23. &lt;dependency&gt;
24. &lt;groupId&gt;javax.annotation&lt;/groupId&gt;
25. &lt;artifactId&gt;jsr250-api&lt;/artifactId&gt;
26. &lt;version&gt;1.0&lt;/version&gt;
27. &lt;/dependency&gt;
28. &lt;dependency&gt;
29. &lt;groupId&gt;org.apache.activemq&lt;/groupId&gt;
30. &lt;artifactId&gt;activemq-core&lt;/artifactId&gt;
31. &lt;version&gt;5.7.0&lt;/version&gt;
32. &lt;/dependency&gt;
33. &lt;/dependencies&gt;

 

### []()1.2.1  activeMQ准备

       既然是使用的apache的activeMQ作为JMS的实现，那么首先我们应该到apache官网上下载activeMQ（[http://activemq.apache.org/download.html](http://activemq.apache.org/download.html)），进行解压后运行其bin目录下面的activemq.bat文件启动activeMQ。

### []()1.2.2配置ConnectionFactory

       ConnectionFactory是用于产生到JMS服务器的链接的，Spring为我们提供了多个ConnectionFactory，有SingleConnectionFactory和CachingConnectionFactory。SingleConnectionFactory对于建立JMS服务器链接的请求会一直返回同一个链接，并且会忽略Connection的close方法调用。CachingConnectionFactory继承了SingleConnectionFactory，所以它拥有SingleConnectionFactory的所有功能，同时它还新增了缓存功能，它可以缓存Session、MessageProducer和MessageConsumer。这里我们使用SingleConnectionFactory来作为示例。

Xml代码  [![收藏代码](http://elim.iteye.com/images/icon_star.png)]()

1. &lt;bean id="connectionFactory" class="org.springframework.jms.connection.SingleConnectionFactory"/&gt;

 

       这样就定义好产生JMS服务器链接的ConnectionFactory了吗？答案是非也。Spring提供的ConnectionFactory只是Spring用于管理ConnectionFactory的，真正产生到JMS服务器链接的ConnectionFactory还得是由JMS服务厂商提供，并且需要把它注入到Spring提供的ConnectionFactory中。我们这里使用的是ActiveMQ实现的JMS，所以在我们这里真正的可以产生Connection的就应该是由ActiveMQ提供的ConnectionFactory。所以定义一个ConnectionFactory的完整代码应该如下所示：

Xml代码  [![收藏代码](http://elim.iteye.com/images/icon_star.png)]()

1. &lt;!-- 真正可以产生Connection的ConnectionFactory，由对应的 JMS服务厂商提供--&gt;
2. &lt;bean id="targetConnectionFactory" class="org.apache.activemq.ActiveMQConnectionFactory"&gt;
3. &lt;property name="brokerURL" value="tcp://localhost:61616"/&gt;
4. &lt;/bean&gt;
5. &lt;!-- Spring用于管理真正的ConnectionFactory的ConnectionFactory --&gt;
6. &lt;bean id="connectionFactory" class="org.springframework.jms.connection.SingleConnectionFactory"&gt;
7. &lt;!-- 目标ConnectionFactory对应真实的可以产生JMS Connection的ConnectionFactory --&gt;
8. &lt;property name="targetConnectionFactory" ref="targetConnectionFactory"/&gt;
9. &lt;/bean&gt;

  

       ActiveMQ为我们提供了一个PooledConnectionFactory，通过往里面注入一个ActiveMQConnectionFactory可以用来将Connection、Session和MessageProducer池化，这样可以大大的减少我们的资源消耗。当使用PooledConnectionFactory时，我们在定义一个ConnectionFactory时应该是如下定义：

 

Xml代码  [![收藏代码](http://elim.iteye.com/images/icon_star.png)]()

01. &lt;!-- 真正可以产生Connection的ConnectionFactory，由对应的 JMS服务厂商提供--&gt;
02. &lt;bean id="targetConnectionFactory" class="org.apache.activemq.ActiveMQConnectionFactory"&gt;
03. &lt;property name="brokerURL" value="tcp://localhost:61616"/&gt;
04. &lt;/bean&gt;
05. &lt;bean id="pooledConnectionFactory" class="org.apache.activemq.pool.PooledConnectionFactory"&gt;
06. &lt;property name="connectionFactory" ref="targetConnectionFactory"/&gt;
07. &lt;property name="maxConnections" value="10"/&gt;
08. &lt;/bean&gt;
09. &lt;bean id="connectionFactory" class="org.springframework.jms.connection.SingleConnectionFactory"&gt;
10. &lt;property name="targetConnectionFactory" ref="pooledConnectionFactory"/&gt;
11. &lt;/bean&gt;

### []()1.2.3配置生产者

配置好ConnectionFactory之后我们就需要配置生产者。生产者负责产生消息并发送到JMS服务器，这通常对应的是我们的一个业务逻辑服务实现类。但是我们的服务实现类是怎么进行消息的发送的呢？这通常是利用Spring为我们提供的JmsTemplate类来实现的，所以配置生产者其实最核心的就是配置进行消息发送的JmsTemplate。对于消息发送者而言，它在发送消息的时候要知道自己该往哪里发，为此，我们在定义JmsTemplate的时候需要往里面注入一个Spring提供的ConnectionFactory对象。

Xml代码  [![收藏代码](http://elim.iteye.com/images/icon_star.png)]()

1. &lt;!-- Spring提供的JMS工具类，它可以进行消息发送、接收等 --&gt;
2. &lt;bean id="jmsTemplate" class="org.springframework.jms.core.JmsTemplate"&gt;
3. &lt;!-- 这个connectionFactory对应的是我们定义的Spring提供的那个ConnectionFactory对象 --&gt;
4. &lt;property name="connectionFactory" ref="connectionFactory"/&gt;
5. &lt;/bean&gt;

 

       在真正利用JmsTemplate进行消息发送的时候，我们需要知道消息发送的目的地，即destination。在Jms中有一个用来表示目的地的Destination接口，它里面没有任何方法定义，只是用来做一个标识而已。当我们在使用JmsTemplate进行消息发送时没有指定destination的时候将使用默认的Destination。默认Destination可以通过在定义jmsTemplate bean对象时通过属性defaultDestination或defaultDestinationName来进行注入，defaultDestinationName对应的就是一个普通字符串。在ActiveMQ中实现了两种类型的Destination，一个是点对点的ActiveMQQueue，另一个就是支持订阅/发布模式的ActiveMQTopic。在定义这两种类型的Destination时我们都可以通过一个name属性来进行构造，如：

 

Xml代码  [![收藏代码](http://elim.iteye.com/images/icon_star.png)]()

01. &lt;!--这个是队列目的地，点对点的--&gt;
02. &lt;bean id="queueDestination" class="org.apache.activemq.command.ActiveMQQueue"&gt;
03. &lt;constructor-arg&gt;
04. &lt;value&gt;queue&lt;/value&gt;
05. &lt;/constructor-arg&gt;
06. &lt;/bean&gt;
07. &lt;!--这个是主题目的地，一对多的--&gt;
08. &lt;bean id="topicDestination" class="org.apache.activemq.command.ActiveMQTopic"&gt;
09. &lt;constructor-arg value="topic"/&gt;
10. &lt;/bean&gt;

       假设我们定义了一个ProducerService，里面有一个向Destination发送纯文本消息的方法sendMessage，那么我们的代码就大概是这个样子：

 

Java代码  [![收藏代码](http://elim.iteye.com/images/icon_star.png)]()

01. package com.tiantian.springintejms.service.impl;
02. import javax.annotation.Resource;
03. import javax.jms.Destination;
04. import javax.jms.JMSException;
05. import javax.jms.Message;
06. import javax.jms.Session;
07. import org.springframework.jms.core.JmsTemplate;
08. import org.springframework.jms.core.MessageCreator;
09. import org.springframework.stereotype.Component;
10. import com.tiantian.springintejms.service.ProducerService;
11. @Component
12. public class ProducerServiceImpl implements ProducerService {
13. private JmsTemplate jmsTemplate;
14. public void sendMessage(Destination destination, final String message) {
15. System.out.println("---------------生产者发送消息-----------------");
16. System.out.println("---------------生产者发了一个消息：" + message);
17. jmsTemplate.send(destination, new MessageCreator() {
18. public Message createMessage(Session session) throws JMSException {
19. return session.createTextMessage(message);
20. }
21. });
22. }
23. public JmsTemplate getJmsTemplate() {
24. returnjmsTemplate;
25. }
26. @Resource
27. public void setJmsTemplate(JmsTemplate jmsTemplate) {
28. this.jmsTemplate = jmsTemplate;
29. }
30. }

       我们可以看到在sendMessage方法体里面我们是通过jmsTemplate来发送消息到对应的Destination的。到此，我们生成一个简单的文本消息并把它发送到指定目的地Destination的生产者就配置好了。

### []()1.2.4配置消费者

生产者往指定目的地Destination发送消息后，接下来就是消费者对指定目的地的消息进行消费了。那么消费者是如何知道有生产者发送消息到指定目的地Destination了呢？这是通过Spring为我们封装的消息监听容器MessageListenerContainer实现的，它负责接收信息，并把接收到的信息分发给真正的MessageListener进行处理。每个消费者对应每个目的地都需要有对应的MessageListenerContainer。对于消息监听容器而言，除了要知道监听哪个目的地之外，还需要知道到哪里去监听，也就是说它还需要知道去监听哪个JMS服务器，这是通过在配置MessageConnectionFactory的时候往里面注入一个ConnectionFactory来实现的。所以我们在配置一个MessageListenerContainer的时候有三个属性必须指定，一个是表示从哪里监听的ConnectionFactory；一个是表示监听什么的Destination；一个是接收到消息以后进行消息处理的MessageListener。Spring一共为我们提供了两种类型的MessageListenerContainer，SimpleMessageListenerContainer和DefaultMessageListenerContainer。

SimpleMessageListenerContainer会在一开始的时候就创建一个会话session和消费者Consumer，并且会使用标准的JMS MessageConsumer.setMessageListener()方法注册监听器让JMS提供者调用监听器的回调函数。它不会动态的适应运行时需要和参与外部的事务管理。兼容性方面，它非常接近于独立的JMS规范，但一般不兼容Java EE的JMS限制。

大多数情况下我们还是使用的DefaultMessageListenerContainer，跟SimpleMessageListenerContainer相比，DefaultMessageListenerContainer会动态的适应运行时需要，并且能够参与外部的事务管理。它很好的平衡了对JMS提供者要求低、先进功能如事务参与和兼容Java EE环境。

**定义处理消息的MessageListener**

       要定义处理消息的MessageListener我们只需要实现JMS规范中的MessageListener接口就可以了。MessageListener接口中只有一个方法onMessage方法，当接收到消息的时候会自动调用该方法。

 

Java代码  [![收藏代码](http://elim.iteye.com/images/icon_star.png)]()

01. package com.tiantian.springintejms.listener;
02. import javax.jms.JMSException;
03. import javax.jms.Message;
04. import javax.jms.MessageListener;
05. import javax.jms.TextMessage;
06. public class ConsumerMessageListener implements MessageListener {
07. public void onMessage(Message message) {
08. //这里我们知道生产者发送的就是一个纯文本消息，所以这里可以直接进行强制转换
09. TextMessage textMsg = (TextMessage) message;
10. System.out.println("接收到一个纯文本消息。");
11. try {
12. System.out.println("消息内容是：" + textMsg.getText());
13. } catch (JMSException e) {
14. e.printStackTrace();
15. }
16. }
17. }

 

       有了MessageListener之后我们就可以在Spring的配置文件中配置一个消息监听容器了。

Xml代码  [![收藏代码](http://elim.iteye.com/images/icon_star.png)]()

01. &lt;!--这个是队列目的地--&gt;
02. &lt;bean id="queueDestination" class="org.apache.activemq.command.ActiveMQQueue"&gt;
03. &lt;constructor-arg&gt;
04. &lt;value&gt;queue&lt;/value&gt;
05. &lt;/constructor-arg&gt;
06. &lt;/bean&gt;
07. &lt;!-- 消息监听器 --&gt;
08. &lt;bean id="consumerMessageListener" class="com.tiantian.springintejms.listener.ConsumerMessageListener"/&gt;
09. &lt;!-- 消息监听容器 --&gt;
10. &lt;bean id="jmsContainer"        class="org.springframework.jms.listener.DefaultMessageListenerContainer"&gt;
11. &lt;property name="connectionFactory" ref="connectionFactory" /&gt;
12. &lt;property name="destination" ref="queueDestination" /&gt;
13. &lt;property name="messageListener" ref="consumerMessageListener" /&gt;
14. &lt;/bean&gt;

  

       我们可以看到我们定义了一个名叫queue的ActiveMQQueue目的地，我们的监听器就是监听了发送到这个目的地的消息。

       至此我们的生成者和消费者都配置完成了，这也就意味着我们的整合已经完成了。这个时候完整的Spring的配置文件应该是这样的：

Xml代码  [![收藏代码](http://elim.iteye.com/images/icon_star.png)]()

01. &lt;?xml version="1.0" encoding="UTF-8"?&gt;
02. &lt;beans xmlns="http://www.springframework.org/schema/beans"
03. xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:context="http://www.springframework.org/schema/context"
04. xmlns:jms="http://www.springframework.org/schema/jms"
05. xsi:schemaLocation="http://www.springframework.org/schema/beans
06. http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
07. http://www.springframework.org/schema/context
08. http://www.springframework.org/schema/context/spring-context-3.0.xsd
09. http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
10. http://www.springframework.org/schema/jms http://www.springframework.org/schema/jms/spring-jms-3.0.xsd"&gt;
11. &lt;context:component-scan base-package="com.tiantian" /&gt;
12. &lt;!-- Spring提供的JMS工具类，它可以进行消息发送、接收等 --&gt;
13. &lt;bean id="jmsTemplate" class="org.springframework.jms.core.JmsTemplate"&gt;
14. &lt;!-- 这个connectionFactory对应的是我们定义的Spring提供的那个ConnectionFactory对象 --&gt;
15. &lt;property name="connectionFactory" ref="connectionFactory"/&gt;
16. &lt;/bean&gt;
17. &lt;!-- 真正可以产生Connection的ConnectionFactory，由对应的 JMS服务厂商提供--&gt;
18. &lt;bean id="targetConnectionFactory" class="org.apache.activemq.ActiveMQConnectionFactory"&gt;
19. &lt;property name="brokerURL" value="tcp://localhost:61616"/&gt;
20. &lt;/bean&gt;
21. &lt;!-- Spring用于管理真正的ConnectionFactory的ConnectionFactory --&gt;
22. &lt;bean id="connectionFactory" class="org.springframework.jms.connection.SingleConnectionFactory"&gt;
23. &lt;!-- 目标ConnectionFactory对应真实的可以产生JMS Connection的ConnectionFactory --&gt;
24. &lt;property name="targetConnectionFactory" ref="targetConnectionFactory"/&gt;
25. &lt;/bean&gt;
26. &lt;!--这个是队列目的地--&gt;
27. &lt;bean id="queueDestination" class="org.apache.activemq.command.ActiveMQQueue"&gt;
28. &lt;constructor-arg&gt;
29. &lt;value&gt;queue&lt;/value&gt;
30. &lt;/constructor-arg&gt;
31. &lt;/bean&gt;
32. &lt;!-- 消息监听器 --&gt;
33. &lt;bean id="consumerMessageListener" class="com.tiantian.springintejms.listener.ConsumerMessageListener"/&gt;
34. &lt;!-- 消息监听容器 --&gt;
35. &lt;bean id="jmsContainer"
36. class="org.springframework.jms.listener.DefaultMessageListenerContainer"&gt;
37. &lt;property name="connectionFactory" ref="connectionFactory" /&gt;
38. &lt;property name="destination" ref="queueDestination" /&gt;
39. &lt;property name="messageListener" ref="consumerMessageListener" /&gt;
40. &lt;/bean&gt;
41. &lt;/beans&gt;

 

 

       接着我们来测试一下，看看我们的整合是否真的成功了，测试代码如下：

 

Java代码  [![收藏代码](http://elim.iteye.com/images/icon_star.png)]()

01. package com.tiantian.springintejms.test;
02. import javax.jms.Destination;
03. import org.junit.Test;
04. import org.junit.runner.RunWith;
05. import org.springframework.beans.factory.annotation.Autowired;
06. import org.springframework.beans.factory.annotation.Qualifier;
07. import org.springframework.test.context.ContextConfiguration;
08. import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
09. import com.tiantian.springintejms.service.ProducerService;
10. @RunWith(SpringJUnit4ClassRunner.class)
11. @ContextConfiguration("/applicationContext.xml")
12. public class ProducerConsumerTest {
13. @Autowired
14. private ProducerService producerService;
15. @Autowired
16. @Qualifier("queueDestination")
17. private Destination destination;
18. @Test
19. public void testSend() {
20. for (int i=0; i&lt;2; i++) {
21. producerService.sendMessage(destination, "你好，生产者！这是消息：" + (i+1));
22. }
23. }
24. }

 

       在上面的测试代码中我们利用生产者发送了两个消息，正常来说，消费者应该可以接收到这两个消息。运行测试代码后控制台输出如下：

   
![](http://dl2.iteye.com/upload/attachment/0086/1032/bc449d33-84f8-3742-bca5-0251132e810f.png)  
 

       看，控制台已经进行了正确的输出，这说明我们的整合确实是已经成功了。