JMS【四】--Spring和ActiveMQ整合的完整实例

第一篇博文JMS【一】--JMS基本概念,我们介绍了JMS的两种消息模型:点对点和发布订阅模型,以及消息被消费的两个方式:同步和异步,JMS编程模型的对象,最后说了JMS的优点。

第二篇博文JMS【二】--ActiveMQ简单介绍以及安装,我们介绍了消息中间件ActiveMQ,安装,启动,以及优缺点。

第三篇博文JMS【三】--ActiveMQ简单的HelloWorld实例,我们实现了一种点对点的同步消息模型,并没有给大家呈现发布订阅模型。

前言

这篇博文,我们基于Spring+JMS+ActiveMQ+Tomcat,做一个Spring4.1.0和ActiveMQ5.11.1整合实例,实现了Point-To-Point的异步队列消息和PUB/SUB(发布/订阅)模型,简单实例,不包含任何业务。

1、环境准备

 1.1、工具

  1. JDK1.6或1.7

  2. Spring4.1.0

  3. ActiveMQ5.11.1

  4. Tomcat7.x

 1.2、目录结构

  这里写图片描述

 1.3、所需jar包

  这里写图片描述

2、项目的配置

 2.1、配置ConnectionFactory

  connectionFactory是Spring用于创建到JMS服务器链接的,Spring提供了多种connectionFactory,我们介绍两个SingleConnectionFactory和CachingConnectionFactory。

  SingleConnectionFactory:对于建立JMS服务器链接的请求会一直返回同一个链接,并且会忽略Connection的close方法调用。

  CachingConnectionFactory:继承了SingleConnectionFactory,所以它拥有 SingleConnectionFactory的所有功能,同时它还新增了缓存功能,它可以缓存Session、MessageProducer和 MessageConsumer。我们使用CachingConnectionFactory来作为示例。

<bean id="connectionFactory" class="org.springframework.jms.connection.CachingConnectionFactory">
    </bean>

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

 1    <!-- ActiveMQ 连接工厂 -->
 2     <!-- 真正可以产生Connection的ConnectionFactory,由对应的 JMS服务厂商提供-->
 3     <!-- 如果连接网络:tcp://ip:61616;未连接网络:tcp://localhost:61616 以及用户名,密码-->
 4     <amq:connectionFactory id="amqConnectionFactory"
 5         brokerURL="tcp://192.168.3.3:61616" userName="admin" password="admin"  />
 6 
 7     <!-- Spring Caching连接工厂 -->
 8     <!-- Spring用于管理真正的ConnectionFactory的ConnectionFactory -->  
 9     <bean id="connectionFactory" class="org.springframework.jms.connection.CachingConnectionFactory">
10         <!-- 目标ConnectionFactory对应真实的可以产生JMS Connection的ConnectionFactory -->  
11         <property name="targetConnectionFactory" ref="amqConnectionFactory"></property>
12         <!-- 同上,同理 -->
13         <!-- <constructor-arg ref="amqConnectionFactory" /> -->
14         <!-- Session缓存数量 -->
15         <property name="sessionCacheSize" value="100" />
16     </bean>

 2.2、配置生产者

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

  在利用JmsTemplate进行消息发送的时候,我们需要知道发送哪种消息类型:一个是点对点的ActiveMQQueue,另一个就是支持订阅/发布模式的ActiveMQTopic。如下所示:

 1     <!-- Spring JmsTemplate 的消息生产者 start-->
 2 
 3     <!-- 定义JmsTemplate的Queue类型 -->
 4     <bean id="jmsQueueTemplate" class="org.springframework.jms.core.JmsTemplate">
 5         <!-- 这个connectionFactory对应的是我们定义的Spring提供的那个ConnectionFactory对象 -->  
 6         <constructor-arg ref="connectionFactory" />
 7         <!-- 非pub/sub模型(发布/订阅),即队列模式 -->
 8         <property name="pubSubDomain" value="false" />
 9     </bean>
10 
11     <!-- 定义JmsTemplate的Topic类型 -->
12     <bean id="jmsTopicTemplate" class="org.springframework.jms.core.JmsTemplate">
13          <!-- 这个connectionFactory对应的是我们定义的Spring提供的那个ConnectionFactory对象 -->  
14         <constructor-arg ref="connectionFactory" />
15         <!-- pub/sub模型(发布/订阅) -->
16         <property name="pubSubDomain" value="true" />
17     </bean>
18 
19     <!--Spring JmsTemplate 的消息生产者 end-->

