【JMS 4】spring 整合activemq

第一篇博文深刻浅出JMS(一)–JMS基本概念,咱们介绍了JMS的两种消息模型:点对点和发布订阅模型,以及消息被消费的两个方式:同步和异步,JMS编程模型的对象,最后说了JMS的优势。java

第二篇博文深刻浅出JMS(二)–ActiveMQ简单介绍以及安装,咱们介绍了消息中间件ActiveMQ,安装,启动,以及优缺点。web

第三篇博文深刻浅出JMS(三)–ActiveMQ简单的HelloWorld实例,咱们实现了一种点对点的同步消息模型,并无给你们呈现发布订阅模型。spring

前言apache

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

环境准备

工具

  1. JDK1.6或1.7spring-mvc

  2. Spring4.1.0缓存

  3. ActiveMQ5.11.1服务器

  4. Tomcat7.x网络

目录结构

这里写图片描述

所需jar包

这里写图片描述

项目的配置

配置ConnectionFactory

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

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

CachingConnectionFactory:继承了SingleConnectionFactory,因此它拥有SingleConnectionFactory的全部功能,同时它还新增了缓存功能,它能够缓存Session、MessageProducer和MessageConsumer。咱们使用CachingConnectionFactory来做为示例。

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

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

    <!-- ActiveMQ 链接工厂 -->
    <!-- 真正能够产生Connection的ConnectionFactory,由对应的 JMS服务厂商提供-->
    <!-- 若是链接网络:tcp://ip:61616;未链接网络:tcp://localhost:61616 以及用户名,密码-->
    <amq:connectionFactory id="amqConnectionFactory"
        brokerURL="tcp://192.168.3.3:61616" userName="admin" password="admin"  />

    <!-- Spring Caching链接工厂 -->
    <!-- Spring用于管理真正的ConnectionFactory的ConnectionFactory -->  
    <bean id="connectionFactory" class="org.springframework.jms.connection.CachingConnectionFactory">
        <!-- 目标ConnectionFactory对应真实的能够产生JMS Connection的ConnectionFactory -->  
        <property name="targetConnectionFactory" ref="amqConnectionFactory"></property>
        <!-- 同上,同理 -->
        <!-- <constructor-arg ref="amqConnectionFactory" /> -->
        <!-- Session缓存数量 -->
        <property name="sessionCacheSize" value="100" />
    </bean>12345678910111213141516

配置生产者

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

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

    <!-- Spring JmsTemplate 的消息生产者 start-->

    <!-- 定义JmsTemplate的Queue类型 -->
    <bean id="jmsQueueTemplate" class="org.springframework.jms.core.JmsTemplate">
        <!-- 这个connectionFactory对应的是咱们定义的Spring提供的那个ConnectionFactory对象 -->  
        <constructor-arg ref="connectionFactory" />
        <!-- 非pub/sub模型(发布/订阅),即队列模式 -->
        <property name="pubSubDomain" value="false" />
    </bean>

    <!-- 定义JmsTemplate的Topic类型 -->
    <bean id="jmsTopicTemplate" class="org.springframework.jms.core.JmsTemplate">
         <!-- 这个connectionFactory对应的是咱们定义的Spring提供的那个ConnectionFactory对象 -->  
        <constructor-arg ref="connectionFactory" />
        <!-- pub/sub模型(发布/订阅) -->
        <property name="pubSubDomain" value="true" />
    </bean>

    <!--Spring JmsTemplate 的消息生产者 end-->12345678910111213141516171819

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

配置消费者

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

<!-- 消息消费者 start-->

    <!-- 定义Queue监听器 -->
    <jms:listener-container destination-type="queue" container-type="default" connection-factory="connectionFactory" acknowledge="auto">
        <jms:listener destination="test.queue" ref="queueReceiver1"/>
        <jms:listener destination="test.queue" ref="queueReceiver2"/>
    </jms:listener-container>

    <!-- 定义Topic监听器 -->
    <jms:listener-container destination-type="topic" container-type="default" connection-factory="connectionFactory" acknowledge="auto">
        <jms:listener destination="test.topic" ref="topicReceiver1"/>
        <jms:listener destination="test.topic" ref="topicReceiver2"/>
    </jms:listener-container>

    <!-- 消息消费者 end -->123456789101112131415

ActiveMQ.xml

