﻿<?xml version="1.0" encoding="utf-8" standalone="yes"?><rss version="2.0" xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:trackback="http://madskills.com/public/xml/rss/module/trackback/" xmlns:wfw="http://wellformedweb.org/CommentAPI/" xmlns:slash="http://purl.org/rss/1.0/modules/slash/"><channel><title>BlogJava-少年阿宾-随笔分类-ActiveMQ</title><link>http://www.blogjava.net/stevenjohn/category/52113.html</link><description>那些青春的岁月</description><language>zh-cn</language><lastBuildDate>Sun, 26 Aug 2012 19:19:15 GMT</lastBuildDate><pubDate>Sun, 26 Aug 2012 19:19:15 GMT</pubDate><ttl>60</ttl><item><title>Apache ActiveMQ or Spring 使用方法</title><link>http://www.blogjava.net/stevenjohn/archive/2012/08/26/386311.html</link><dc:creator>abin</dc:creator><author>abin</author><pubDate>Sun, 26 Aug 2012 12:18:00 GMT</pubDate><guid>http://www.blogjava.net/stevenjohn/archive/2012/08/26/386311.html</guid><wfw:comment>http://www.blogjava.net/stevenjohn/comments/386311.html</wfw:comment><comments>http://www.blogjava.net/stevenjohn/archive/2012/08/26/386311.html#Feedback</comments><slash:comments>0</slash:comments><wfw:commentRss>http://www.blogjava.net/stevenjohn/comments/commentRss/386311.html</wfw:commentRss><trackback:ping>http://www.blogjava.net/stevenjohn/services/trackbacks/386311.html</trackback:ping><description><![CDATA[<p style="line-height: normal; color: #333333; font-family: Arial; background-color: #ffffff; ">Apache ActiveMQ简介： MQ是Apache出品的一个基于JMS消息处理机制的服务，可以很容易的潜入到Spring中，作为项目消息处理服务。</p><p style="line-height: normal; color: #333333; font-family: Arial; background-color: #ffffff; ">Apache ActiveMQ用途： jms有两种消息机制一种是消息队列，一种是消息订阅，简单来说就是一对一，和一对多。</p><p style="line-height: normal; color: #333333; font-family: Arial; background-color: #ffffff; ">一对一可以看做 一组消息对应一个 或 多个消息获取服务，其中每一个单独的消息体都只会被一个消息获取服务获取。</p><p style="line-height: normal; color: #333333; font-family: Arial; background-color: #ffffff; ">一对多可以看做 一组消息对应一个 或 多个接收服务，其中每一个单独的消息体发出之后都会被所有的接收服务收到一次。</p><p style="line-height: normal; color: #333333; font-family: Arial; background-color: #ffffff; ">根据以上两种模式我们可以在 发送短信的时候使用 消息队列模式。也可以在分布式的时候使用消息队列模式</p><p style="line-height: normal; color: #333333; font-family: Arial; background-color: #ffffff; ">消息订阅模式用的不是很多但是，在分布式中也可以应用同时可以结合WebService进行使用。例如一个新闻需要发给多个系统内的单独服务。</p><p style="line-height: normal; color: #333333; font-family: Arial; background-color: #ffffff; ">只要深刻理解了他的处理机制在很多情况下都可以应用，如果实在不喜欢这种方式也可以遵照java JMS的API用线程自己模拟消息队列机制。</p><p style="line-height: normal; color: #333333; font-family: Arial; background-color: #ffffff; ">我个人认为区别不是很大。更何况很多时候MQ的很多功能我们有没有用到，用这么个大家伙就是为了图省事。</p><p style="line-height: normal; color: #333333; font-family: Arial; background-color: #ffffff; "><span style="color: #ff0000; ">这里备注一下：按原有设定在创建连接的时候MQ是不发送消息的。</span></p><p style="line-height: normal; color: #333333; font-family: Arial; background-color: #ffffff; ">下面我说以下使用方式：</p><p style="line-height: normal; color: #333333; font-family: Arial; background-color: #ffffff; ">本人目前只会JAVA</p><p style="line-height: normal; color: #333333; font-family: Arial; background-color: #ffffff; ">需要的包为</p><p style="line-height: normal; color: #333333; font-family: Arial; background-color: #ffffff; ">&nbsp;&nbsp; &nbsp;&nbsp;&nbsp;&nbsp; activemq-all-5.2.0.jar<br />&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;log4j-1.2.14.jar<br />&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;spring-beans-2.5.5.jar<br />&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;spring-context-2.5.5.jar<br />&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;spring-jms-2.5.5.jar</p><p style="line-height: normal; color: #333333; font-family: Arial; background-color: #ffffff; ">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; spring-core-2.5.5.jar</p><p style="line-height: normal; color: #333333; font-family: Arial; background-color: #ffffff; ">这些应该就足够了</p><p style="line-height: normal; color: #333333; font-family: Arial; background-color: #ffffff; ">我做测试的时候用的包为</p><p style="line-height: normal; color: #333333; font-family: Arial; background-color: #ffffff; ">&nbsp;&nbsp; &nbsp;&nbsp;&nbsp;&nbsp; activemq-all-5.2.0.jar<br />&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;commons-pool-1.4.jar<br />&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;log4j-1.2.14.jar<br />&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;spring-beans-2.5.5.jar<br />&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;spring-context-2.5.5.jar<br />&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;spring-core-2.5.5.jar<br />&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;spring-jms-2.5.5.jar<br />&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;spring-tx-2.5.5.jar<br />&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;spring-web-2.5.5.jar<br />&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;spring-webmvc-2.5.5.jar<br />&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;xbean-spring-3.4.jar<br />&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;msbase.jar<br />&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;mssqlserver.jar<br />&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;msutil.jar<br />&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;mysql-connector-java-5.0.4-bin.jar<br />&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;mysql.jar</p><p style="line-height: normal; color: #333333; font-family: Arial; background-color: #ffffff; ">&nbsp;</p><p style="line-height: normal; color: #333333; font-family: Arial; background-color: #ffffff; ">基本配置</p><p style="line-height: normal; color: #333333; font-family: Arial; background-color: #ffffff; ">下载 Apache ActiveMQ 不知道的googlean</p><p style="line-height: normal; color: #333333; font-family: Arial; background-color: #ffffff; ">安装服务的文件在</p><p style="line-height: normal; color: #333333; font-family: Arial; background-color: #ffffff; ">apache-activemq-5.5.0\bin\win32 目录下面Linux版也一样大同小异 运行.sh文件就好，如果出现问题请打开运行文件查看里面参数</p><p style="line-height: normal; color: #333333; font-family: Arial; background-color: #ffffff; ">activemq.bat直接运行启动服务</p><p style="line-height: normal; color: #333333; font-family: Arial; background-color: #ffffff; ">InstallService.bat安装成windos服务</p><p style="line-height: normal; color: #333333; font-family: Arial; background-color: #ffffff; ">UninstallService.bat卸载windos服务</p><p style="line-height: normal; color: #333333; font-family: Arial; background-color: #ffffff; ">安装成功后</p><p style="line-height: normal; color: #333333; font-family: Arial; background-color: #ffffff; ">在运行中 cmd</p><p style="line-height: normal; color: #333333; font-family: Arial; background-color: #ffffff; ">netstat -an|find &#8220;61616&#8221; 查询端口是否已经打开&nbsp;</p><p style="line-height: normal; color: #333333; font-family: Arial; background-color: #ffffff; ">D:\apache-activemq-5.5.0\conf\activemq.xml 本文件为服务配置文件</p><p style="line-height: normal; color: #333333; font-family: Arial; background-color: #ffffff; ">&nbsp;&nbsp;&nbsp;&nbsp; &lt;transportConnectors&gt;<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; &lt;transportConnector name="openwire" uri="tcp://0.0.0.0:61618"/&gt;<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; &lt;/transportConnectors&gt;</p><p style="line-height: normal; color: #333333; font-family: Arial; background-color: #ffffff; ">这一项为端口设置</p><p style="line-height: normal; color: #333333; font-family: Arial; background-color: #ffffff; ">默认为 &#8220;61616&#8221;</p><p style="line-height: normal; color: #333333; font-family: Arial; background-color: #ffffff; ">目前以windos为例</p><p style="line-height: normal; color: #333333; font-family: Arial; background-color: #ffffff; ">把spring和项目联系起来</p><p style="line-height: normal; color: #333333; font-family: Arial; background-color: #ffffff; ">第一步为web.xml</p><p style="line-height: normal; color: #333333; font-family: Arial; background-color: #ffffff; ">&lt;?xml version="1.0" encoding="UTF-8"?&gt;<br />&lt;web-app version="2.4" xmlns="http://java.sun.com/xml/ns/j2ee"<br />&nbsp;&nbsp; &nbsp;xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"<br />&nbsp;&nbsp; &nbsp;xsi:schemaLocation="http://java.sun.com/xml/ns/j2ee&nbsp;<br />&nbsp;&nbsp; &nbsp;http://java.sun.com/xml/ns/j2ee/web-app_2_4.xsd"&gt;</p><p style="line-height: normal; color: #333333; font-family: Arial; background-color: #ffffff; ">&lt;!-- 配置Spring--&gt;<br />&nbsp;&nbsp; &nbsp;&lt;context-param&gt;<br />&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&lt;param-name&gt;contextConfigLocation&lt;/param-name&gt;<br />&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&lt;param-value&gt;<br />&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;/WEB-INF/classes/NewFileMQ.xml<br />&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&lt;/param-value&gt;<br />&nbsp;&nbsp; &nbsp;&lt;/context-param&gt;<br />&nbsp;&nbsp; &nbsp;&lt;listener&gt;<br />&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&lt;listener-class&gt;<br />&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;org.springframework.web.context.ContextLoaderListener<br />&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&lt;/listener-class&gt;<br />&nbsp;&nbsp; &nbsp;&lt;/listener&gt;</p><p style="line-height: normal; color: #333333; font-family: Arial; background-color: #ffffff; "><br />&nbsp; &lt;welcome-file-list&gt;<br />&nbsp;&nbsp;&nbsp; &lt;welcome-file&gt;index.jsp&lt;/welcome-file&gt;<br />&nbsp; &lt;/welcome-file-list&gt;<br />&nbsp;&nbsp; &nbsp;<br />&lt;/web-app&gt;</p><p style="line-height: normal; color: #333333; font-family: Arial; background-color: #ffffff; ">先说 一下消息队列（也就是一对一）</p><p style="line-height: normal; color: #333333; font-family: Arial; background-color: #ffffff; ">NewFileMQ.xml</p><p style="line-height: normal; color: #333333; font-family: Arial; background-color: #ffffff; ">&lt;?xml version="1.0" encoding="UTF-8"?&gt;<br />&lt;beans xmlns="http://www.springframework.org/schema/beans"<br />&nbsp;&nbsp; &nbsp;xmlns:amq="http://activemq.apache.org/schema/core"<br />&nbsp;&nbsp; &nbsp;xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"<br />&nbsp;&nbsp; &nbsp;xsi:schemaLocation="http://www.springframework.org/schema/beans<br />&nbsp;&nbsp; &nbsp; http://www.springframework.org/schema/beans/spring-beans-2.0.xsd<br />&nbsp; http://activemq.apache.org/schema/core&nbsp;<br />&nbsp; http://activemq.apache.org/schema/core/activemq-core.xsd"&gt;</p><p style="line-height: normal; color: #333333; font-family: Arial; background-color: #ffffff; ">&nbsp; &lt;!-- 配置JMS链接模版 --&gt;&nbsp;&nbsp;<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; &lt;bean id="connectionFactory" class="org.apache.activemq.spring.ActiveMQConnectionFactory"&gt; &nbsp;<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; &lt;property name="brokerURL" value="tcp://localhost:61618"/&gt; &nbsp;<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; &lt;/bean&gt; &nbsp;<br />&nbsp;&nbsp;&nbsp;&nbsp; &nbsp;<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; &lt;!-- 配置JMS模版 --&gt; &nbsp;<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; &lt;bean id="jmsTemplate" class="org.springframework.jms.core.JmsTemplate"&gt; &nbsp;<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; &lt;property name="connectionFactory" ref="connectionFactory"/&gt; &nbsp;<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; &lt;property name="pubSubDomain" value="false" /&gt; &nbsp;<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; &lt;property name="explicitQosEnabled" value="true" /&gt; &lt;!-- deliveryMode, priority, timeToLive 的开关，要生效，必须配置为true，默认false--&gt; &nbsp;<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; &lt;property name="deliveryMode" value="1" /&gt; &lt;!-- 发送模式&nbsp; DeliveryMode.NON_PERSISTENT=1:非持久 ; DeliveryMode.PERSISTENT=2:持久--&gt; &nbsp;<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; &nbsp;<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; &lt;/bean&gt; &nbsp;<br />&nbsp;&nbsp;&nbsp;&nbsp; &nbsp;<br /><br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; &lt;!-- 发送消息的目的地（一个队列） --&gt;<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; &lt;bean id="destination" class="org.apache.activemq.command.ActiveMQQueue"&gt; &nbsp;<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; &lt;!-- 设置消息队列的名字 --&gt;<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; &lt;constructor-arg index="0" value="HelloJmsQueue"/&gt; &nbsp;<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; &lt;/bean&gt;&nbsp;&nbsp;&nbsp;&nbsp;<br /><br />&nbsp;&nbsp; &nbsp;&lt;bean id="sender" class="message.Sender"&gt;<br />&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&lt;property name="jmsTemplate" ref="jmsTemplate"&gt;&lt;/property&gt;<br />&nbsp;&nbsp; &nbsp;&lt;/bean&gt;<br />&lt;!--消息获取类--&gt;<br />&nbsp;&nbsp; &nbsp;&lt;bean id="receive" class="message.Receiver"&gt;&lt;/bean&gt;<br />&nbsp;&nbsp;&nbsp; &lt;!--给消息获取类加个监听让他能自动获取消息--&gt;<br />&nbsp;&nbsp; &nbsp;&lt;bean id="listenerContainer"<br />&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;class="org.springframework.jms.listener.DefaultMessageListenerContainer"&gt;<br />&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&lt;property name="connectionFactory" ref="connectionFactory"&gt;&lt;/property&gt;<br />&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&lt;property name="destination" ref="destination"&gt;&lt;/property&gt;<br />&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&lt;property name="messageListener" ref="receive"&gt;&lt;/property&gt;<br />&nbsp;&nbsp; &nbsp;&lt;/bean&gt;<br />&lt;/beans&gt;</p><p style="line-height: normal; color: #333333; font-family: Arial; background-color: #ffffff; ">发送消息的类</p><p style="line-height: normal; color: #333333; font-family: Arial; background-color: #ffffff; ">package message;<br /><br />import javax.jms.Destination;<br />import javax.jms.JMSException;<br />import javax.jms.Message;<br />import javax.jms.Session;<br /><br />import org.springframework.context.ApplicationContext;<br />import org.springframework.context.support.ClassPathXmlApplicationContext;<br />import org.springframework.jms.core.JmsTemplate;<br />import org.springframework.jms.core.MessageCreator;<br /><br /><br />public class Sender {<br /><br />&nbsp;&nbsp; &nbsp;private JmsTemplate jmsTemplate;<br /><br />&nbsp;&nbsp; &nbsp;public void setJmsTemplate(JmsTemplate jmsTemplate) {<br />&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;this.jmsTemplate = jmsTemplate;<br />&nbsp;&nbsp; &nbsp;}<br />&nbsp;&nbsp; &nbsp;<br />&nbsp;&nbsp; &nbsp;public void send(final String text){<br />&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;System.out.println("---Send:"+text);<br />&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;jmsTemplate.send(new MessageCreator(){<br /><br />&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;public Message createMessage(Session arg0) throws JMSException {<br />&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;// TODO Auto-generated method stub<br />&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;return arg0.createTextMessage(text);<br />&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;}<br />&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;<br />&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;});<br />&nbsp;&nbsp; &nbsp;}<br />&nbsp;&nbsp; &nbsp;<br />&nbsp;&nbsp;&nbsp; public static void main(String[] args) { &nbsp;<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; ApplicationContext ctx = new ClassPathXmlApplicationContext("/applicationContext.xml"); &nbsp;<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; JmsTemplate template = (JmsTemplate) ctx.getBean("jmsTemplate"); &nbsp;<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; Destination destination = (Destination) ctx.getBean("destination"); &nbsp;<br />for(int i=0;i&lt;10;i++){<br />&nbsp;&nbsp; &nbsp;final String dd = i+"";<br />//jmsTemplate.convertAndSend(destination,"发送消息： ActiveMQ Text Message！"+dd);<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; template.send(destination, new MessageCreator() { &nbsp;<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; public Message createMessage(Session session) throws JMSException { &nbsp;<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; return session.createTextMessage("发送消息： ActiveMQ Text Message！"+dd); &nbsp;<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; } &nbsp;<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; }); &nbsp;<br />}<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; System.out.println("成功发送了一条JMS消息"); &nbsp;<br />} &nbsp;<br />}<br />接收消息类</p><p style="line-height: normal; color: #333333; font-family: Arial; background-color: #ffffff; ">package message;<br /><br />import javax.jms.Destination;<br />import javax.jms.JMSException;<br />import javax.jms.Message;<br />import javax.jms.MessageListener;<br />import javax.jms.TextMessage;<br /><br />import org.apache.xbean.spring.context.ClassPathXmlApplicationContext;<br />import org.springframework.context.ApplicationContext;<br />import org.springframework.jms.core.JmsTemplate;<br /><br /><br />public class Receiver implements MessageListener {<br />//接听接收<br />&nbsp;&nbsp; &nbsp;public void onMessage(Message message) {<br />&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;if (message instanceof TextMessage) {<br />&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;TextMessage text = (TextMessage) message;<br /><br />&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;try {<br />&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;System.out.println("Receive:第四个接收者" + text.getText());<br />&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;} catch (JMSException e) {<br />&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;// TODO Auto-generated catch block<br />&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;e.printStackTrace();<br />&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;}<br /><br />&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;}<br />&nbsp;&nbsp; &nbsp;}<br />&nbsp;&nbsp;&nbsp; //手动接收<br />&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; public static void main(String[] args) throws JMSException { &nbsp;<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; ApplicationContext ctx = new ClassPathXmlApplicationContext("/applicationContext.xml"); &nbsp;<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; JmsTemplate template = (JmsTemplate) ctx.getBean("jmsTemplate"); &nbsp;<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; Destination destination = (Destination) ctx.getBean("destination"); &nbsp;<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; while (true) { &nbsp;<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; TextMessage txtmsg = (TextMessage) template.receive(destination); &nbsp;<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; if (null != txtmsg) &nbsp;<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; System.out.println("收到消息内容为: " + txtmsg.getText()); &nbsp;<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; else &nbsp;<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; break; &nbsp;<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; } &nbsp;<br />&nbsp;&nbsp; } &nbsp;<br />}</p><p style="line-height: normal; color: #333333; font-family: Arial; background-color: #ffffff; ">发布者模式</p><p style="line-height: normal; color: #333333; font-family: Arial; background-color: #ffffff; ">其实两种模式其它设置基本相同</p><p style="line-height: normal; color: #333333; font-family: Arial; background-color: #ffffff; ">唯一不用的就是 Spring配置文件中把一下这段替换一下就可以了</p><p style="line-height: normal; color: #333333; font-family: Arial; background-color: #ffffff; ">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; &lt;!-- 发送消息的目的地（一个队列） --&gt;<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; &lt;bean id="destination" class="org.apache.activemq.command.ActiveMQTopic"&gt; &nbsp;<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; &lt;!-- 设置消息队列的名字 --&gt;<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; &lt;constructor-arg index="0" value="HelloJmsTopic"/&gt; &nbsp;<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; &lt;/bean&gt;&nbsp;&nbsp;&nbsp;&nbsp;</p><p style="line-height: normal; color: #333333; font-family: Arial; background-color: #ffffff; ">&nbsp;<br /><br /><br /><br /><br /><br /><br /><br /><br /><a href="http://hi.baidu.com/wu_chao/blog/item/1d9aa2772e663107b051b9cc.html">http://hi.baidu.com/wu_chao/blog/item/1d9aa2772e663107b051b9cc.html</a>&nbsp;<br /></p><img src ="http://www.blogjava.net/stevenjohn/aggbug/386311.html" width = "1" height = "1" /><br><br><div align=right><a style="text-decoration:none;" href="http://www.blogjava.net/stevenjohn/" target="_blank">abin</a> 2012-08-26 20:18 <a href="http://www.blogjava.net/stevenjohn/archive/2012/08/26/386311.html#Feedback" target="_blank" style="text-decoration:none;">发表评论</a></div>]]></description></item><item><title>spring-activemq </title><link>http://www.blogjava.net/stevenjohn/archive/2012/08/25/386274.html</link><dc:creator>abin</dc:creator><author>abin</author><pubDate>Sat, 25 Aug 2012 13:34:00 GMT</pubDate><guid>http://www.blogjava.net/stevenjohn/archive/2012/08/25/386274.html</guid><wfw:comment>http://www.blogjava.net/stevenjohn/comments/386274.html</wfw:comment><comments>http://www.blogjava.net/stevenjohn/archive/2012/08/25/386274.html#Feedback</comments><slash:comments>0</slash:comments><wfw:commentRss>http://www.blogjava.net/stevenjohn/comments/commentRss/386274.html</wfw:commentRss><trackback:ping>http://www.blogjava.net/stevenjohn/services/trackbacks/386274.html</trackback:ping><description><![CDATA[<div id="article_content" class="article_content">
<p><strong><span style="color: #ff9900">1,项目中所引的包</span></strong></p>
<p><img alt="" src="http://hi.csdn.net/attachment/201107/24/0_13114960815J85.gif" /></p>
<p>&nbsp;</p>
<p><strong><span style="color: #ff0000">2，发送和接受消息的代码，每个都是一个Junit函数</span></strong></p>
<p>package test;</p>
<p>import javax.jms.Destination;<br />import javax.jms.JMSException;<br />import javax.jms.Message;<br />import javax.jms.Session;<br />import javax.jms.TextMessage;</p>
<p>import junit.framework.TestCase;</p>
<p>import org.apache.xbean.spring.context.ClassPathXmlApplicationContext;<br />import org.junit.Test;<br />import org.springframework.context.ApplicationContext;<br />import org.springframework.context.support.FileSystemXmlApplicationContext;<br />import org.springframework.jms.core.JmsTemplate;<br />import org.springframework.jms.core.MessageCreator;</p>
<p>public class ActivemqTest extends TestCase {<br />&nbsp;@Test<br />&nbsp;public void testJmsTemplateSend() {&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; </p>
<p>&nbsp;&nbsp;ApplicationContext ctx = new FileSystemXmlApplicationContext(<br />&nbsp;&nbsp;&nbsp;&nbsp;"WebRoot/WEB-INF/applicationContext.xml");<br />&nbsp;&nbsp;JmsTemplate template = (JmsTemplate) ctx.getBean("jmsTemplate");<br />&nbsp;&nbsp;Destination destination = (Destination) ctx.getBean("destination");</p>
<p>&nbsp;&nbsp;template.send(destination, new MessageCreator() {<br />&nbsp;&nbsp;&nbsp;public Message createMessage(Session session) throws JMSException {<br />&nbsp;&nbsp;&nbsp;&nbsp;return session.createTextMessage("发送消息：Hello ActiveMQ Text Message！");<br />&nbsp;&nbsp;&nbsp;}<br />&nbsp;&nbsp;});<br />&nbsp;&nbsp;System.out.println("成功发送了一条JMS消息");<br />&nbsp;}</p>
<p>&nbsp;@Test<br />&nbsp;public void testJmsTemplateReceive() throws JMSException {<br />&nbsp;&nbsp;ApplicationContext ctx = new FileSystemXmlApplicationContext(<br />&nbsp;&nbsp;&nbsp;&nbsp;"WebRoot/WEB-INF/applicationContext.xml");<br />&nbsp;&nbsp;JmsTemplate template = (JmsTemplate) ctx.getBean("jmsTemplate");<br />&nbsp;&nbsp;Destination destination = (Destination) ctx.getBean("destination");<br />&nbsp;&nbsp;while (true) {<br />&nbsp;&nbsp;&nbsp;TextMessage txtmsg = (TextMessage) template.receive(destination);<br />&nbsp;&nbsp;&nbsp;if (null != txtmsg)<br />&nbsp;&nbsp;&nbsp;&nbsp;System.out.println("收到消息内容为: " + txtmsg.getText());<br />&nbsp;&nbsp;&nbsp;else<br />&nbsp;&nbsp;&nbsp;&nbsp;break;<br />&nbsp;&nbsp;}<br />&nbsp;}</p>
<p>&nbsp;public static void main(String[] args) throws Exception{<br />&nbsp;&nbsp;new ActivemqTest().testJmsTemplateSend();<br />&nbsp;&nbsp;<br />&nbsp;&nbsp;new ActivemqTest().testJmsTemplateReceive();<br />&nbsp;}<br />}<br /></p>
<p>&nbsp;</p>
<p><strong><span style="color: #ff0000">3，applicationContext.xml文件内容</span></strong></p>
<p>&lt;?xml version="1.0" encoding="UTF-8"?&gt;&nbsp;&nbsp; <br />&lt;beans xmlns="<a href="http://www.springframework.org/schema/beans"><font color="#ca0000">http://www.springframework.org/schema/beans</font></a>" xmlns:xsi="<a href="http://www.w3.org/2001/XMLSchema-instance"><font color="#ca0000">http://www.w3.org/2001/XMLSchema-instance</font></a>"&nbsp; <br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; xmlns:context="<a href="http://www.springframework.org/schema/context"><font color="#ca0000">http://www.springframework.org/schema/context</font></a>"&nbsp; <br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; xsi:schemaLocation="<a href="http://www.springframework.org/schema/beans"><font color="#ca0000">http://www.springframework.org/schema/beans</font></a>&nbsp;&nbsp; <br />&nbsp;&nbsp;&nbsp; <a href="http://www.springframework.org/schema/beans/spring-beans-2.5.xsd"><font color="#ca0000">http://www.springframework.org/schema/beans/spring-beans-2.5.xsd</font></a>&nbsp;&nbsp; <br />&nbsp;&nbsp;&nbsp; <a href="http://www.springframework.org/schema/context"><font color="#ca0000">http://www.springframework.org/schema/context</font></a>&nbsp;&nbsp; <br />&nbsp;&nbsp;&nbsp; <a href="http://www.springframework.org/schema/context/spring-context-2.5.xsd"><font color="#ca0000">http://www.springframework.org/schema/context/spring-context-2.5.xsd</font></a>"&gt;&nbsp;&nbsp; <br />&nbsp; <br />&nbsp;&nbsp;&nbsp; &lt;!-- 配置JMS连接工厂 --&gt;&nbsp;&nbsp; <br />&nbsp;&nbsp;&nbsp; &lt;bean id="connectionFactory" class="org.apache.activemq.spring.ActiveMQConnectionFactory"&gt;&nbsp;&nbsp; <br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; &lt;property name="brokerURL" value="tcp://localhost:61616"/&gt;&nbsp;&nbsp; <br />&nbsp;&nbsp;&nbsp; &lt;/bean&gt;&nbsp;&nbsp; <br />&nbsp; <br />&nbsp;&nbsp;&nbsp; &lt;!-- 配置JMS模版 --&gt;&nbsp;&nbsp; <br />&nbsp;&nbsp;&nbsp; &lt;bean id="jmsTemplate" class="org.springframework.jms.core.JmsTemplate"&gt;&nbsp;&nbsp; <br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; &lt;property name="connectionFactory" ref="connectionFactory"/&gt;&nbsp;&nbsp; <br />&nbsp;&nbsp;&nbsp; &lt;/bean&gt;&nbsp;&nbsp; <br />&nbsp; <br />&nbsp;&nbsp;&nbsp; &lt;!-- 发送消息的目的地（一个队列） --&gt;&nbsp;&nbsp; <br />&nbsp;&nbsp;&nbsp; &lt;bean id="destination" class="org.apache.activemq.command.ActiveMQQueue"&gt;&nbsp;&nbsp; <br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; &lt;!-- Set the Queue Name --&gt;&nbsp;&nbsp; <br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; &lt;constructor-arg index="0" value="HelloWorldQueue"/&gt;&nbsp;&nbsp; <br />&nbsp;&nbsp;&nbsp; &lt;/bean&gt;&nbsp;&nbsp; <br />&lt;/beans&gt;<br /><br /><br /><br /><br /><a href="http://blog.csdn.net/hnzhangshilong/article/details/6629780">http://blog.csdn.net/hnzhangshilong/article/details/6629780</a></p></div><img src ="http://www.blogjava.net/stevenjohn/aggbug/386274.html" width = "1" height = "1" /><br><br><div align=right><a style="text-decoration:none;" href="http://www.blogjava.net/stevenjohn/" target="_blank">abin</a> 2012-08-25 21:34 <a href="http://www.blogjava.net/stevenjohn/archive/2012/08/25/386274.html#Feedback" target="_blank" style="text-decoration:none;">发表评论</a></div>]]></description></item><item><title>ActiveMQ StreamMessage </title><link>http://www.blogjava.net/stevenjohn/archive/2012/08/02/384622.html</link><dc:creator>abin</dc:creator><author>abin</author><pubDate>Thu, 02 Aug 2012 08:35:00 GMT</pubDate><guid>http://www.blogjava.net/stevenjohn/archive/2012/08/02/384622.html</guid><wfw:comment>http://www.blogjava.net/stevenjohn/comments/384622.html</wfw:comment><comments>http://www.blogjava.net/stevenjohn/archive/2012/08/02/384622.html#Feedback</comments><slash:comments>0</slash:comments><wfw:commentRss>http://www.blogjava.net/stevenjohn/comments/commentRss/384622.html</wfw:commentRss><trackback:ping>http://www.blogjava.net/stevenjohn/services/trackbacks/384622.html</trackback:ping><description><![CDATA[<p>package org.abin.lee.activemq;</p>
<p>import javax.jms.Connection;<br />import javax.jms.ConnectionFactory;<br />import javax.jms.DeliveryMode;<br />import javax.jms.Destination;<br />import javax.jms.MapMessage;<br />import javax.jms.MessageProducer;<br />import javax.jms.Session;</p>
<p>import org.apache.activemq.ActiveMQConnection;<br />import org.apache.activemq.ActiveMQConnectionFactory;</p>
<p>public class MapSender {</p>
<p>&nbsp;private static final int SEND_NUMBER = 5;</p>
<p>&nbsp;public static void main(String[] args) {<br />&nbsp;&nbsp;ConnectionFactory connectionFactory = new ActiveMQConnectionFactory(<br />&nbsp;&nbsp;&nbsp;&nbsp;ActiveMQConnection.DEFAULT_USER,<br />&nbsp;&nbsp;&nbsp;&nbsp;ActiveMQConnection.DEFAULT_PASSWORD, "tcp://localhost:61616");<br />&nbsp;&nbsp;Connection connection = null;<br />&nbsp;&nbsp;Session session;<br />&nbsp;&nbsp;Destination destination = null;<br />&nbsp;&nbsp;MessageProducer messageProducer;<br />&nbsp;&nbsp;try {<br />&nbsp;&nbsp;&nbsp;connection=connectionFactory.createConnection();<br />&nbsp;&nbsp;&nbsp;connection.start();<br />&nbsp;&nbsp;&nbsp;session=connection.createSession(Boolean.TRUE, Session.AUTO_ACKNOWLEDGE);<br />&nbsp;&nbsp;&nbsp;destination=session.createQueue("FirstQueue");<br />&nbsp;&nbsp;&nbsp;messageProducer=session.createProducer(destination);<br />&nbsp;&nbsp;&nbsp;messageProducer.setDeliveryMode(DeliveryMode.NON_PERSISTENT);<br />&nbsp;&nbsp;&nbsp;<br />&nbsp;&nbsp;&nbsp;MapMessage map=session.createMapMessage();<br />&nbsp;&nbsp;&nbsp;map.setBoolean("flag", true);<br />&nbsp;&nbsp;&nbsp;map.setDouble("dou", 1.01);<br />&nbsp;&nbsp;&nbsp;map.setInt("zx", 88);<br />&nbsp;&nbsp;&nbsp;map.setString("zifu", "zzzzzz");<br />&nbsp;&nbsp;&nbsp;messageProducer.send(map);<br />&nbsp;&nbsp;&nbsp;session.commit();<br />&nbsp;&nbsp;} catch (Exception e) {<br />&nbsp;&nbsp;&nbsp;e.printStackTrace();<br />&nbsp;&nbsp;}<br />&nbsp;}</p>
<p><br />}<br /></p><br /><br /><br /><br /><br />
<p>package org.abin.lee.activemq;</p>
<p>import javax.jms.Connection;<br />import javax.jms.ConnectionFactory;<br />import javax.jms.Destination;<br />import javax.jms.MapMessage;<br />import javax.jms.MessageConsumer;<br />import javax.jms.Session;</p>
<p>import org.apache.activemq.ActiveMQConnection;<br />import org.apache.activemq.ActiveMQConnectionFactory;</p>
<p>public class MapReceiver {public static void main(String[] args) {<br />&nbsp;ConnectionFactory connectionFactory = new ActiveMQConnectionFactory(<br />&nbsp;&nbsp;&nbsp;ActiveMQConnection.DEFAULT_USER,<br />&nbsp;&nbsp;&nbsp;ActiveMQConnection.DEFAULT_PASSWORD, "tcp://localhost:61616");<br />&nbsp;Connection connection = null;<br />&nbsp;Session session;<br />&nbsp;Destination destination;<br />&nbsp;MessageConsumer consumer;<br />&nbsp;try {<br />&nbsp;&nbsp;connection = connectionFactory.createConnection();<br />&nbsp;&nbsp;connection.start();<br />&nbsp;&nbsp;session = connection.createSession(Boolean.FALSE,<br />&nbsp;&nbsp;&nbsp;&nbsp;Session.AUTO_ACKNOWLEDGE);<br />&nbsp;&nbsp;destination = session.createQueue("FirstQueue");<br />&nbsp;&nbsp;consumer = session.createConsumer(destination);<br />&nbsp;&nbsp;while(true){<br />&nbsp;&nbsp;&nbsp;MapMessage map=(MapMessage)consumer.receive(500000);<br />&nbsp;&nbsp;&nbsp;if (null != map) {<br />&nbsp;&nbsp;&nbsp;&nbsp;boolean flag=map.getBoolean("falg");<br />&nbsp;&nbsp;&nbsp;&nbsp;System.out.println("AcitveMQ 接收到的消息&nbsp; flag="+flag);<br />&nbsp;&nbsp;&nbsp;&nbsp;double dou=map.getDouble("dou");<br />&nbsp;&nbsp;&nbsp;&nbsp;System.out.println("AcitveMQ 接收到的消息&nbsp; dou="+dou);<br />&nbsp;&nbsp;&nbsp;&nbsp;int zx=map.getInt("zx");<br />&nbsp;&nbsp;&nbsp;&nbsp;System.out.println("AcitveMQ 接收到的消息&nbsp; zx="+zx);<br />&nbsp;&nbsp;&nbsp;&nbsp;String zifu=map.getString("zifu");<br />&nbsp;&nbsp;&nbsp;&nbsp;System.out.println("AcitveMQ 接收到的消息&nbsp; zifu="+zifu);<br />&nbsp;&nbsp;&nbsp;}else<br />&nbsp;&nbsp;&nbsp;&nbsp;break;</p>
<p>&nbsp;&nbsp;}<br />&nbsp;&nbsp;<br />&nbsp;&nbsp;&nbsp;</p>
<p>&nbsp;} catch (Exception e) {<br />&nbsp;&nbsp;e.printStackTrace();<br />&nbsp;}</p>
<p>}<br />}<br /></p><img src ="http://www.blogjava.net/stevenjohn/aggbug/384622.html" width = "1" height = "1" /><br><br><div align=right><a style="text-decoration:none;" href="http://www.blogjava.net/stevenjohn/" target="_blank">abin</a> 2012-08-02 16:35 <a href="http://www.blogjava.net/stevenjohn/archive/2012/08/02/384622.html#Feedback" target="_blank" style="text-decoration:none;">发表评论</a></div>]]></description></item><item><title>ActiveMQ入门示例 </title><link>http://www.blogjava.net/stevenjohn/archive/2012/08/02/384609.html</link><dc:creator>abin</dc:creator><author>abin</author><pubDate>Thu, 02 Aug 2012 06:59:00 GMT</pubDate><guid>http://www.blogjava.net/stevenjohn/archive/2012/08/02/384609.html</guid><wfw:comment>http://www.blogjava.net/stevenjohn/comments/384609.html</wfw:comment><comments>http://www.blogjava.net/stevenjohn/archive/2012/08/02/384609.html#Feedback</comments><slash:comments>0</slash:comments><wfw:commentRss>http://www.blogjava.net/stevenjohn/comments/commentRss/384609.html</wfw:commentRss><trackback:ping>http://www.blogjava.net/stevenjohn/services/trackbacks/384609.html</trackback:ping><description><![CDATA[<p>企业中各项目中相互协作的时候可能用得到消息通知机制。比如有东西更新了，可以通知做索引。</p>
<p>在 Java 里有 JMS 的多个实现。其中 apache 下的 ActiveMQ 就是不错的选择。ActiveMQ 是Apache出品，最流行的，能力强劲的开源消息总线。ActiveMQ 是一个完全支持JMS1.1和J2EE 1.4规范的 JMS Provider实现。这里示例下使用 ActiveMQ</p>
<p>用 ActiveMQ 最好还是了解下 JMS</p>
<div>
<table border="2">
<tbody>
<tr>
<td><strong>JMS 公共</strong> </td>
<td><strong>点对点域</strong> </td>
<td><strong>发布/订阅域</strong> </td></tr>
<tr>
<td>ConnectionFactory</td>
<td>QueueConnectionFactory</td>
<td>TopicConnectionFactory</td></tr>
<tr>
<td>Connection</td>
<td>QueueConnection</td>
<td>TopicConnection</td></tr>
<tr>
<td>Destination</td>
<td>Queue</td>
<td>Topic</td></tr>
<tr>
<td>Session</td>
<td>QueueSession</td>
<td>TopicSession</td></tr>
<tr>
<td>MessageProducer</td>
<td>QueueSender</td>
<td>TopicPublisher</td></tr>
<tr>
<td>MessageConsumer</td>
<td>QueueReceiver</td>
<td>TopicSubscriber</td></tr></tbody></table></div>
<p>JMS 定义了两种方式：Quere（点对点）；Topic（发布/订阅）。</p>
<p>ConnectionFactory 是连接工厂，负责创建Connection。</p>
<p>Connection 负责创建 Session。</p>
<p>Session 创建 MessageProducer（用来发消息） 和 MessageConsumer（用来接收消息）。</p>
<p>Destination 是消息的目的地。</p>
<p>详细的可以网上找些 JMS 规范（有中文版）。</p>
<p>下载 apache-activemq-5.3.0。<a target="_blank"><span style="color: #800080">http://activemq.apache.org/download.html</span> </a>，解压，然后双击 bin/activemq.bat。运行后，可以在 <a target="_blank"><span style="color: #800080">http://localhost:8161/admin</span> </a>观察。也有 demo， <a target="_blank"><span style="color: #800080">http://localhost:8161/demo</span> </a>。把 activemq-all-5.3.0.jar 加入 classpath。</p>
<p>Jms 发送 代码：</p>public static void main(String[] args) throws Exception {&nbsp;&nbsp;&nbsp; <br />&nbsp;&nbsp;&nbsp; ConnectionFactory connectionFactory = new ActiveMQConnectionFactory();&nbsp;&nbsp;&nbsp; <br />&nbsp;&nbsp; <br />&nbsp;&nbsp;&nbsp; Connection connection = connectionFactory.createConnection();&nbsp;&nbsp;&nbsp; <br />&nbsp;&nbsp;&nbsp; connection.start();&nbsp;&nbsp;&nbsp; <br />&nbsp;&nbsp; <br />&nbsp;&nbsp;&nbsp; Session session = connection.createSession(Boolean.TRUE, Session.AUTO_ACKNOWLEDGE);&nbsp;&nbsp;&nbsp; <br />&nbsp;&nbsp;&nbsp; Destination destination = session.createQueue("my-queue");&nbsp;&nbsp;&nbsp; <br />&nbsp;&nbsp; <br />&nbsp;&nbsp;&nbsp; MessageProducer producer = session.createProducer(destination);&nbsp;&nbsp;&nbsp; <br />&nbsp;&nbsp;&nbsp; for(int i=0; i&lt;3; i++) {&nbsp;&nbsp;&nbsp; <br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; MapMessage message = session.createMapMessage();&nbsp;&nbsp;&nbsp; <br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; message.setLong("count", new Date().getTime());&nbsp;&nbsp;&nbsp; <br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; Thread.sleep(1000);&nbsp;&nbsp;&nbsp; <br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; //通过消息生产者发出消息&nbsp;&nbsp;&nbsp; <br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; producer.send(message);&nbsp;&nbsp;&nbsp; <br />&nbsp;&nbsp;&nbsp; }&nbsp;&nbsp;&nbsp; <br />&nbsp;&nbsp;&nbsp; session.commit();&nbsp;&nbsp;&nbsp; <br />&nbsp;&nbsp;&nbsp; session.close();&nbsp;&nbsp;&nbsp; <br />&nbsp;&nbsp;&nbsp; connection.close();&nbsp;&nbsp;&nbsp; <br />} <br /><br /><br /><br />
<p>Jms 接收代码：</p><br />public static void main(String[] args) throws Exception {&nbsp;&nbsp;&nbsp; <br />&nbsp;&nbsp;&nbsp; ConnectionFactory connectionFactory = new ActiveMQConnectionFactory();&nbsp;&nbsp;&nbsp; <br />&nbsp;&nbsp; <br />&nbsp;&nbsp;&nbsp; Connection connection = connectionFactory.createConnection();&nbsp;&nbsp;&nbsp; <br />&nbsp;&nbsp;&nbsp; connection.start();&nbsp;&nbsp;&nbsp; <br />&nbsp;&nbsp; <br />&nbsp;&nbsp;&nbsp; final Session session = connection.createSession(Boolean.TRUE, Session.AUTO_ACKNOWLEDGE);&nbsp;&nbsp;&nbsp; <br />&nbsp;&nbsp;&nbsp; Destination destination = session.createQueue("my-queue");&nbsp;&nbsp;&nbsp; <br />&nbsp;&nbsp; <br />&nbsp;&nbsp;&nbsp; MessageConsumer consumer = session.createConsumer(destination);&nbsp;&nbsp;&nbsp; <br />&nbsp;&nbsp;&nbsp; int i=0;&nbsp;&nbsp;&nbsp; <br />&nbsp;&nbsp;&nbsp; while(i&lt;3) {&nbsp;&nbsp;&nbsp; <br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; i++;&nbsp;&nbsp;&nbsp; <br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; MapMessage message = (MapMessage) consumer.receive();&nbsp;&nbsp;&nbsp; <br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; session.commit();&nbsp;&nbsp;&nbsp; <br />&nbsp;&nbsp; <br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; //TODO something....&nbsp;&nbsp;&nbsp; <br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; System.out.println("收到消息：" + new Date(message.getLong("count")));&nbsp;&nbsp;&nbsp; <br />&nbsp;&nbsp;&nbsp; }&nbsp;&nbsp;&nbsp; <br />&nbsp;&nbsp; <br />&nbsp;&nbsp;&nbsp; session.close();&nbsp;&nbsp;&nbsp; <br />&nbsp;&nbsp;&nbsp; connection.close();&nbsp;&nbsp;&nbsp; <br />}<br /><br /><br /><br />
<p><span style="font-size: large"><font size="4">JMS五种消息的发送/接收的例子</font></span> </p>
<p><span style="font-size: large"><font size="4">转自：</font><a href="http://chenjumin.javaeye.com/blog/687124"><font size="4">http://chenjumin.javaeye.com/blog/687124</font></a><font size="4"> &nbsp;</font></span> </p>
<p>1、消息发送</p>//连接工厂&nbsp;&nbsp; <br />ConnectionFactory connFactory = new ActiveMQConnectionFactory(&nbsp;&nbsp; <br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; ActiveMQConnection.DEFAULT_USER,&nbsp;&nbsp; <br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; ActiveMQConnection.DEFAULT_PASSWORD,&nbsp;&nbsp; <br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; "tcp://localhost:61616");&nbsp;&nbsp; <br />&nbsp; <br />//连接到JMS提供者&nbsp;&nbsp; <br />Connection conn = connFactory.createConnection();&nbsp;&nbsp; <br />conn.start();&nbsp;&nbsp; <br />&nbsp; <br />//事务性会话，自动确认消息&nbsp;&nbsp; <br />Session session = conn.createSession(true, Session.AUTO_ACKNOWLEDGE);&nbsp;&nbsp; <br />&nbsp; <br />//消息的目的地&nbsp;&nbsp; <br />Destination destination = session.createQueue("queue.hello");&nbsp;&nbsp; <br />&nbsp; <br />//消息生产者&nbsp;&nbsp; <br />MessageProducer producer = session.createProducer(destination);&nbsp;&nbsp; <br />producer.setDeliveryMode(DeliveryMode.NON_PERSISTENT); //不持久化&nbsp;&nbsp; <br />&nbsp; <br />&nbsp; <br />//文本消息&nbsp;&nbsp; <br />TextMessage textMessage = session.createTextMessage("文本消息");&nbsp;&nbsp; <br />producer.send(textMessage);&nbsp;&nbsp; <br />&nbsp; <br />//键值对消息&nbsp;&nbsp; <br />MapMessage mapMessage = session.createMapMessage();&nbsp;&nbsp; <br />mapMessage.setLong("age", new Long(32));&nbsp;&nbsp; <br />mapMessage.setDouble("sarray", new Double(5867.15));&nbsp;&nbsp; <br />mapMessage.setString("username", "键值对消息");&nbsp;&nbsp; <br />producer.send(mapMessage);&nbsp;&nbsp; <br />&nbsp; <br />//流消息&nbsp;&nbsp; <br />StreamMessage streamMessage = session.createStreamMessage();&nbsp;&nbsp; <br />streamMessage.writeString("streamMessage流消息");&nbsp;&nbsp; <br />streamMessage.writeLong(55);&nbsp;&nbsp; <br />producer.send(streamMessage);&nbsp;&nbsp; <br />&nbsp; <br />//字节消息&nbsp;&nbsp; <br />String s = "BytesMessage字节消息";&nbsp;&nbsp; <br />BytesMessage bytesMessage = session.createBytesMessage();&nbsp;&nbsp; <br />bytesMessage.writeBytes(s.getBytes());&nbsp;&nbsp; <br />producer.send(bytesMessage);&nbsp;&nbsp; <br />&nbsp; <br />//对象消息&nbsp;&nbsp; <br />User user = new User("cjm", "对象消息"); //User对象必须实现Serializable接口&nbsp;&nbsp; <br />ObjectMessage objectMessage = session.createObjectMessage();&nbsp;&nbsp; <br />objectMessage.setObject(user);&nbsp;&nbsp; <br />producer.send(objectMessage);&nbsp;&nbsp; <br />&nbsp; <br />&nbsp; <br />session.commit(); //在事务性会话中，只有commit之后，消息才会真正到达目的地&nbsp;&nbsp; <br />producer.close();&nbsp;&nbsp; <br />session.close();&nbsp;&nbsp; <br />conn.close();&nbsp; <br /><br /><br /><br />
<p>2、消息接收：通过消息监听器的方式接收消息</p><br />public class Receiver implements MessageListener{&nbsp;&nbsp; <br />&nbsp;&nbsp;&nbsp; private boolean stop = false;&nbsp;&nbsp; <br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; <br />&nbsp;&nbsp;&nbsp; public void execute() throws Exception {&nbsp;&nbsp; <br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; //连接工厂&nbsp;&nbsp; <br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; ConnectionFactory connFactory = new ActiveMQConnectionFactory(&nbsp;&nbsp; <br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; ActiveMQConnection.DEFAULT_USER,&nbsp;&nbsp; <br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; ActiveMQConnection.DEFAULT_PASSWORD,&nbsp;&nbsp; <br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; "tcp://localhost:61616");&nbsp;&nbsp; <br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; <br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; //连接到JMS提供者&nbsp;&nbsp; <br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; Connection conn = connFactory.createConnection();&nbsp;&nbsp; <br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; conn.start();&nbsp;&nbsp; <br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; <br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; //事务性会话，自动确认消息&nbsp;&nbsp; <br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; Session session = conn.createSession(true, Session.AUTO_ACKNOWLEDGE);&nbsp;&nbsp; <br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; <br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; //消息的来源地&nbsp;&nbsp; <br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; Destination destination = session.createQueue("queue.hello");&nbsp;&nbsp; <br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; <br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; //消息消费者&nbsp;&nbsp; <br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; MessageConsumer consumer = session.createConsumer(destination);&nbsp;&nbsp; <br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; consumer.setMessageListener(this);&nbsp;&nbsp; <br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; <br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; //等待接收消息&nbsp;&nbsp; <br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; while(!stop){&nbsp;&nbsp; <br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; Thread.sleep(5000);&nbsp;&nbsp; <br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; }&nbsp;&nbsp; <br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; <br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; session.commit();&nbsp;&nbsp; <br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; <br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; consumer.close();&nbsp;&nbsp; <br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; session.close();&nbsp;&nbsp; <br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; conn.close();&nbsp;&nbsp; <br />&nbsp;&nbsp;&nbsp; }&nbsp;&nbsp; <br />&nbsp; <br />&nbsp;&nbsp;&nbsp; public void onMessage(Message m) {&nbsp;&nbsp; <br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; try{&nbsp;&nbsp; <br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; if(m instanceof TextMessage){ //接收文本消息&nbsp;&nbsp; <br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; TextMessage message = (TextMessage)m;&nbsp;&nbsp; <br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; System.out.println(message.getText());&nbsp;&nbsp; <br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; }else if(m instanceof MapMessage){ //接收键值对消息&nbsp;&nbsp; <br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; MapMessage message = (MapMessage)m;&nbsp;&nbsp; <br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; System.out.println(message.getLong("age"));&nbsp;&nbsp; <br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; System.out.println(message.getDouble("sarray"));&nbsp;&nbsp; <br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; System.out.println(message.getString("username"));&nbsp;&nbsp; <br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; }else if(m instanceof StreamMessage){ //接收流消息&nbsp;&nbsp; <br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; StreamMessage message = (StreamMessage)m;&nbsp;&nbsp; <br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; System.out.println(message.readString());&nbsp;&nbsp; <br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; System.out.println(message.readLong());&nbsp;&nbsp; <br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; }else if(m instanceof BytesMessage){ //接收字节消息&nbsp;&nbsp; <br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; byte[] b = new byte[1024];&nbsp;&nbsp; <br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; int len = -1;&nbsp;&nbsp; <br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; BytesMessage message = (BytesMessage)m;&nbsp;&nbsp; <br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; while((len=message.readBytes(b))!=-1){&nbsp;&nbsp; <br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; System.out.println(new String(b, 0, len));&nbsp;&nbsp; <br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; }&nbsp;&nbsp; <br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; }else if(m instanceof ObjectMessage){ //接收对象消息&nbsp;&nbsp; <br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; ObjectMessage message = (ObjectMessage)m;&nbsp;&nbsp; <br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; User user = (User)message.getObject();&nbsp;&nbsp; <br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; System.out.println(user.getUsername() + " _ " + user.getPassword());&nbsp;&nbsp; <br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; }else{&nbsp;&nbsp; <br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; System.out.println(m);&nbsp;&nbsp; <br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; }&nbsp;&nbsp; <br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; <br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; stop = true;&nbsp;&nbsp; <br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; }catch(JMSException e){&nbsp;&nbsp; <br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; stop = true;&nbsp;&nbsp; <br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; e.printStackTrace();&nbsp;&nbsp; <br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; }&nbsp;&nbsp; <br />&nbsp;&nbsp;&nbsp; }&nbsp;&nbsp; <br />}&nbsp; <br /><br /><br /><br /><br /><a href="http://blog.csdn.net/caihaijiang/article/details/5903296">http://blog.csdn.net/caihaijiang/article/details/5903296</a><br /><img src ="http://www.blogjava.net/stevenjohn/aggbug/384609.html" width = "1" height = "1" /><br><br><div align=right><a style="text-decoration:none;" href="http://www.blogjava.net/stevenjohn/" target="_blank">abin</a> 2012-08-02 14:59 <a href="http://www.blogjava.net/stevenjohn/archive/2012/08/02/384609.html#Feedback" target="_blank" style="text-decoration:none;">发表评论</a></div>]]></description></item><item><title>ActiveMQ入门实例</title><link>http://www.blogjava.net/stevenjohn/archive/2012/07/07/382440.html</link><dc:creator>abin</dc:creator><author>abin</author><pubDate>Sat, 07 Jul 2012 06:47:00 GMT</pubDate><guid>http://www.blogjava.net/stevenjohn/archive/2012/07/07/382440.html</guid><wfw:comment>http://www.blogjava.net/stevenjohn/comments/382440.html</wfw:comment><comments>http://www.blogjava.net/stevenjohn/archive/2012/07/07/382440.html#Feedback</comments><slash:comments>0</slash:comments><wfw:commentRss>http://www.blogjava.net/stevenjohn/comments/commentRss/382440.html</wfw:commentRss><trackback:ping>http://www.blogjava.net/stevenjohn/services/trackbacks/382440.html</trackback:ping><description><![CDATA[<div>
<div id="blog_text">
<h2>1.下载ActiveMQ</h2>
<p>去官方网站下载：<a href="http://activemq.apache.org/" target="_blank">http://activemq.apache.org/</a></p>
<h2>2.运行ActiveMQ</h2>
<p>解压缩apache-activemq-5.5.1-bin.zip，然后双击apache-activemq-5.5.1\bin\activemq.bat运行ActiveMQ程序。</p>
<p>启动ActiveMQ以后，登陆：<a href="http://localhost:8161/admin/" target="_blank">http://localhost:8161/admin/</a>，创建一个Queue，命名为FirstQueue。</p>
<h2>3.创建Eclipse项目并运行</h2>
<p>创建project：ActiveMQ-5.5，并导入apache-activemq-5.5.1\lib目录下需要用到的jar文件，项目结构如下图所示：</p>
<p><img alt="" src="http://hiphotos.baidu.com/stevenjohn/pic/item/b8495ccfa786c917cc702896c93d70cf3ac757bc.jpg" /></p>
<p>3.1.Sender.java</p>
<p>package com.xuwei.activemq;<br /><br />import javax.jms.Connection;<br />import javax.jms.ConnectionFactory;<br />import javax.jms.DeliveryMode;<br />import javax.jms.Destination;<br />import javax.jms.MessageProducer;<br />import javax.jms.Session;<br />import javax.jms.TextMessage;<br />import org.apache.activemq.ActiveMQConnection;<br />import org.apache.activemq.ActiveMQConnectionFactory;<br /><br />public class Sender {<br />&nbsp;&nbsp;&nbsp; private static final int SEND_NUMBER = 5;<br /><br />&nbsp;&nbsp;&nbsp; public static void main(String[] args) {<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; // ConnectionFactory ：连接工厂，JMS 用它创建连接<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; ConnectionFactory connectionFactory;<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; // Connection ：JMS 客户端到JMS Provider 的连接<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; Connection connection = null;<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; // Session： 一个发送或接收消息的线程<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; Session session;<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; // Destination ：消息的目的地;消息发送给谁.<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; Destination destination;<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; // MessageProducer：消息发送者<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; MessageProducer producer;<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; // TextMessage message;<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; // 构造ConnectionFactory实例对象，此处采用ActiveMq的实现jar<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; connectionFactory = new ActiveMQConnectionFactory(<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; ActiveMQConnection.DEFAULT_USER,<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; ActiveMQConnection.DEFAULT_PASSWORD,<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; "tcp://localhost:61616");<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; try {<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; // 构造从工厂得到连接对象<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; connection = connectionFactory.createConnection();<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; // 启动<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; connection.start();<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; // 获取操作连接<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; session = connection.createSession(Boolean.TRUE,<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; Session.AUTO_ACKNOWLEDGE);<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; // 获取session注意参数值xingbo.xu-queue是一个服务器的queue，须在在ActiveMq的console配置<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; destination = session.createQueue("FirstQueue");<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; // 得到消息生成者【发送者】<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; producer = session.createProducer(destination);<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; // 设置不持久化，此处学习，实际根据项目决定<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; producer.setDeliveryMode(DeliveryMode.NON_PERSISTENT);<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; // 构造消息，此处写死，项目就是参数，或者方法获取<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; sendMessage(session, producer);<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; session.commit();<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; } catch (Exception e) {<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; e.printStackTrace();<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; } finally {<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; try {<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; if (null != connection)<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; connection.close();<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; } catch (Throwable ignore) {<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; }<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; }<br />&nbsp;&nbsp;&nbsp; }<br /><br />&nbsp;&nbsp;&nbsp; public static void sendMessage(Session session, MessageProducer producer)<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; throws Exception {<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; for (int i = 1; i &lt;= SEND_NUMBER; i++) {<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; TextMessage message = session<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; .createTextMessage("ActiveMq 发送的消息" + i);<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; // 发送消息到目的地方<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; System.out.println("发送消息：" + "ActiveMq 发送的消息" + i);<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; producer.send(message);<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; }<br />&nbsp;&nbsp;&nbsp; }<br />}</p>
<p>&nbsp;</p>
<p>&nbsp;</p>
<p>&nbsp;</p>
<p>&nbsp;</p>
<p>3.2.Receiver.java</p>
<p>package com.xuwei.activemq;<br /><br />import javax.jms.Connection;<br />import javax.jms.ConnectionFactory;<br />import javax.jms.Destination;<br />import javax.jms.MessageConsumer;<br />import javax.jms.Session;<br />import javax.jms.TextMessage;<br />import org.apache.activemq.ActiveMQConnection;<br />import org.apache.activemq.ActiveMQConnectionFactory;<br /><br />public class Receiver {<br />&nbsp;&nbsp;&nbsp; public static void main(String[] args) {<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; // ConnectionFactory ：连接工厂，JMS 用它创建连接<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; ConnectionFactory connectionFactory;<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; // Connection ：JMS 客户端到JMS Provider 的连接<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; Connection connection = null;<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; // Session： 一个发送或接收消息的线程<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; Session session;<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; // Destination ：消息的目的地;消息发送给谁.<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; Destination destination;<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; // 消费者，消息接收者<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; MessageConsumer consumer;<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; connectionFactory = new ActiveMQConnectionFactory(<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; ActiveMQConnection.DEFAULT_USER,<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; ActiveMQConnection.DEFAULT_PASSWORD,<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; "tcp://localhost:61616");<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; try {<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; // 构造从工厂得到连接对象<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; connection = connectionFactory.createConnection();<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; // 启动<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; connection.start();<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; // 获取操作连接<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; session = connection.createSession(Boolean.FALSE,<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; Session.AUTO_ACKNOWLEDGE);<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; // 获取session注意参数值xingbo.xu-queue是一个服务器的queue，须在在ActiveMq的console配置<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; destination = session.createQueue("FirstQueue");<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; consumer = session.createConsumer(destination);<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; while (true) {<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; //设置接收者接收消息的时间，为了便于测试，这里谁定为100s<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; TextMessage message = (TextMessage) consumer.receive(100000);<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; if (null != message) {<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; System.out.println("收到消息" + message.getText());<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; } else {<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; break;<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; }<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; }<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; } catch (Exception e) {<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; e.printStackTrace();<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; } finally {<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; try {<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; if (null != connection)<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; connection.close();<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; } catch (Throwable ignore) {<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; }<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; }<br />&nbsp;&nbsp;&nbsp; }<br />}</p>
<p>&nbsp;</p>
<p>&nbsp;</p>
<p>&nbsp;</p>
<h2>4.注意事项</h2>
<ol><li>最后接收者跟发送者在不同的机器上测试</li><li>项目所引用的jar最后在ActiveMQ下的lib中找，这样不会出现版本冲突。 </li></ol>
<h2>5.测试过程</h2>
<p>因为是在单机上测试，所以需要开启两个eclipse，每一个eclipse都有自身的workspace。我们在eclipse1中运行Receiver，在eclipse2中运行Sender。</p>
<p>刚开始eclipse1中运行Receiver以后console介面没有任何信息，在eclipse2中运行Sender以后，eclipse2中的console显示如下信息：</p>
<p><span style="color: #3366ff">发送消息：ActiveMq 发送的消息1</span><br /><span style="color: #3366ff">发送消息：ActiveMq 发送的消息2</span><br /><span style="color: #3366ff">发送消息：ActiveMq 发送的消息3</span><br /><span style="color: #3366ff">发送消息：ActiveMq 发送的消息4</span><br /><span style="color: #3366ff">发送消息：ActiveMq 发送的消息5</span></p>
<p>而回到eclipse1中发现console界面出现如下信息：</p>
<p><span style="color: #800080">收到消息ActiveMq 发送的消息1</span><br /><span style="color: #800080">收到消息ActiveMq 发送的消息2</span><br /><span style="color: #800080">收到消息ActiveMq 发送的消息3</span><br /><span style="color: #800080">收到消息ActiveMq 发送的消息4</span><br /><span style="color: #800080">收到消息ActiveMq 发送的消息5</span></p>
<h2>&nbsp;PS:2012-2-27</h2>
<p>今天发现测试并不需要开启两个eclipse，在一个eclipse下页可以启动多个程序，并且有多个console，在上面的Receiver.java中，设置一个较大的时间，比如receive(500000)，如下代码所示：</p>
<div><pre>TextMessage message = (TextMessage) consumer.receive(500000);</pre></div>
<p>这个时候运行Receiver.java的话，会使得这个Receiver.java一直运行500秒，在eclipse中可以发现：</p>
<p><img alt="" src="http://hiphotos.baidu.com/stevenjohn/pic/item/24741d559822720e3bcd98557bcb0a46f31fabac.jpg" /></p>
<p>点击那个红色方块可以手动停止运行程序。</p>
<p>运行玩receiver以后我们在运行sender，在运行完sender以后，我们要切换到receiver的console，如下图所示：</p>
<p><img alt="" src="http://hiphotos.baidu.com/stevenjohn/pic/item/bdd11525720e0cf3eff434540a46f21fbf09aaac.jpg" /><br /><br /><br /><br /><a href="http://www.cnblogs.com/xwdreamer/archive/2012/02/21/2360818.html">http://www.cnblogs.com/xwdreamer/archive/2012/02/21/2360818.html</a></p>
<p><br /></p>
<p>&nbsp;</p></div></div><img src ="http://www.blogjava.net/stevenjohn/aggbug/382440.html" width = "1" height = "1" /><br><br><div align=right><a style="text-decoration:none;" href="http://www.blogjava.net/stevenjohn/" target="_blank">abin</a> 2012-07-07 14:47 <a href="http://www.blogjava.net/stevenjohn/archive/2012/07/07/382440.html#Feedback" target="_blank" style="text-decoration:none;">发表评论</a></div>]]></description></item></channel></rss>