生产者如何指定目的地和发送消息?大家看源码即可,就不再这提供了。

 2.3、配置消费者

  生产者往指定目的地Destination发送消息后,接下来就是消费者对指定目的地的消息进行消费了。那么消费者是如何知道有生产者发送消息到指定目的地Destination了呢?每个消费者对应每个目的地都需要有对应的MessageListenerContainer。对于消息监听容器而言,除了要知道监听哪个目的地之外,还需要知道到哪里去监听,也就是说它还需要知道去监听哪个JMS服务器,通过配置 MessageListenerContainer的时候往里面注入一个ConnectionFactory来实现的。所以我们在配置一个 MessageListenerContainer的时候有三个属性必须指定:一个是表示从哪里监听的ConnectionFactory;一个是表示监 听什么的Destination;一个是接收到消息以后进行消息处理的MessageListener。

 1 <!-- 消息消费者 start-->
 2 
 3     <!-- 定义Queue监听器 -->
 4     <jms:listener-container destination-type="queue" container-type="default" connection-factory="connectionFactory" acknowledge="auto">
 5         <jms:listener destination="test.queue" ref="queueReceiver1"/>
 6         <jms:listener destination="test.queue" ref="queueReceiver2"/>
 7     </jms:listener-container>
 8 
 9     <!-- 定义Topic监听器 -->
10     <jms:listener-container destination-type="topic" container-type="default" connection-factory="connectionFactory" acknowledge="auto">
11         <jms:listener destination="test.topic" ref="topicReceiver1"/>
12         <jms:listener destination="test.topic" ref="topicReceiver2"/>
13     </jms:listener-container>
14   
15     <!-- 消息消费者 end -->

 2.4、ActiveMQ.xml

  此时,Spring和JMS,ActiveMQ整合的ActiveMQ.xml已经完成,下面展示所有的xml

 1 <?xml version="1.0" encoding="UTF-8"?>
 2 <beans xmlns="http://www.springframework.org/schema/beans"
 3     xmlns:context="http://www.springframework.org/schema/context"
 4     xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:amq="http://activemq.apache.org/schema/core"
 5     xmlns:jms="http://www.springframework.org/schema/jms"
 6     xsi:schemaLocation="http://www.springframework.org/schema/beans   
 7         http://www.springframework.org/schema/beans/spring-beans-4.0.xsd   
 8         http://www.springframework.org/schema/context   
 9         http://www.springframework.org/schema/context/spring-context-4.0.xsd
10         http://www.springframework.org/schema/jms
11         http://www.springframework.org/schema/jms/spring-jms-4.0.xsd
12         http://activemq.apache.org/schema/core
13         http://activemq.apache.org/schema/core/activemq-core-5.8.0.xsd">
14 
15     <!-- ActiveMQ 连接工厂 -->
16     <!-- 真正可以产生Connection的ConnectionFactory,由对应的 JMS服务厂商提供-->
17     <!-- 如果连接网络:tcp://ip:61616;未连接网络:tcp://localhost:61616 以及用户名,密码-->
18     <amq:connectionFactory id="amqConnectionFactory"
19         brokerURL="tcp://192.168.3.3:61616" userName="admin" password="admin"  />
20 
21     <!-- Spring Caching连接工厂 -->
22     <!-- Spring用于管理真正的ConnectionFactory的ConnectionFactory -->  
23     <bean id="connectionFactory" class="org.springframework.jms.connection.CachingConnectionFactory">
24         <!-- 目标ConnectionFactory对应真实的可以产生JMS Connection的ConnectionFactory -->  
25         <property name="targetConnectionFactory" ref="amqConnectionFactory"></property>
26         <!-- 同上,同理 -->
27         <!-- <constructor-arg ref="amqConnectionFactory" /> -->
28         <!-- Session缓存数量 -->
29         <property name="sessionCacheSize" value="100" />
30     </bean>
31 
32     <!-- Spring JmsTemplate 的消息生产者 start-->
33 
34     <!-- 定义JmsTemplate的Queue类型 -->
35     <bean id="jmsQueueTemplate" class="org.springframework.jms.core.JmsTemplate">
36         <!-- 这个connectionFactory对应的是我们定义的Spring提供的那个ConnectionFactory对象 -->  
37         <constructor-arg ref="connectionFactory" />
38         <!-- 非pub/sub模型(发布/订阅),即队列模式 -->
39         <property name="pubSubDomain" value="false" />
40     </bean>
41 
42     <!-- 定义JmsTemplate的Topic类型 -->
43     <bean id="jmsTopicTemplate" class="org.springframework.jms.core.JmsTemplate">
44          <!-- 这个connectionFactory对应的是我们定义的Spring提供的那个ConnectionFactory对象 -->  
45         <constructor-arg ref="connectionFactory" />
46         <!-- pub/sub模型(发布/订阅) -->
47         <property name="pubSubDomain" value="true" />
48     </bean>
49 
50     <!--Spring JmsTemplate 的消息生产者 end-->
51 
52 
53     <!-- 消息消费者 start-->
54 
55     <!-- 定义Queue监听器 -->
56     <jms:listener-container destination-type="queue" container-type="default" connection-factory="connectionFactory" acknowledge="auto">
57         <jms:listener destination="test.queue" ref="queueReceiver1"/>
58         <jms:listener destination="test.queue" ref="queueReceiver2"/>
59     </jms:listener-container>
60 
61     <!-- 定义Topic监听器 -->
62     <jms:listener-container destination-type="topic" container-type="default" connection-factory="connectionFactory" acknowledge="auto">
63         <jms:listener destination="test.topic" ref="topicReceiver1"/>
64         <jms:listener destination="test.topic" ref="topicReceiver2"/>
65     </jms:listener-container>
66 
67     <!-- 消息消费者 end -->
68 </beans> 

