入浅出JMS(三)--ActiveMQ简单的HelloWorld实例

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

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

这篇博文,我们使用ActiveMQ为大家实现一种点对点的消息模型。如果你对点对点模型的认识较浅,可以看一下第一篇博文的介绍。

JMS其实并没有想象的那么高大上,看完这篇博文之后,你就知道什么叫简单,下面直接进入主题。

开发环境

我们使用的是ActiveMQ5.11.1Release的Windows版,官网最新版是ActiveMQ5.12.0Release,大家可以自行下载,下载地址。

需要注意的是,开发时候,要将apache-activemq-5.11.1-bin.zip解压缩后里面的activemq-all-5.11.1.jar包加入到classpath下面,这个包包含了所有jms接口api的实现。

搭建开发环境

建立项目

我们只需要建立一个java项目就可以了,导入jar包,项目截图:

这里写图片描述

点对点的消息模型,只需要一个消息生成者和消息消费者,下面我们编写代码。

编写生产者

<codeclass="language-javahljshas-numbering"style="display:block;padding:0px;color:inherit;box-sizing:border-box;font-family:'SourceCodePro',monospace;font-size:undefined;white-space:pre;border-radius:0px;word-wrap:normal;background:transparent;"><spanclass="hljs-keyword"style="color:rgb(0,0,136);box-sizing:border-box;">package</span>com.tgb.activemq;<spanclass="hljs-keyword"style="color:rgb(0,0,136);box-sizing:border-box;">import</span>javax.jms.Connection;<spanclass="hljs-keyword"style="color:rgb(0,0,136);box-sizing:border-box;">import</span>javax.jms.ConnectionFactory;<spanclass="hljs-keyword"style="color:rgb(0,0,136);box-sizing:border-box;">import</span>javax.jms.Destination;<spanclass="hljs-keyword"style="color:rgb(0,0,136);box-sizing:border-box;">import</span>javax.jms.JMSException;<spanclass="hljs-keyword"style="color:rgb(0,0,136);box-sizing:border-box;">import</span>javax.jms.MessageProducer;<spanclass="hljs-keyword"style="color:rgb(0,0,136);box-sizing:border-box;">import</span>javax.jms.Session;<spanclass="hljs-keyword"style="color:rgb(0,0,136);box-sizing:border-box;">import</span>javax.jms.TextMessage;<spanclass="hljs-keyword"style="color:rgb(0,0,136);box-sizing:border-box;">import</span>org.apache.activemq.ActiveMQConnection;<spanclass="hljs-keyword"style="color:rgb(0,0,136);box-sizing:border-box;">import</span>org.apache.activemq.ActiveMQConnectionFactory;<spanclass="hljs-javadoc"style="color:rgb(136,0,0);box-sizing:border-box;">/***消息的生产者(发送者)*<spanclass="hljs-javadoctag"style="color:rgb(102,0,102);box-sizing:border-box;">@author</span>liang**/</span><spanclass="hljs-keyword"style="color:rgb(0,0,136);box-sizing:border-box;">public</span><spanclass="hljs-class"style="box-sizing:border-box;"><spanclass="hljs-keyword"style="color:rgb(0,0,136);box-sizing:border-box;">class</span><spanclass="hljs-title"style="box-sizing:border-box;color:rgb(102,0,102);">JMSProducer</span>{</span><spanclass="hljs-comment"style="color:rgb(136,0,0);box-sizing:border-box;">//默认连接用户名</span><spanclass="hljs-keyword"style="color:rgb(0,0,136);box-sizing:border-box;">private</span><spanclass="hljs-keyword"style="color:rgb(0,0,136);box-sizing:border-box;">static</span><spanclass="hljs-keyword"style="color:rgb(0,0,136);box-sizing:border-box;">final</span>StringUSERNAME=ActiveMQConnection.DEFAULT_USER;<spanclass="hljs-comment"style="color:rgb(136,0,0);box-sizing:border-box;">//默认连接密码</span><spanclass="hljs-keyword"style="color:rgb(0,0,136);box-sizing:border-box;">private</span><spanclass="hljs-keyword"style="color:rgb(0,0,136);box-sizing:border-box;">static</span><spanclass="hljs-keyword"style="color:rgb(0,0,136);box-sizing:border-box;">final</span>StringPASSWORD=ActiveMQConnection.DEFAULT_PASSWORD;<spanclass="hljs-comment"style="color:rgb(136,0,0);box-sizing:border-box;">//默认连接地址</span><spanclass="hljs-keyword"style="color:rgb(0,0,136);box-sizing:border-box;">private</span><spanclass="hljs-keyword"style="color:rgb(0,0,136);box-sizing:border-box;">static</span><spanclass="hljs-keyword"style="color:rgb(0,0,136);box-sizing:border-box;">final</span>StringBROKEURL=ActiveMQConnection.DEFAULT_BROKER_URL;<spanclass="hljs-comment"style="color:rgb(136,0,0);box-sizing:border-box;">//发送的消息数量</span><spanclass="hljs-keyword"style="color:rgb(0,0,136);box-sizing:border-box;">private</span><spanclass="hljs-keyword"style="color:rgb(0,0,136);box-sizing:border-box;">static</span><spanclass="hljs-keyword"style="color:rgb(0,0,136);box-sizing:border-box;">final</span><spanclass="hljs-keyword"style="color:rgb(0,0,136);box-sizing:border-box;">int</span>SENDNUM=<spanclass="hljs-number"style="color:rgb(0,102,102);box-sizing:border-box;">10</span>;<spanclass="hljs-keyword"style="color:rgb(0,0,136);box-sizing:border-box;">public</span><spanclass="hljs-keyword"style="color:rgb(0,0,136);box-sizing:border-box;">static</span><spanclass="hljs-keyword"style="color:rgb(0,0,136);box-sizing:border-box;">void</span><spanclass="hljs-title"style="box-sizing:border-box;">main</span>(String[]args){<spanclass="hljs-comment"style="color:rgb(136,0,0);box-sizing:border-box;">//连接工厂</span>ConnectionFactoryconnectionFactory;<spanclass="hljs-comment"style="color:rgb(136,0,0);box-sizing:border-box;">//连接</span>Connectionconnection=<spanclass="hljs-keyword"style="color:rgb(0,0,136);box-sizing:border-box;">null</span>;<spanclass="hljs-comment"style="color:rgb(136,0,0);box-sizing:border-box;">//会话接受或者发送消息的线程</span>Sessionsession;<spanclass="hljs-comment"style="color:rgb(136,0,0);box-sizing:border-box;">//消息的目的地</span>Destinationdestination;<spanclass="hljs-comment"style="color:rgb(136,0,0);box-sizing:border-box;">//消息生产者</span>MessageProducermessageProducer;<spanclass="hljs-comment"style="color:rgb(136,0,0);box-sizing:border-box;">//实例化连接工厂</span>connectionFactory=<spanclass="hljs-keyword"style="color:rgb(0,0,136);box-sizing:border-box;">new</span>ActiveMQConnectionFactory(JMSProducer.USERNAME,JMSProducer.PASSWORD,JMSProducer.BROKEURL);<spanclass="hljs-keyword"style="color:rgb(0,0,136);box-sizing:border-box;">try</span>{<spanclass="hljs-comment"style="color:rgb(136,0,0);box-sizing:border-box;">//通过连接工厂获取连接</span>connection=connectionFactory.createConnection();<spanclass="hljs-comment"style="color:rgb(136,0,0);box-sizing:border-box;">//启动连接</span>connection.start();<spanclass="hljs-comment"style="color:rgb(136,0,0);box-sizing:border-box;">//创建session</span>session=connection.createSession(<spanclass="hljs-keyword"style="color:rgb(0,0,136);box-sizing:border-box;">true</span>,Session.AUTO_ACKNOWLEDGE);<spanclass="hljs-comment"style="color:rgb(136,0,0);box-sizing:border-box;">//创建一个名称为HelloWorld的消息队列</span>destination=session.createQueue(<spanclass="hljs-string"style="color:rgb(0,136,0);box-sizing:border-box;">"HelloWorld"</span>);<spanclass="hljs-comment"style="color:rgb(136,0,0);box-sizing:border-box;">//创建消息生产者</span>messageProducer=session.createProducer(destination);<spanclass="hljs-comment"style="color:rgb(136,0,0);box-sizing:border-box;">//发送消息</span>sendMessage(session,messageProducer);session.commit();}<spanclass="hljs-keyword"style="color:rgb(0,0,136);box-sizing:border-box;">catch</span>(Exceptione){e.printStackTrace();}<spanclass="hljs-keyword"style="color:rgb(0,0,136);box-sizing:border-box;">finally</span>{<spanclass="hljs-keyword"style="color:rgb(0,0,136);box-sizing:border-box;">if</span>(connection!=<spanclass="hljs-keyword"style="color:rgb(0,0,136);box-sizing:border-box;">null</span>){<spanclass="hljs-keyword"style="color:rgb(0,0,136);box-sizing:border-box;">try</span>{connection.close();}<spanclass="hljs-keyword"style="color:rgb(0,0,136);box-sizing:border-box;">catch</span>(JMSExceptione){e.printStackTrace();}}}}<spanclass="hljs-javadoc"style="color:rgb(136,0,0);box-sizing:border-box;">/***发送消息*<spanclass="hljs-javadoctag"style="color:rgb(102,0,102);box-sizing:border-box;">@param</span>session*<spanclass="hljs-javadoctag"style="color:rgb(102,0,102);box-sizing:border-box;">@param</span>messageProducer消息生产者*<spanclass="hljs-javadoctag"style="color:rgb(102,0,102);box-sizing:border-box;">@throws</span>Exception*/</span><spanclass="hljs-keyword"style="color:rgb(0,0,136);box-sizing:border-box;">public</span><spanclass="hljs-keyword"style="color:rgb(0,0,136);box-sizing:border-box;">static</span><spanclass="hljs-keyword"style="color:rgb(0,0,136);box-sizing:border-box;">void</span><spanclass="hljs-title"style="box-sizing:border-box;">sendMessage</span>(Sessionsession,MessageProducermessageProducer)<spanclass="hljs-keyword"style="color:rgb(0,0,136);box-sizing:border-box;">throws</span>Exception{<spanclass="hljs-keyword"style="color:rgb(0,0,136);box-sizing:border-box;">for</span>(<spanclass="hljs-keyword"style="color:rgb(0,0,136);box-sizing:border-box;">int</span>i=<spanclass="hljs-number"style="color:rgb(0,102,102);box-sizing:border-box;">0</span>;i<JMSProducer.SENDNUM;i++){<spanclass="hljs-comment"style="color:rgb(136,0,0);box-sizing:border-box;">//创建一条文本消息</span>TextMessagemessage=session.createTextMessage(<spanclass="hljs-string"style="color:rgb(0,136,0);box-sizing:border-box;">"ActiveMQ发送消息"</span>+i);System.out.println(<spanclass="hljs-string"style="color:rgb(0,136,0);box-sizing:border-box;">"发送消息:Activemq发送消息"</span>+i);<spanclass="hljs-comment"style="color:rgb(136,0,0);box-sizing:border-box;">//通过消息生产者发出消息</span>messageProducer.send(message);}}}</code><ulclass="pre-numbering"style="box-sizing:border-box;position:absolute;width:50px;top:0px;left:0px;margin:0px;padding:6px0px40px;border-right-width:1px;border-right-style:solid;border-right-color:rgb(221,221,221);list-style:none;text-align:right;background-color:rgb(238,238,238);"><listyle="box-sizing:border-box;padding:0px5px;">1</li><listyle="box-sizing:border-box;padding:0px5px;">2</li><listyle="box-sizing:border-box;padding:0px5px;">3</li><listyle="box-sizing:border-box;padding:0px5px;">4</li><listyle="box-sizing:border-box;padding:0px5px;">5</li><listyle="box-sizing:border-box;padding:0px5px;">6</li><listyle="box-sizing:border-box;padding:0px5px;">7</li><listyle="box-sizing:border-box;padding:0px5px;">8</li><listyle="box-sizing:border-box;padding:0px5px;">9</li><listyle="box-sizing:border-box;padding:0px5px;">10</li><listyle="box-sizing:border-box;padding:0px5px;">11</li><listyle="box-sizing:border-box;padding:0px5px;">12</li><listyle="box-sizing:border-box;padding:0px5px;">13</li><listyle="box-sizing:border-box;padding:0px5px;">14</li><listyle="box-sizing:border-box;padding:0px5px;">15</li><listyle="box-sizing:border-box;padding:0px5px;">16</li><listyle="box-sizing:border-box;padding:0px5px;">17</li><listyle="box-sizing:border-box;padding:0px5px;">18</li><listyle="box-sizing:border-box;padding:0px5px;">19</li><listyle="box-sizing:border-box;padding:0px5px;">20</li><listyle="box-sizing:border-box;padding:0px5px;">21</li><listyle="box-sizing:border-box;padding:0px5px;">22</li><listyle="box-sizing:border-box;padding:0px5px;">23</li><listyle="box-sizing:border-box;padding:0px5px;">24</li><listyle="box-sizing:border-box;padding:0px5px;">25</li><listyle="box-sizing:border-box;padding:0px5px;">26</li><listyle="box-sizing:border-box;padding:0px5px;">27</li><listyle="box-sizing:border-box;padding:0px5px;">28</li><listyle="box-sizing:border-box;padding:0px5px;">29</li><listyle="box-sizing:border-box;padding:0px5px;">30</li><listyle="box-sizing:border-box;padding:0px5px;">31</li><listyle="box-sizing:border-box;padding:0px5px;">32</li><listyle="box-sizing:border-box;padding:0px5px;">33</li><listyle="box-sizing:border-box;padding:0px5px;">34</li><listyle="box-sizing:border-box;padding:0px5px;">35</li><listyle="box-sizing:border-box;padding:0px5px;">36</li><listyle="box-sizing:border-box;padding:0px5px;">37</li><listyle="box-sizing:border-box;padding:0px5px;">38</li><listyle="box-sizing:border-box;padding:0px5px;">39</li><listyle="box-sizing:border-box;padding:0px5px;">40</li><listyle="box-sizing:border-box;padding:0px5px;">41</li><listyle="box-sizing:border-box;padding:0px5px;">42</li><listyle="box-sizing:border-box;padding:0px5px;">43</li><listyle="box-sizing:border-box;padding:0px5px;">44</li><listyle="box-sizing:border-box;padding:0px5px;">45</li><listyle="box-sizing:border-box;padding:0px5px;">46</li><listyle="box-sizing:border-box;padding:0px5px;">47</li><listyle="box-sizing:border-box;padding:0px5px;">48</li><listyle="box-sizing:border-box;padding:0px5px;">49</li><listyle="box-sizing:border-box;padding:0px5px;">50</li><listyle="box-sizing:border-box;padding:0px5px;">51</li><listyle="box-sizing:border-box;padding:0px5px;">52</li><listyle="box-sizing:border-box;padding:0px5px;">53</li><listyle="box-sizing:border-box;padding:0px5px;">54</li><listyle="box-sizing:border-box;padding:0px5px;">55</li><listyle="box-sizing:border-box;padding:0px5px;">56</li><listyle="box-sizing:border-box;padding:0px5px;">57</li><listyle="box-sizing:border-box;padding:0px5px;">58</li><listyle="box-sizing:border-box;padding:0px5px;">59</li><listyle="box-sizing:border-box;padding:0px5px;">60</li><listyle="box-sizing:border-box;padding:0px5px;">61</li><listyle="box-sizing:border-box;padding:0px5px;">62</li><listyle="box-sizing:border-box;padding:0px5px;">63</li><listyle="box-sizing:border-box;padding:0px5px;">64</li><listyle="box-sizing:border-box;padding:0px5px;">65</li><listyle="box-sizing:border-box;padding:0px5px;">66</li><listyle="box-sizing:border-box;padding:0px5px;">67</li><listyle="box-sizing:border-box;padding:0px5px;">68</li><listyle="box-sizing:border-box;padding:0px5px;">69</li><listyle="box-sizing:border-box;padding:0px5px;">70</li><listyle="box-sizing:border-box;padding:0px5px;">71</li><listyle="box-sizing:border-box;padding:0px5px;">72</li><listyle="box-sizing:border-box;padding:0px5px;">73</li><listyle="box-sizing:border-box;padding:0px5px;">74</li><listyle="box-sizing:border-box;padding:0px5px;">75</li><listyle="box-sizing:border-box;padding:0px5px;">76</li><listyle="box-sizing:border-box;padding:0px5px;">77</li><listyle="box-sizing:border-box;padding:0px5px;">78</li><listyle="box-sizing:border-box;padding:0px5px;">79</li><listyle="box-sizing:border-box;padding:0px5px;">80</li><listyle="box-sizing:border-box;padding:0px5px;">81</li><listyle="box-sizing:border-box;padding:0px5px;">82</li><listyle="box-sizing:border-box;padding:0px5px;">83</li><listyle="box-sizing:border-box;padding:0px5px;">84</li><listyle="box-sizing:border-box;padding:0px5px;">85</li><listyle="box-sizing:border-box;padding:0px5px;">86</li><listyle="box-sizing:border-box;padding:0px5px;">87</li><listyle="box-sizing:border-box;padding:0px5px;">88</li><listyle="box-sizing:border-box;padding:0px5px;">89</li><listyle="box-sizing:border-box;padding:0px5px;">90</li></ul>

编写消费者

<codeclass="language-javahljshas-numbering"style="display:block;padding:0px;color:inherit;box-sizing:border-box;font-family:'SourceCodePro',monospace;font-size:undefined;white-space:pre;border-radius:0px;word-wrap:normal;background:transparent;"><spanclass="hljs-keyword"style="color:rgb(0,0,136);box-sizing:border-box;">package</span>com.tgb.activemq;<spanclass="hljs-keyword"style="color:rgb(0,0,136);box-sizing:border-box;">import</span>javax.jms.Connection;<spanclass="hljs-keyword"style="color:rgb(0,0,136);box-sizing:border-box;">import</span>javax.jms.ConnectionFactory;<spanclass="hljs-keyword"style="color:rgb(0,0,136);box-sizing:border-box;">import</span>javax.jms.Destination;<spanclass="hljs-keyword"style="color:rgb(0,0,136);box-sizing:border-box;">import</span>javax.jms.JMSException;<spanclass="hljs-keyword"style="color:rgb(0,0,136);box-sizing:border-box;">import</span>javax.jms.MessageConsumer;<spanclass="hljs-keyword"style="color:rgb(0,0,136);box-sizing:border-box;">import</span>javax.jms.Session;<spanclass="hljs-keyword"style="color:rgb(0,0,136);box-sizing:border-box;">import</span>javax.jms.TextMessage;<spanclass="hljs-keyword"style="color:rgb(0,0,136);box-sizing:border-box;">import</span>org.apache.activemq.ActiveMQConnection;<spanclass="hljs-keyword"style="color:rgb(0,0,136);box-sizing:border-box;">import</span>org.apache.activemq.ActiveMQConnectionFactory;<spanclass="hljs-javadoc"style="color:rgb(136,0,0);box-sizing:border-box;">/***消息的消费者(接受者)*<spanclass="hljs-javadoctag"style="color:rgb(102,0,102);box-sizing:border-box;">@author</span>liang**/</span><spanclass="hljs-keyword"style="color:rgb(0,0,136);box-sizing:border-box;">public</span><spanclass="hljs-class"style="box-sizing:border-box;"><spanclass="hljs-keyword"style="color:rgb(0,0,136);box-sizing:border-box;">class</span><spanclass="hljs-title"style="box-sizing:border-box;color:rgb(102,0,102);">JMSConsumer</span>{</span><spanclass="hljs-keyword"style="color:rgb(0,0,136);box-sizing:border-box;">private</span><spanclass="hljs-keyword"style="color:rgb(0,0,136);box-sizing:border-box;">static</span><spanclass="hljs-keyword"style="color:rgb(0,0,136);box-sizing:border-box;">final</span>StringUSERNAME=ActiveMQConnection.DEFAULT_USER;<spanclass="hljs-comment"style="color:rgb(136,0,0);box-sizing:border-box;">//默认连接用户名</span><spanclass="hljs-keyword"style="color:rgb(0,0,136);box-sizing:border-box;">private</span><spanclass="hljs-keyword"style="color:rgb(0,0,136);box-sizing:border-box;">static</span><spanclass="hljs-keyword"style="color:rgb(0,0,136);box-sizing:border-box;">final</span>StringPASSWORD=ActiveMQConnection.DEFAULT_PASSWORD;<spanclass="hljs-comment"style="color:rgb(136,0,0);box-sizing:border-box;">//默认连接密码</span><spanclass="hljs-keyword"style="color:rgb(0,0,136);box-sizing:border-box;">private</span><spanclass="hljs-keyword"style="color:rgb(0,0,136);box-sizing:border-box;">static</span><spanclass="hljs-keyword"style="color:rgb(0,0,136);box-sizing:border-box;">final</span>StringBROKEURL=ActiveMQConnection.DEFAULT_BROKER_URL;<spanclass="hljs-comment"style="color:rgb(136,0,0);box-sizing:border-box;">//默认连接地址</span><spanclass="hljs-keyword"style="color:rgb(0,0,136);box-sizing:border-box;">public</span><spanclass="hljs-keyword"style="color:rgb(0,0,136);box-sizing:border-box;">static</span><spanclass="hljs-keyword"style="color:rgb(0,0,136);box-sizing:border-box;">void</span><spanclass="hljs-title"style="box-sizing:border-box;">main</span>(String[]args){ConnectionFactoryconnectionFactory;<spanclass="hljs-comment"style="color:rgb(136,0,0);box-sizing:border-box;">//连接工厂</span>Connectionconnection=<spanclass="hljs-keyword"style="color:rgb(0,0,136);box-sizing:border-box;">null</span>;<spanclass="hljs-comment"style="color:rgb(136,0,0);box-sizing:border-box;">//连接</span>Sessionsession;<spanclass="hljs-comment"style="color:rgb(136,0,0);box-sizing:border-box;">//会话接受或者发送消息的线程</span>Destinationdestination;<spanclass="hljs-comment"style="color:rgb(136,0,0);box-sizing:border-box;">//消息的目的地</span>MessageConsumermessageConsumer;<spanclass="hljs-comment"style="color:rgb(136,0,0);box-sizing:border-box;">//消息的消费者</span><spanclass="hljs-comment"style="color:rgb(136,0,0);box-sizing:border-box;">//实例化连接工厂</span>connectionFactory=<spanclass="hljs-keyword"style="color:rgb(0,0,136);box-sizing:border-box;">new</span>ActiveMQConnectionFactory(JMSConsumer.USERNAME,JMSConsumer.PASSWORD,JMSConsumer.BROKEURL);<spanclass="hljs-keyword"style="color:rgb(0,0,136);box-sizing:border-box;">try</span>{<spanclass="hljs-comment"style="color:rgb(136,0,0);box-sizing:border-box;">//通过连接工厂获取连接</span>connection=connectionFactory.createConnection();<spanclass="hljs-comment"style="color:rgb(136,0,0);box-sizing:border-box;">//启动连接</span>connection.start();<spanclass="hljs-comment"style="color:rgb(136,0,0);box-sizing:border-box;">//创建session</span>session=connection.createSession(<spanclass="hljs-keyword"style="color:rgb(0,0,136);box-sizing:border-box;">false</span>,Session.AUTO_ACKNOWLEDGE);<spanclass="hljs-comment"style="color:rgb(136,0,0);box-sizing:border-box;">//创建一个连接HelloWorld的消息队列</span>destination=session.createQueue(<spanclass="hljs-string"style="color:rgb(0,136,0);box-sizing:border-box;">"HelloWorld"</span>);<spanclass="hljs-comment"style="color:rgb(136,0,0);box-sizing:border-box;">//创建消息消费者</span>messageConsumer=session.createConsumer(destination);<spanclass="hljs-keyword"style="color:rgb(0,0,136);box-sizing:border-box;">while</span>(<spanclass="hljs-keyword"style="color:rgb(0,0,136);box-sizing:border-box;">true</span>){TextMessagetextMessage=(TextMessage)messageConsumer.receive(<spanclass="hljs-number"style="color:rgb(0,102,102);box-sizing:border-box;">100000</span>);<spanclass="hljs-keyword"style="color:rgb(0,0,136);box-sizing:border-box;">if</span>(textMessage!=<spanclass="hljs-keyword"style="color:rgb(0,0,136);box-sizing:border-box;">null</span>){System.out.println(<spanclass="hljs-string"style="color:rgb(0,136,0);box-sizing:border-box;">"收到的消息:"</span>+textMessage.getText());}<spanclass="hljs-keyword"style="color:rgb(0,0,136);box-sizing:border-box;">else</span>{<spanclass="hljs-keyword"style="color:rgb(0,0,136);box-sizing:border-box;">break</span>;}}}<spanclass="hljs-keyword"style="color:rgb(0,0,136);box-sizing:border-box;">catch</span>(JMSExceptione){e.printStackTrace();}}}</code><ulclass="pre-numbering"style="box-sizing:border-box;position:absolute;width:50px;top:0px;left:0px;margin:0px;padding:6px0px40px;border-right-width:1px;border-right-style:solid;border-right-color:rgb(221,221,221);list-style:none;text-align:right;background-color:rgb(238,238,238);"><listyle="box-sizing:border-box;padding:0px5px;">1</li><listyle="box-sizing:border-box;padding:0px5px;">2</li><listyle="box-sizing:border-box;padding:0px5px;">3</li><listyle="box-sizing:border-box;padding:0px5px;">4</li><listyle="box-sizing:border-box;padding:0px5px;">5</li><listyle="box-sizing:border-box;padding:0px5px;">6</li><listyle="box-sizing:border-box;padding:0px5px;">7</li><listyle="box-sizing:border-box;padding:0px5px;">8</li><listyle="box-sizing:border-box;padding:0px5px;">9</li><listyle="box-sizing:border-box;padding:0px5px;">10</li><listyle="box-sizing:border-box;padding:0px5px;">11</li><listyle="box-sizing:border-box;padding:0px5px;">12</li><listyle="box-sizing:border-box;padding:0px5px;">13</li><listyle="box-sizing:border-box;padding:0px5px;">14</li><listyle="box-sizing:border-box;padding:0px5px;">15</li><listyle="box-sizing:border-box;padding:0px5px;">16</li><listyle="box-sizing:border-box;padding:0px5px;">17</li><listyle="box-sizing:border-box;padding:0px5px;">18</li><listyle="box-sizing:border-box;padding:0px5px;">19</li><listyle="box-sizing:border-box;padding:0px5px;">20</li><listyle="box-sizing:border-box;padding:0px5px;">21</li><listyle="box-sizing:border-box;padding:0px5px;">22</li><listyle="box-sizing:border-box;padding:0px5px;">23</li><listyle="box-sizing:border-box;padding:0px5px;">24</li><listyle="box-sizing:border-box;padding:0px5px;">25</li><listyle="box-sizing:border-box;padding:0px5px;">26</li><listyle="box-sizing:border-box;padding:0px5px;">27</li><listyle="box-sizing:border-box;padding:0px5px;">28</li><listyle="box-sizing:border-box;padding:0px5px;">29</li><listyle="box-sizing:border-box;padding:0px5px;">30</li><listyle="box-sizing:border-box;padding:0px5px;">31</li><listyle="box-sizing:border-box;padding:0px5px;">32</li><listyle="box-sizing:border-box;padding:0px5px;">33</li><listyle="box-sizing:border-box;padding:0px5px;">34</li><listyle="box-sizing:border-box;padding:0px5px;">35</li><listyle="box-sizing:border-box;padding:0px5px;">36</li><listyle="box-sizing:border-box;padding:0px5px;">37</li><listyle="box-sizing:border-box;padding:0px5px;">38</li><listyle="box-sizing:border-box;padding:0px5px;">39</li><listyle="box-sizing:border-box;padding:0px5px;">40</li><listyle="box-sizing:border-box;padding:0px5px;">41</li><listyle="box-sizing:border-box;padding:0px5px;">42</li><listyle="box-sizing:border-box;padding:0px5px;">43</li><listyle="box-sizing:border-box;padding:0px5px;">44</li><listyle="box-sizing:border-box;padding:0px5px;">45</li><listyle="box-sizing:border-box;padding:0px5px;">46</li><listyle="box-sizing:border-box;padding:0px5px;">47</li><listyle="box-sizing:border-box;padding:0px5px;">48</li><listyle="box-sizing:border-box;padding:0px5px;">49</li><listyle="box-sizing:border-box;padding:0px5px;">50</li><listyle="box-sizing:border-box;padding:0px5px;">51</li><listyle="box-sizing:border-box;padding:0px5px;">52</li><listyle="box-sizing:border-box;padding:0px5px;">53</li><listyle="box-sizing:border-box;padding:0px5px;">54</li><listyle="box-sizing:border-box;padding:0px5px;">55</li><listyle="box-sizing:border-box;padding:0px5px;">56</li><listyle="box-sizing:border-box;padding:0px5px;">57</li><listyle="box-sizing:border-box;padding:0px5px;">58</li><listyle="box-sizing:border-box;padding:0px5px;">59</li><listyle="box-sizing:border-box;padding:0px5px;">60</li><listyle="box-sizing:border-box;padding:0px5px;">61</li><listyle="box-sizing:border-box;padding:0px5px;">62</li><listyle="box-sizing:border-box;padding:0px5px;">63</li><listyle="box-sizing:border-box;padding:0px5px;">64</li><listyle="box-sizing:border-box;padding:0px5px;">65</li></ul>

运行

首先,启动ActiveMQ,如何启动ActiveMQ如何启动,请看第二篇博文。在浏览器中输入:http://localhost:8161/admin/,然后开始执行:

运行发送者,eclipse控制台输出,如下图:

这里写图片描述

此时,我们先看一下ActiveMQ服务器,Queues内容如下:

这里写图片描述

我们可以看到创建了一个名称为HelloWorld的消息队列,队列中有10条消息未被消费,我们也可以通过Browse查看是哪些消息,如下图:

这里写图片描述

如果这些队列中的消息,被删除,消费者则无法消费。

我们继续运行一下消费者,eclipse控制台打印消息,如下:

这里写图片描述

此时,我们先看一下ActiveMQ服务器,Queues内容如下:

这里写图片描述

我们可以看到HelloWorld的消息队列发生变化,多一个消息者,队列中的10条消息被消费了,点击Browse查看,已经为空了。

点击ActiveConsumers,我们可以看到这个消费者的详细信息:

这里写图片描述

我们的实例到此就结束了,大家可以自己多点ActiveMQ服务器的内容,进一步熟悉ActiveMQ。

总结

这篇博文我们实现了点对点的消息模型以及发送的一个同步消息,是不是非常的简单?

下面博文,我们将实现一个ActiveMQ和spring整合的实例。

相关推荐