第一篇博文深入浅出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(发布/订阅)模型,简单实例,不包含任何业务。
环境准备
工具
JDK1.6或1.7
Spring4.1.0
ActiveMQ5.11.1
Tomcat7.x
目录结构
所需jar包
项目的配置
配置ConnectionFactory
connectionFactory是Spring用于创建到JMS服务器链接的,Spring提供了多种connectionFactory,我们介绍两个SingleConnectionFactory和CachingConnectionFactory。
SingleConnectionFactory:对于建立JMS服务器链接的请求会一直返回同一个链接,并且会忽略Connection的close方法调用。
CachingConnectionFactory:继承了SingleConnectionFactory,所以它拥有SingleConnectionFactory的所有功能,同时它还新增了缓存功能,它可以缓存Session、MessageProducer和MessageConsumer。我们使用CachingConnectionFactory来作为示例。
<code class="language-xml hljs has-numbering"><span class="hljs-tag"><<span class="hljs-title">bean</span> <span class="hljs-attribute">id</span>=<span class="hljs-value">"connectionFactory"</span> <span class="hljs-attribute">class</span>=<span class="hljs-value">"org.springframework.jms.connection.CachingConnectionFactory"</span>></span>
<span class="hljs-tag"></<span class="hljs-title">bean</span>></span></code><ul style="" class="pre-numbering"><li>1</li><li>2</li></ul><ul style="" class="pre-numbering"><li>1</li><li>2</li></ul>
Spring提供的ConnectionFactory只是Spring用于管理ConnectionFactory的,真正产生到JMS服务器链接的ConnectionFactory还得是由JMS服务厂商提供,并且需要把它注入到Spring提供的ConnectionFactory中。我们这里使用的是ActiveMQ实现的JMS,所以在我们这里真正的可以产生Connection的就应该是由ActiveMQ提供的ConnectionFactory。所以定义一个ConnectionFactory的完整代码应该如下所示:
<code class="language-xml hljs has-numbering"> <span class="hljs-comment"><!-- ActiveMQ 连接工厂 --></span>
<span class="hljs-comment"><!-- 真正可以产生Connection的ConnectionFactory,由对应的 JMS服务厂商提供--></span>
<span class="hljs-comment"><!-- 如果连接网络:tcp://ip:61616;未连接网络:tcp://localhost:61616 以及用户名,密码--></span>
<span class="hljs-tag"><<span class="hljs-title">amq:connectionFactory</span> <span class="hljs-attribute">id</span>=<span class="hljs-value">"amqConnectionFactory"</span>
<span class="hljs-attribute">brokerURL</span>=<span class="hljs-value">"tcp://192.168.3.3:61616"</span> <span class="hljs-attribute">userName</span>=<span class="hljs-value">"admin"</span> <span class="hljs-attribute">password</span>=<span class="hljs-value">"admin"</span> /></span>
<span class="hljs-comment"><!-- Spring Caching连接工厂 --></span>
<span class="hljs-comment"><!-- Spring用于管理真正的ConnectionFactory的ConnectionFactory --></span>
<span class="hljs-tag"><<span class="hljs-title">bean</span> <span class="hljs-attribute">id</span>=<span class="hljs-value">"connectionFactory"</span> <span class="hljs-attribute">class</span>=<span class="hljs-value">"org.springframework.jms.connection.CachingConnectionFactory"</span>></span>
<span class="hljs-comment"><!-- 目标ConnectionFactory对应真实的可以产生JMS Connection的ConnectionFactory --></span>
<span class="hljs-tag"><<span class="hljs-title">property</span> <span class="hljs-attribute">name</span>=<span class="hljs-value">"targetConnectionFactory"</span> <span class="hljs-attribute">ref</span>=<span class="hljs-value">"amqConnectionFactory"</span>></span><span class="hljs-tag"></<span class="hljs-title">property</span>></span>
<span class="hljs-comment"><!-- 同上,同理 --></span>
<span class="hljs-comment"><!-- <constructor-arg ref="amqConnectionFactory" /> --></span>
<span class="hljs-comment"><!-- Session缓存数量 --></span>
<span class="hljs-tag"><<span class="hljs-title">property</span> <span class="hljs-attribute">name</span>=<span class="hljs-value">"sessionCacheSize"</span> <span class="hljs-attribute">value</span>=<span class="hljs-value">"100"</span> /></span>
<span class="hljs-tag"></<span class="hljs-title">bean</span>></span></code><ul style="" class="pre-numbering"><li>1</li><li>2</li><li>3</li><li>4</li><li>5</li><li>6</li><li>7</li><li>8</li><li>9</li><li>10</li><li>11</li><li>12</li><li>13</li><li>14</li><li>15</li><li>16</li></ul><ul style="" class="pre-numbering"><li>1</li><li>2</li><li>3</li><li>4</li><li>5</li><li>6</li><li>7</li><li>8</li><li>9</li><li>10</li><li>11</li><li>12</li><li>13</li><li>14</li><li>15</li><li>16</li></ul>
配置生产者
配置好ConnectionFactory之后我们就需要配置生产者。生产者负责产生消息并发送到JMS服务器。但是我们要怎么进行消息发送呢?通常是利用Spring为我们提供的JmsTemplate类来实现的,所以配置生产者其实最核心的就是配置消息发送的JmsTemplate。对于消息发送者而言,它在发送消息的时候要知道自己该往哪里发,为此,我们在定义JmsTemplate的时候需要注入一个Spring提供的ConnectionFactory对象。
在利用JmsTemplate进行消息发送的时候,我们需要知道发送哪种消息类型:一个是点对点的ActiveMQQueue,另一个就是支持订阅/发布模式的ActiveMQTopic。如下所示:
<code class="language-xml hljs has-numbering"> <span class="hljs-comment"><!-- Spring JmsTemplate 的消息生产者 start--></span>
<span class="hljs-comment"><!-- 定义JmsTemplate的Queue类型 --></span>
<span class="hljs-tag"><<span class="hljs-title">bean</span> <span class="hljs-attribute">id</span>=<span class="hljs-value">"jmsQueueTemplate"</span> <span class="hljs-attribute">class</span>=<span class="hljs-value">"org.springframework.jms.core.JmsTemplate"</span>></span>
<span class="hljs-comment"><!-- 这个connectionFactory对应的是我们定义的Spring提供的那个ConnectionFactory对象 --></span>
<span class="hljs-tag"><<span class="hljs-title">constructor-arg</span> <span class="hljs-attribute">ref</span>=<span class="hljs-value">"connectionFactory"</span> /></span>
<span class="hljs-comment"><!-- 非pub/sub模型(发布/订阅),即队列模式 --></span>
<span class="hljs-tag"><<span class="hljs-title">property</span> <span class="hljs-attribute">name</span>=<span class="hljs-value">"pubSubDomain"</span> <span class="hljs-attribute">value</span>=<span class="hljs-value">"false"</span> /></span>
<span class="hljs-tag"></<span class="hljs-title">bean</span>></span>
<span class="hljs-comment"><!-- 定义JmsTemplate的Topic类型 --></span>
<span class="hljs-tag"><<span class="hljs-title">bean</span> <span class="hljs-attribute">id</span>=<span class="hljs-value">"jmsTopicTemplate"</span> <span class="hljs-attribute">class</span>=<span class="hljs-value">"org.springframework.jms.core.JmsTemplate"</span>></span>
<span class="hljs-comment"><!-- 这个connectionFactory对应的是我们定义的Spring提供的那个ConnectionFactory对象 --></span>
<span class="hljs-tag"><<span class="hljs-title">constructor-arg</span> <span class="hljs-attribute">ref</span>=<span class="hljs-value">"connectionFactory"</span> /></span>
<span class="hljs-comment"><!-- pub/sub模型(发布/订阅) --></span>
<span class="hljs-tag"><<span class="hljs-title">property</span> <span class="hljs-attribute">name</span>=<span class="hljs-value">"pubSubDomain"</span> <span class="hljs-attribute">value</span>=<span class="hljs-value">"true"</span> /></span>
<span class="hljs-tag"></<span class="hljs-title">bean</span>></span>
<span class="hljs-comment"><!--Spring JmsTemplate 的消息生产者 end--></span></code><ul style="" class="pre-numbering"><li>1</li><li>2</li><li>3</li><li>4</li><li>5</li><li>6</li><li>7</li><li>8</li><li>9</li><li>10</li><li>11</li><li>12</li><li>13</li><li>14</li><li>15</li><li>16</li><li>17</li><li>18</li><li>19</li></ul><ul style="" class="pre-numbering"><li>1</li><li>2</li><li>3</li><li>4</li><li>5</li><li>6</li><li>7</li><li>8</li><li>9</li><li>10</li><li>11</li><li>12</li><li>13</li><li>14</li><li>15</li><li>16</li><li>17</li><li>18</li><li>19</li></ul>
生产者如何指定目的地和发送消息?大家看源码即可,就不再这提供了。
配置消费者
生产者往指定目的地Destination发送消息后,接下来就是消费者对指定目的地的消息进行消费了。那么消费者是如何知道有生产者发送消息到指定目的地Destination了呢?每个消费者对应每个目的地都需要有对应的MessageListenerContainer。对于消息监听容器而言,除了要知道监听哪个目的地之外,还需要知道到哪里去监听,也就是说它还需要知道去监听哪个JMS服务器,通过配置MessageListenerContainer的时候往里面注入一个ConnectionFactory来实现的。所以我们在配置一个MessageListenerContainer的时候有三个属性必须指定:一个是表示从哪里监听的ConnectionFactory;一个是表示监听什么的Destination;一个是接收到消息以后进行消息处理的MessageListener。
<code class="language-xml hljs has-numbering"><span class="hljs-comment"><!-- 消息消费者 start--></span>
<span class="hljs-comment"><!-- 定义Queue监听器 --></span>
<span class="hljs-tag"><<span class="hljs-title">jms:listener-container</span> <span class="hljs-attribute">destination-type</span>=<span class="hljs-value">"queue"</span> <span class="hljs-attribute">container-type</span>=<span class="hljs-value">"default"</span> <span class="hljs-attribute">connection-factory</span>=<span class="hljs-value">"connectionFactory"</span> <span class="hljs-attribute">acknowledge</span>=<span class="hljs-value">"auto"</span>></span>
<span class="hljs-tag"><<span class="hljs-title">jms:listener</span> <span class="hljs-attribute">destination</span>=<span class="hljs-value">"test.queue"</span> <span class="hljs-attribute">ref</span>=<span class="hljs-value">"queueReceiver1"</span>/></span>
<span class="hljs-tag"><<span class="hljs-title">jms:listener</span> <span class="hljs-attribute">destination</span>=<span class="hljs-value">"test.queue"</span> <span class="hljs-attribute">ref</span>=<span class="hljs-value">"queueReceiver2"</span>/></span>
<span class="hljs-tag"></<span class="hljs-title">jms:listener-container</span>></span>
<span class="hljs-comment"><!-- 定义Topic监听器 --></span>
<span class="hljs-tag"><<span class="hljs-title">jms:listener-container</span> <span class="hljs-attribute">destination-type</span>=<span class="hljs-value">"topic"</span> <span class="hljs-attribute">container-type</span>=<span class="hljs-value">"default"</span> <span class="hljs-attribute">connection-factory</span>=<span class="hljs-value">"connectionFactory"</span> <span class="hljs-attribute">acknowledge</span>=<span class="hljs-value">"auto"</span>></span>
<span class="hljs-tag"><<span class="hljs-title">jms:listener</span> <span class="hljs-attribute">destination</span>=<span class="hljs-value">"test.topic"</span> <span class="hljs-attribute">ref</span>=<span class="hljs-value">"topicReceiver1"</span>/></span>
<span class="hljs-tag"><<span class="hljs-title">jms:listener</span> <span class="hljs-attribute">destination</span>=<span class="hljs-value">"test.topic"</span> <span class="hljs-attribute">ref</span>=<span class="hljs-value">"topicReceiver2"</span>/></span>
<span class="hljs-tag"></<span class="hljs-title">jms:listener-container</span>></span>
<span class="hljs-comment"><!-- 消息消费者 end --></span></code><ul style="" class="pre-numbering"><li>1</li><li>2</li><li>3</li><li>4</li><li>5</li><li>6</li><li>7</li><li>8</li><li>9</li><li>10</li><li>11</li><li>12</li><li>13</li><li>14</li><li>15</li></ul><ul style="" class="pre-numbering"><li>1</li><li>2</li><li>3</li><li>4</li><li>5</li><li>6</li><li>7</li><li>8</li><li>9</li><li>10</li><li>11</li><li>12</li><li>13</li><li>14</li><li>15</li></ul>
ActiveMQ.xml
此时,Spring和JMS,ActiveMQ整合的ActiveMQ.xml已经完成,下面展示所有的xml
<code class="language-xml hljs has-numbering"><span class="hljs-pi"><?xml version="1.0" encoding="UTF-8"?></span>
<span class="hljs-tag"><<span class="hljs-title">beans</span> <span class="hljs-attribute">xmlns</span>=<span class="hljs-value">"http://www.springframework.org/schema/beans"</span>
<span class="hljs-attribute">xmlns:context</span>=<span class="hljs-value">"http://www.springframework.org/schema/context"</span>
<span class="hljs-attribute">xmlns:xsi</span>=<span class="hljs-value">"http://www.w3.org/2001/XMLSchema-instance"</span> <span class="hljs-attribute">xmlns:amq</span>=<span class="hljs-value">"http://activemq.apache.org/schema/core"</span>
<span class="hljs-attribute">xmlns:jms</span>=<span class="hljs-value">"http://www.springframework.org/schema/jms"</span>
<span class="hljs-attribute">xsi:schemaLocation</span>=<span class="hljs-value">"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"</span>></span>
<span class="hljs-comment"><!-- ActiveMQ 连接工厂 --></span>
<span class="hljs-comment"><!-- 真正可以产生Connection的ConnectionFactory,由对应的 JMS服务厂商提供--></span>
<span class="hljs-comment"><!-- 如果连接网络:tcp://ip:61616;未连接网络:tcp://localhost:61616 以及用户名,密码--></span>
<span class="hljs-tag"><<span class="hljs-title">amq:connectionFactory</span> <span class="hljs-attribute">id</span>=<span class="hljs-value">"amqConnectionFactory"</span>
<span class="hljs-attribute">brokerURL</span>=<span class="hljs-value">"tcp://192.168.3.3:61616"</span> <span class="hljs-attribute">userName</span>=<span class="hljs-value">"admin"</span> <span class="hljs-attribute">password</span>=<span class="hljs-value">"admin"</span> /></span>
<span class="hljs-comment"><!-- Spring Caching连接工厂 --></span>
<span class="hljs-comment"><!-- Spring用于管理真正的ConnectionFactory的ConnectionFactory --></span>
<span class="hljs-tag"><<span class="hljs-title">bean</span> <span class="hljs-attribute">id</span>=<span class="hljs-value">"connectionFactory"</span> <span class="hljs-attribute">class</span>=<span class="hljs-value">"org.springframework.jms.connection.CachingConnectionFactory"</span>></span>
<span class="hljs-comment"><!-- 目标ConnectionFactory对应真实的可以产生JMS Connection的ConnectionFactory --></span>
<span class="hljs-tag"><<span class="hljs-title">property</span> <span class="hljs-attribute">name</span>=<span class="hljs-value">"targetConnectionFactory"</span> <span class="hljs-attribute">ref</span>=<span class="hljs-value">"amqConnectionFactory"</span>></span><span class="hljs-tag"></<span class="hljs-title">property</span>></span>
<span class="hljs-comment"><!-- 同上,同理 --></span>
<span class="hljs-comment"><!-- <constructor-arg ref="amqConnectionFactory" /> --></span>
<span class="hljs-comment"><!-- Session缓存数量 --></span>
<span class="hljs-tag"><<span class="hljs-title">property</span> <span class="hljs-attribute">name</span>=<span class="hljs-value">"sessionCacheSize"</span> <span class="hljs-attribute">value</span>=<span class="hljs-value">"100"</span> /></span>
<span class="hljs-tag"></<span class="hljs-title">bean</span>></span>
<span class="hljs-comment"><!-- Spring JmsTemplate 的消息生产者 start--></span>
<span class="hljs-comment"><!-- 定义JmsTemplate的Queue类型 --></span>
<span class="hljs-tag"><<span class="hljs-title">bean</span> <span class="hljs-attribute">id</span>=<span class="hljs-value">"jmsQueueTemplate"</span> <span class="hljs-attribute">class</span>=<span class="hljs-value">"org.springframework.jms.core.JmsTemplate"</span>></span>
<span class="hljs-comment"><!-- 这个connectionFactory对应的是我们定义的Spring提供的那个ConnectionFactory对象 --></span>
<span class="hljs-tag"><<span class="hljs-title">constructor-arg</span> <span class="hljs-attribute">ref</span>=<span class="hljs-value">"connectionFactory"</span> /></span>
<span class="hljs-comment"><!-- 非pub/sub模型(发布/订阅),即队列模式 --></span>
<span class="hljs-tag"><<span class="hljs-title">property</span> <span class="hljs-attribute">name</span>=<span class="hljs-value">"pubSubDomain"</span> <span class="hljs-attribute">value</span>=<span class="hljs-value">"false"</span> /></span>
<span class="hljs-tag"></<span class="hljs-title">bean</span>></span>
<span class="hljs-comment"><!-- 定义JmsTemplate的Topic类型 --></span>
<span class="hljs-tag"><<span class="hljs-title">bean</span> <span class="hljs-attribute">id</span>=<span class="hljs-value">"jmsTopicTemplate"</span> <span class="hljs-attribute">class</span>=<span class="hljs-value">"org.springframework.jms.core.JmsTemplate"</span>></span>
<span class="hljs-comment"><!-- 这个connectionFactory对应的是我们定义的Spring提供的那个ConnectionFactory对象 --></span>
<span class="hljs-tag"><<span class="hljs-title">constructor-arg</span> <span class="hljs-attribute">ref</span>=<span class="hljs-value">"connectionFactory"</span> /></span>
<span class="hljs-comment"><!-- pub/sub模型(发布/订阅) --></span>
<span class="hljs-tag"><<span class="hljs-title">property</span> <span class="hljs-attribute">name</span>=<span class="hljs-value">"pubSubDomain"</span> <span class="hljs-attribute">value</span>=<span class="hljs-value">"true"</span> /></span>
<span class="hljs-tag"></<span class="hljs-title">bean</span>></span>
<span class="hljs-comment"><!--Spring JmsTemplate 的消息生产者 end--></span>
<span class="hljs-comment"><!-- 消息消费者 start--></span>
<span class="hljs-comment"><!-- 定义Queue监听器 --></span>
<span class="hljs-tag"><<span class="hljs-title">jms:listener-container</span> <span class="hljs-attribute">destination-type</span>=<span class="hljs-value">"queue"</span> <span class="hljs-attribute">container-type</span>=<span class="hljs-value">"default"</span> <span class="hljs-attribute">connection-factory</span>=<span class="hljs-value">"connectionFactory"</span> <span class="hljs-attribute">acknowledge</span>=<span class="hljs-value">"auto"</span>></span>
<span class="hljs-tag"><<span class="hljs-title">jms:listener</span> <span class="hljs-attribute">destination</span>=<span class="hljs-value">"test.queue"</span> <span class="hljs-attribute">ref</span>=<span class="hljs-value">"queueReceiver1"</span>/></span>
<span class="hljs-tag"><<span class="hljs-title">jms:listener</span> <span class="hljs-attribute">destination</span>=<span class="hljs-value">"test.queue"</span> <span class="hljs-attribute">ref</span>=<span class="hljs-value">"queueReceiver2"</span>/></span>
<span class="hljs-tag"></<span class="hljs-title">jms:listener-container</span>></span>
<span class="hljs-comment"><!-- 定义Topic监听器 --></span>
<span class="hljs-tag"><<span class="hljs-title">jms:listener-container</span> <span class="hljs-attribute">destination-type</span>=<span class="hljs-value">"topic"</span> <span class="hljs-attribute">container-type</span>=<span class="hljs-value">"default"</span> <span class="hljs-attribute">connection-factory</span>=<span class="hljs-value">"connectionFactory"</span> <span class="hljs-attribute">acknowledge</span>=<span class="hljs-value">"auto"</span>></span>
<span class="hljs-tag"><<span class="hljs-title">jms:listener</span> <span class="hljs-attribute">destination</span>=<span class="hljs-value">"test.topic"</span> <span class="hljs-attribute">ref</span>=<span class="hljs-value">"topicReceiver1"</span>/></span>
<span class="hljs-tag"><<span class="hljs-title">jms:listener</span> <span class="hljs-attribute">destination</span>=<span class="hljs-value">"test.topic"</span> <span class="hljs-attribute">ref</span>=<span class="hljs-value">"topicReceiver2"</span>/></span>
<span class="hljs-tag"></<span class="hljs-title">jms:listener-container</span>></span>
<span class="hljs-comment"><!-- 消息消费者 end --></span>
<span class="hljs-tag"></<span class="hljs-title">beans</span>></span> </code><ul style="" class="pre-numbering"><li>1</li><li>2</li><li>3</li><li>4</li><li>5</li><li>6</li><li>7</li><li>8</li><li>9</li><li>10</li><li>11</li><li>12</li><li>13</li><li>14</li><li>15</li><li>16</li><li>17</li><li>18</li><li>19</li><li>20</li><li>21</li><li>22</li><li>23</li><li>24</li><li>25</li><li>26</li><li>27</li><li>28</li><li>29</li><li>30</li><li>31</li><li>32</li><li>33</li><li>34</li><li>35</li><li>36</li><li>37</li><li>38</li><li>39</li><li>40</li><li>41</li><li>42</li><li>43</li><li>44</li><li>45</li><li>46</li><li>47</li><li>48</li><li>49</li><li>50</li><li>51</li><li>52</li><li>53</li><li>54</li><li>55</li><li>56</li><li>57</li><li>58</li><li>59</li><li>60</li><li>61</li><li>62</li><li>63</li><li>64</li><li>65</li><li>66</li><li>67</li><li>68</li></ul><ul style="" class="pre-numbering"><li>1</li><li>2</li><li>3</li><li>4</li><li>5</li><li>6</li><li>7</li><li>8</li><li>9</li><li>10</li><li>11</li><li>12</li><li>13</li><li>14</li><li>15</li><li>16</li><li>17</li><li>18</li><li>19</li><li>20</li><li>21</li><li>22</li><li>23</li><li>24</li><li>25</li><li>26</li><li>27</li><li>28</li><li>29</li><li>30</li><li>31</li><li>32</li><li>33</li><li>34</li><li>35</li><li>36</li><li>37</li><li>38</li><li>39</li><li>40</li><li>41</li><li>42</li><li>43</li><li>44</li><li>45</li><li>46</li><li>47</li><li>48</li><li>49</li><li>50</li><li>51</li><li>52</li><li>53</li><li>54</li><li>55</li><li>56</li><li>57</li><li>58</li><li>59</li><li>60</li><li>61</li><li>62</li><li>63</li><li>64</li><li>65</li><li>66</li><li>67</li><li>68</li></ul>
鉴于博文内容较多,我们只是在粘贴web.xml的配置,就不在博文中提供Spring和SpringMVC的XML配置,其他内容,大家查看源码即可。
web.xml
<code class="language-xml hljs has-numbering"><span class="hljs-pi"><?xml version="1.0" encoding="UTF-8"?></span>
<span class="hljs-tag"><<span class="hljs-title">web-app</span> <span class="hljs-attribute">xmlns:xsi</span>=<span class="hljs-value">"http://www.w3.org/2001/XMLSchema-instance"</span>
<span class="hljs-attribute">xmlns</span>=<span class="hljs-value">"http://java.sun.com/xml/ns/javaee"</span> <span class="hljs-attribute">xmlns:web</span>=<span class="hljs-value">"http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd"</span>
<span class="hljs-attribute">xsi:schemaLocation</span>=<span class="hljs-value">"http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd"</span>
<span class="hljs-attribute">version</span>=<span class="hljs-value">"3.0"</span>></span>
<span class="hljs-tag"><<span class="hljs-title">display-name</span>></span>ActiveMQSpringDemo<span class="hljs-tag"></<span class="hljs-title">display-name</span>></span>
<span class="hljs-comment"><!-- Log4J Start --></span>
<span class="hljs-tag"><<span class="hljs-title">context-param</span>></span>
<span class="hljs-tag"><<span class="hljs-title">param-name</span>></span>log4jConfigLocation<span class="hljs-tag"></<span class="hljs-title">param-name</span>></span>
<span class="hljs-tag"><<span class="hljs-title">param-value</span>></span>classpath:log4j.properties<span class="hljs-tag"></<span class="hljs-title">param-value</span>></span>
<span class="hljs-tag"></<span class="hljs-title">context-param</span>></span>
<span class="hljs-tag"><<span class="hljs-title">context-param</span>></span>
<span class="hljs-tag"><<span class="hljs-title">param-name</span>></span>log4jRefreshInterval<span class="hljs-tag"></<span class="hljs-title">param-name</span>></span>
<span class="hljs-tag"><<span class="hljs-title">param-value</span>></span>6000<span class="hljs-tag"></<span class="hljs-title">param-value</span>></span>
<span class="hljs-tag"></<span class="hljs-title">context-param</span>></span>
<span class="hljs-comment"><!-- Spring Log4J config --></span>
<span class="hljs-tag"><<span class="hljs-title">listener</span>></span>
<span class="hljs-tag"><<span class="hljs-title">listener-class</span>></span>org.springframework.web.util.Log4jConfigListener<span class="hljs-tag"></<span class="hljs-title">listener-class</span>></span>
<span class="hljs-tag"></<span class="hljs-title">listener</span>></span>
<span class="hljs-comment"><!-- Log4J End --></span>
<span class="hljs-comment"><!-- Spring 编码过滤器 start --></span>
<span class="hljs-tag"><<span class="hljs-title">filter</span>></span>
<span class="hljs-tag"><<span class="hljs-title">filter-name</span>></span>characterEncoding<span class="hljs-tag"></<span class="hljs-title">filter-name</span>></span>
<span class="hljs-tag"><<span class="hljs-title">filter-class</span>></span>org.springframework.web.filter.CharacterEncodingFilter<span class="hljs-tag"></<span class="hljs-title">filter-class</span>></span>
<span class="hljs-tag"><<span class="hljs-title">init-param</span>></span>
<span class="hljs-tag"><<span class="hljs-title">param-name</span>></span>encoding<span class="hljs-tag"></<span class="hljs-title">param-name</span>></span>
<span class="hljs-tag"><<span class="hljs-title">param-value</span>></span>UTF-8<span class="hljs-tag"></<span class="hljs-title">param-value</span>></span>
<span class="hljs-tag"></<span class="hljs-title">init-param</span>></span>
<span class="hljs-tag"><<span class="hljs-title">init-param</span>></span>
<span class="hljs-tag"><<span class="hljs-title">param-name</span>></span>forceEncoding<span class="hljs-tag"></<span class="hljs-title">param-name</span>></span>
<span class="hljs-tag"><<span class="hljs-title">param-value</span>></span>true<span class="hljs-tag"></<span class="hljs-title">param-value</span>></span>
<span class="hljs-tag"></<span class="hljs-title">init-param</span>></span>
<span class="hljs-tag"></<span class="hljs-title">filter</span>></span>
<span class="hljs-tag"><<span class="hljs-title">filter-mapping</span>></span>
<span class="hljs-tag"><<span class="hljs-title">filter-name</span>></span>characterEncoding<span class="hljs-tag"></<span class="hljs-title">filter-name</span>></span>
<span class="hljs-tag"><<span class="hljs-title">url-pattern</span>></span>/*<span class="hljs-tag"></<span class="hljs-title">url-pattern</span>></span>
<span class="hljs-tag"></<span class="hljs-title">filter-mapping</span>></span>
<span class="hljs-comment"><!-- Spring 编码过滤器 End --></span>
<span class="hljs-comment"><!-- Spring Application Context Listener Start --></span>
<span class="hljs-tag"><<span class="hljs-title">context-param</span>></span>
<span class="hljs-tag"><<span class="hljs-title">param-name</span>></span>contextConfigLocation<span class="hljs-tag"></<span class="hljs-title">param-name</span>></span>
<span class="hljs-tag"><<span class="hljs-title">param-value</span>></span>classpath*:applicationContext.xml,classpath*:ActiveMQ.xml<span class="hljs-tag"></<span class="hljs-title">param-value</span>></span>
<span class="hljs-tag"></<span class="hljs-title">context-param</span>></span>
<span class="hljs-tag"><<span class="hljs-title">listener</span>></span>
<span class="hljs-tag"><<span class="hljs-title">listener-class</span>></span>org.springframework.web.context.ContextLoaderListener<span class="hljs-tag"></<span class="hljs-title">listener-class</span>></span>
<span class="hljs-tag"></<span class="hljs-title">listener</span>></span>
<span class="hljs-comment"><!-- Spring Application Context Listener End --></span>
<span class="hljs-comment"><!-- Spring MVC Config Start --></span>
<span class="hljs-tag"><<span class="hljs-title">servlet</span>></span>
<span class="hljs-tag"><<span class="hljs-title">servlet-name</span>></span>SpringMVC<span class="hljs-tag"></<span class="hljs-title">servlet-name</span>></span>
<span class="hljs-tag"><<span class="hljs-title">servlet-class</span>></span>org.springframework.web.servlet.DispatcherServlet<span class="hljs-tag"></<span class="hljs-title">servlet-class</span>></span>
<span class="hljs-tag"><<span class="hljs-title">init-param</span>></span>
<span class="hljs-tag"><<span class="hljs-title">param-name</span>></span>contextConfigLocation<span class="hljs-tag"></<span class="hljs-title">param-name</span>></span>
<span class="hljs-tag"><<span class="hljs-title">param-value</span>></span>classpath:spring-mvc.xml<span class="hljs-tag"></<span class="hljs-title">param-value</span>></span>
<span class="hljs-tag"></<span class="hljs-title">init-param</span>></span>
<span class="hljs-tag"><<span class="hljs-title">load-on-startup</span>></span>1<span class="hljs-tag"></<span class="hljs-title">load-on-startup</span>></span>
<span class="hljs-tag"></<span class="hljs-title">servlet</span>></span>
<span class="hljs-tag"><<span class="hljs-title">servlet-mapping</span>></span>
<span class="hljs-tag"><<span class="hljs-title">servlet-name</span>></span>SpringMVC<span class="hljs-tag"></<span class="hljs-title">servlet-name</span>></span>
<span class="hljs-comment"><!-- Filter all resources --></span>
<span class="hljs-tag"><<span class="hljs-title">url-pattern</span>></span>/<span class="hljs-tag"></<span class="hljs-title">url-pattern</span>></span>
<span class="hljs-tag"></<span class="hljs-title">servlet-mapping</span>></span>
<span class="hljs-comment"><!-- Spring MVC Config End --></span>
<span class="hljs-tag"></<span class="hljs-title">web-app</span>></span></code><ul style="" class="pre-numbering"><li>1</li><li>2</li><li>3</li><li>4</li><li>5</li><li>6</li><li>7</li><li>8</li><li>9</li><li>10</li><li>11</li><li>12</li><li>13</li><li>14</li><li>15</li><li>16</li><li>17</li><li>18</li><li>19</li><li>20</li><li>21</li><li>22</li><li>23</li><li>24</li><li>25</li><li>26</li><li>27</li><li>28</li><li>29</li><li>30</li><li>31</li><li>32</li><li>33</li><li>34</li><li>35</li><li>36</li><li>37</li><li>38</li><li>39</li><li>40</li><li>41</li><li>42</li><li>43</li><li>44</li><li>45</li><li>46</li><li>47</li><li>48</li><li>49</li><li>50</li><li>51</li><li>52</li><li>53</li><li>54</li><li>55</li><li>56</li><li>57</li><li>58</li><li>59</li><li>60</li><li>61</li><li>62</li><li>63</li><li>64</li><li>65</li><li>66</li><li>67</li><li>68</li><li>69</li><li>70</li><li>71</li></ul><ul style="" class="pre-numbering"><li>1</li><li>2</li><li>3</li><li>4</li><li>5</li><li>6</li><li>7</li><li>8</li><li>9</li><li>10</li><li>11</li><li>12</li><li>13</li><li>14</li><li>15</li><li>16</li><li>17</li><li>18</li><li>19</li><li>20</li><li>21</li><li>22</li><li>23</li><li>24</li><li>25</li><li>26</li><li>27</li><li>28</li><li>29</li><li>30</li><li>31</li><li>32</li><li>33</li><li>34</li><li>35</li><li>36</li><li>37</li><li>38</li><li>39</li><li>40</li><li>41</li><li>42</li><li>43</li><li>44</li><li>45</li><li>46</li><li>47</li><li>48</li><li>49</li><li>50</li><li>51</li><li>52</li><li>53</li><li>54</li><li>55</li><li>56</li><li>57</li><li>58</li><li>59</li><li>60</li><li>61</li><li>62</li><li>63</li><li>64</li><li>65</li><li>66</li><li>67</li><li>68</li><li>69</li><li>70</li><li>71</li></ul>
运行效果
从上图可以看出队列模型和PUB/SUB模型的区别,Queue只能由一个消费者接收,其他Queue中的成员无法接受到被已消费的信息,而Topic则可以,只要是订阅了Topic的消费者,全部可以获取到生产者发布的信息。
总结
Spring提供了对JMS的支持,ActiveMQ提供了很好的实现,而此时我们已经将两者完美的结合在了一起。
下篇博文我们实现Spring和ActiveMQ消息的持久化。
转载来自:http://blog.csdn.net/jiuqiyuliang/article/details/48758203