鉴于博文内容较多,我们只是在粘贴web.xml的配置,就不在博文中提供Spring和SpringMVC的XML配置,其他内容,大家查看源码即可。

 2.5、web.xml

 1 <?xml version="1.0" encoding="UTF-8"?>
 2 <web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
 3     xmlns="http://java.sun.com/xml/ns/javaee" xmlns:web="http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd"
 4     xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd"
 5     version="3.0">
 6     <display-name>ActiveMQSpringDemo</display-name>
 7 
 8     <!-- Log4J Start -->
 9     <context-param>
10         <param-name>log4jConfigLocation</param-name>
11         <param-value>classpath:log4j.properties</param-value>
12     </context-param>
13     <context-param>
14         <param-name>log4jRefreshInterval</param-name>
15         <param-value>6000</param-value>
16     </context-param>
17     <!-- Spring Log4J config -->
18     <listener>
19         <listener-class>org.springframework.web.util.Log4jConfigListener</listener-class>
20     </listener>
21     <!-- Log4J End -->
22 
23     <!-- Spring 编码过滤器 start -->
24     <filter>
25         <filter-name>characterEncoding</filter-name>
26         <filter-class>org.springframework.web.filter.CharacterEncodingFilter</filter-class>
27         <init-param>
28             <param-name>encoding</param-name>
29             <param-value>UTF-8</param-value>
30         </init-param>
31         <init-param>
32             <param-name>forceEncoding</param-name>
33             <param-value>true</param-value>
34         </init-param>
35     </filter>
36     <filter-mapping>
37         <filter-name>characterEncoding</filter-name>
38         <url-pattern>/*</url-pattern>
39     </filter-mapping>
40     <!-- Spring 编码过滤器 End -->
41 
42     <!-- Spring Application Context Listener Start -->
43     <context-param>
44         <param-name>contextConfigLocation</param-name>
45         <param-value>classpath*:applicationContext.xml,classpath*:ActiveMQ.xml</param-value>
46     </context-param>
47     <listener>
48         <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
49     </listener>
50     <!-- Spring Application Context Listener End -->
51 
52 
53     <!-- Spring MVC Config Start -->
54     <servlet>
55         <servlet-name>SpringMVC</servlet-name>
56         <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
57 
58         <init-param>
59             <param-name>contextConfigLocation</param-name>
60             <param-value>classpath:spring-mvc.xml</param-value>
61         </init-param>
62         <load-on-startup>1</load-on-startup>
63     </servlet>
64     <servlet-mapping>
65         <servlet-name>SpringMVC</servlet-name>
66         <!-- Filter all resources -->
67         <url-pattern>/</url-pattern>
68     </servlet-mapping>
69     <!-- Spring MVC Config End -->
70 
71 </web-app>

3、运行效果

  这里写图片描述

  这里写图片描述

  从上图可以看出队列模型和PUB/SUB模型的区别,Queue只能由一个消费者接收,其他Queue中的成员无法接受到被已消费的信息,而Topic则可以,只要是订阅了Topic的消费者,全部可以获取到生产者发布的信息。

4、总结

  Spring提供了对JMS的支持,ActiveMQ提供了很好的实现,而此时我们已经将两者完美的结合在了一起。

  下篇博文我们实现Spring和ActiveMQ消息的持久化。

  源码下载

  git地址:git@github.com:changbingbing/ActiveMQ.git

posted @ 2016-02-18 15:48  灰太郎^_^  阅读(356)  评论(0编辑  收藏  举报