此时,Spring和JMS,ActiveMQ整合的ActiveMQ.xml已经完成,下面展现全部的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:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:amq="http://activemq.apache.org/schema/core"
    xmlns:jms="http://www.springframework.org/schema/jms"
    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/jms
        http://www.springframework.org/schema/jms/spring-jms-4.0.xsd
        http://activemq.apache.org/schema/core
        http://activemq.apache.org/schema/core/activemq-core-5.8.0.xsd">

    <!-- ActiveMQ 链接工厂 -->
    <!-- 真正能够产生Connection的ConnectionFactory,由对应的 JMS服务厂商提供-->
    <!-- 若是链接网络:tcp://ip:61616;未链接网络:tcp://localhost:61616 以及用户名,密码-->
    <amq:connectionFactory id="amqConnectionFactory"
        brokerURL="tcp://192.168.3.3:61616" userName="admin" password="admin"  />

    <!-- Spring Caching链接工厂 -->
    <!-- Spring用于管理真正的ConnectionFactory的ConnectionFactory -->  
    <bean id="connectionFactory" class="org.springframework.jms.connection.CachingConnectionFactory">
        <!-- 目标ConnectionFactory对应真实的能够产生JMS Connection的ConnectionFactory -->  
        <property name="targetConnectionFactory" ref="amqConnectionFactory"></property>
        <!-- 同上,同理 -->
        <!-- <constructor-arg ref="amqConnectionFactory" /> -->
        <!-- Session缓存数量 -->
        <property name="sessionCacheSize" value="100" />
    </bean>

    <!-- Spring JmsTemplate 的消息生产者 start-->

    <!-- 定义JmsTemplate的Queue类型 -->
    <bean id="jmsQueueTemplate" class="org.springframework.jms.core.JmsTemplate">
        <!-- 这个connectionFactory对应的是咱们定义的Spring提供的那个ConnectionFactory对象 -->  
        <constructor-arg ref="connectionFactory" />
        <!-- 非pub/sub模型(发布/订阅),即队列模式 -->
        <property name="pubSubDomain" value="false" />
    </bean>

    <!-- 定义JmsTemplate的Topic类型 -->
    <bean id="jmsTopicTemplate" class="org.springframework.jms.core.JmsTemplate">
         <!-- 这个connectionFactory对应的是咱们定义的Spring提供的那个ConnectionFactory对象 -->  
        <constructor-arg ref="connectionFactory" />
        <!-- pub/sub模型(发布/订阅) -->
        <property name="pubSubDomain" value="true" />
    </bean>

    <!--Spring JmsTemplate 的消息生产者 end-->


    <!-- 消息消费者 start-->

    <!-- 定义Queue监听器 -->
    <jms:listener-container destination-type="queue" container-type="default" connection-factory="connectionFactory" acknowledge="auto">
        <jms:listener destination="test.queue" ref="queueReceiver1"/>
        <jms:listener destination="test.queue" ref="queueReceiver2"/>
    </jms:listener-container>

    <!-- 定义Topic监听器 -->
    <jms:listener-container destination-type="topic" container-type="default" connection-factory="connectionFactory" acknowledge="auto">
        <jms:listener destination="test.topic" ref="topicReceiver1"/>
        <jms:listener destination="test.topic" ref="topicReceiver2"/>
    </jms:listener-container>

    <!-- 消息消费者 end --></beans>  1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768

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

web.xml

<?xml version="1.0" encoding="UTF-8"?><web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xmlns="http://java.sun.com/xml/ns/javaee" xmlns:web="http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd"
    xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd"
    version="3.0">
    <display-name>ActiveMQSpringDemo</display-name>

    <!-- Log4J Start -->
    <context-param>
        <param-name>log4jConfigLocation</param-name>
        <param-value>classpath:log4j.properties</param-value>
    </context-param>
    <context-param>
        <param-name>log4jRefreshInterval</param-name>
        <param-value>6000</param-value>
    </context-param>
    <!-- Spring Log4J config -->
    <listener>
        <listener-class>org.springframework.web.util.Log4jConfigListener</listener-class>
    </listener>
    <!-- Log4J End -->

    <!-- Spring 编码过滤器 start -->
    <filter>
        <filter-name>characterEncoding</filter-name>
        <filter-class>org.springframework.web.filter.CharacterEncodingFilter</filter-class>
        <init-param>
            <param-name>encoding</param-name>
            <param-value>UTF-8</param-value>
        </init-param>
        <init-param>
            <param-name>forceEncoding</param-name>
            <param-value>true</param-value>
        </init-param>
    </filter>
    <filter-mapping>
        <filter-name>characterEncoding</filter-name>
        <url-pattern>/*</url-pattern>
    </filter-mapping>
    <!-- Spring 编码过滤器 End -->

    <!-- Spring Application Context Listener Start -->
    <context-param>
        <param-name>contextConfigLocation</param-name>
        <param-value>classpath*:applicationContext.xml,classpath*:ActiveMQ.xml</param-value>
    </context-param>
    <listener>
        <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
    </listener>
    <!-- Spring Application Context Listener End -->


    <!-- Spring MVC Config Start -->
    <servlet>
        <servlet-name>SpringMVC</servlet-name>
        <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>

        <init-param>
            <param-name>contextConfigLocation</param-name>
            <param-value>classpath:spring-mvc.xml</param-value>
        </init-param>
        <load-on-startup>1</load-on-startup>
    </servlet>
    <servlet-mapping>
        <servlet-name>SpringMVC</servlet-name>
        <!-- Filter all resources -->
        <url-pattern>/</url-pattern>
    </servlet-mapping>
    <!-- Spring MVC Config End --></web-app>1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071

运行效果

这里写图片描述

这里写图片描述

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

总结

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

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

源码下载

相关文章
相关标签/搜索