- 浏览: 234924 次
- 性别:
- 来自: 杭州
-
文章分类
最新评论
ACTIVEMQ相关知识点
<p class="MsoNormal" style="margin: 0cm 0cm 0pt;"><span style="font-size: small;"><span lang="EN-US"><span style="font-family: Calibri;">JMS</span></span><span style="">始终在</span><span lang="EN-US"><span style="font-family: Calibri;">JavaEE</span></span><span style="">五花八门的协议里,</span><span lang="EN-US"><span style="font-family: Calibri;">WebService</span></span><span style="">满天飞的时候占一位置,是因为:</span></span></p>
<p class="MsoNormal" style="margin: 0cm 0cm 0pt;"><span style="" lang="EN-US"></span></p>
<p class="MsoNormal" style="margin: 0cm 0cm 0pt;"><span style="">它可以把不影响用户执行结果又比较耗时的任务(比如发邮件通知管理员)异步的扔给</span><span style="" lang="EN-US">JMS </span><span style="">服务端去做,而尽快的把屏幕返还给用户。</span><span style="" lang="EN-US"> </span></p>
<p class="MsoNormal" style="margin: 0cm 0cm 0pt;"><span style="">服务端能够多线程排队响应高并发的请求。</span><span style="" lang="EN-US"> </span></p>
<p class="MsoNormal" style="margin: 0cm 0cm 0pt;"><span style="">可以在</span><span style="" lang="EN-US">Java</span><span style="">世界里达到最高的解耦。客户端与服务端无需直连,甚至无需知晓对方是谁、在哪里、有多少人,只要对流过的信息作响应就行了,在企业应用环境复杂时作用明显。</span><span style=""> <span lang="EN-US"></span></span></p>
<p class="MsoNormal" style="margin: 0cm 0cm 0pt;"><span lang="EN-US"><span style="font-size: small; font-family: Calibri;"></span></span></p>
<p class="MsoNormal" style="margin: 0cm 0cm 0pt;"><span style="" lang="EN-US">JMS(Java Message Service)</span><span style="">即</span><span style="" lang="EN-US"> java</span><span style="">消息服务</span><span style=""> </span><span style="">用于分布式系统得消息通信,</span><span style="" lang="EN-US">JMS</span><span style="">是</span><span style="" lang="EN-US">J2EE</span><span style="">技术中的一个部分,</span><span style="" lang="EN-US">Sun</span><span style="">公司定义了</span><span style="" lang="EN-US">JMS</span><span style="">的标准接口,即</span><span style="" lang="EN-US">javax.jms.*</span><span style="">。</span><span style="" lang="EN-US"><br><br>JMS</span><span style="">服务使得分布式系统的信息通信松散连接的,发送信息的客户端只需要负责发送信息,接收信息的客户端接收信息,两个客户端之间没有必要是同时可用的,甚至发送客户端都没有必要知道客户端的信息,只要负责发送到接收的服务端就可以,</span><span style="" lang="EN-US">JMS</span><span style="">可以说是低耦合的。</span><span style="" lang="EN-US"><br><br></span><span style="">同时</span><span style="" lang="EN-US">JMS API </span><span style="">做到了以下</span><span style="" lang="EN-US">2</span><span style="">点</span><span style="" lang="EN-US"><br></span><span style="">异步的,服务端可以发送信息到一个客户端,客户端不需要为了收到信息而请求信息。</span><span style="" lang="EN-US"><br><br></span><span style="">可靠的,</span><span style="" lang="EN-US">JMS API</span><span style="">保证了服务端所有发送的信息最少发送一次和只发送一次</span><span style="" lang="EN-US"><br><br></span><span style="">下面介绍一些</span><span style="" lang="EN-US">JMS API </span><span style="">的基本知识</span><span style="" lang="EN-US"><br><br>JMS API </span><span style="">基本组成</span><span style="" lang="EN-US"><br><br> JMS provider</span><span style="">服务者,是一个消息系统通过实现</span><span style="" lang="EN-US">JMS API </span><span style="">接口,用于管理和控制信息。</span><span style="font-size: 9pt; color: black; font-family: Symbol;" lang="EN-US"><span style="">l</span></span><span style="" lang="EN-US"><br></span><span style="font-size: 9pt; color: black; font-family: Symbol;" lang="EN-US"><span style="">l</span></span><span style="" lang="EN-US"> JMS client </span><span style="">客户端</span><span style="" lang="EN-US"></span><span style="">用于发送和接收信息,通常是用在</span><span style="" lang="EN-US">java</span><span style="">程序中用</span><span style="" lang="EN-US">java</span><span style="">编写的</span><span style="" lang="EN-US"><br> Messages</span><span style="font-size: 9pt; color: black; font-family: Symbol;" lang="EN-US"><span style="">l</span></span><span style="" lang="EN-US"> </span><span style="">用于在客户端信息通信的对象</span><span style="" lang="EN-US"><br> Administered Objects </span><span style="">由</span><span style="" lang="EN-US">JMS</span><span style="font-size: 9pt; color: black; font-family: Symbol;" lang="EN-US"><span style="">l</span></span><span style="" lang="EN-US"> provider</span><span style="">为了</span><span style="" lang="EN-US">client</span><span style="">创建的对象,通常是</span><span style="" lang="EN-US">connectionFactory</span><span style="">和</span><span style="" lang="EN-US">destination<br><br>Messages </span><span style="">通信方式</span><span style="" lang="EN-US"><br>JMS</span><span style="">通信方式分为点对点通信,和发布</span><span style="" lang="EN-US">/</span><span style="">订阅方式</span><span style="" lang="EN-US"><br><br></span><span style="">点对点方式(</span><span style="" lang="EN-US">point-to-point</span><span style="">)</span><span style="" lang="EN-US"><br></span><span style="">点对点的消息发送方式主要建立在</span><span style="" lang="EN-US"> Message Queue,Sender,reciever</span><span style="">上,</span><span style="" lang="EN-US">Message Queue </span><span style="">存贮消息,</span><span style="" lang="EN-US">Sneder </span><span style="">发送消息,</span><span style="" lang="EN-US">receive</span><span style="">接收消息,</span><span style="" lang="EN-US"><br><br></span><span style="">具体的信息就是</span><span style="" lang="EN-US"> Sender Client</span><span style="">发送</span><span style="" lang="EN-US">Message </span><span style="">到</span><span style="" lang="EN-US">Queue ,</span><span style="">而</span><span style="" lang="EN-US"> receiver Cliernt </span><span style="">从</span><span style="" lang="EN-US">Queue</span><span style="">中接收消息和发送消息已接受到</span><span style="" lang="EN-US">Quere,</span><span style="">确认消息接收。</span><span style="" lang="EN-US"><br><br></span><span style="">在使用点对点方式时需要注意,</span><span style="" lang="EN-US"><br><br></span><span style="">一条消息只有一个接收端,消息发送客户端与接收客户端没有时间上的依赖,发送客户端可以在任何时刻发送信息到</span><span style="" lang="EN-US">Queue</span><span style="">,而不需要知道接收客户端是不是在运行</span><span style="" lang="EN-US"><br><br></span><span style="">发布</span><span style="" lang="EN-US">/</span><span style="">订阅</span><span style=""> </span><span style="">方式(</span><span style="" lang="EN-US">publish/subscriber Messaging</span><span style="">)</span><span style="" lang="EN-US"><br></span><span style="">发布</span><span style="" lang="EN-US">/</span><span style="">订阅方式用于多接收客户端的方式</span><span style="" lang="EN-US"><br></span><span style="">作为发布订阅的方式,可能存在多个接收客户端,并且接收端客户端与发送客户端存在时间上的依赖。一个接收端只能接收他创建以后发送客户端发送的信息。</span><span style="" lang="EN-US"><br><br></span><span style="">作为</span><span style="" lang="EN-US">subscriber ,</span><span style="">在接收消息时有两种方法,</span><span style="" lang="EN-US">destination</span><span style="">的</span><span style="" lang="EN-US">receive</span><span style="">方法,和实现</span><span style="" lang="EN-US">message listener </span><span style="">接口的</span><span style="" lang="EN-US">onMessage </span><span style="">方法。</span><span style="" lang="EN-US"><br><br></span><span style="">基础知识这里说的很少,具体的可以参照</span><span style="" lang="EN-US">java.sun.com.</span><span style="">的</span><span style="" lang="EN-US">JMS API </span><span style="">指南</span></p>
<p class="MsoNormal" style="margin: 0cm 0cm 0pt;"><span style="font-size: small;"><span lang="EN-US"><span style="font-family: Calibri;"><span style=""> </span>ActiveMQ</span></span><span style="">的特性:</span></span></p>
<p class="MsoNormal" style="margin: 0cm 0cm 0pt;"><span lang="EN-US"><span style="font-size: small; font-family: Calibri;"></span></span></p>
<p class="MsoNormal" style="margin: 0cm 0cm 0pt;"><span style="">完全支持</span><span style="" lang="EN-US">JMS1.1</span><span style="">和</span><span style="" lang="EN-US">J2EE 1.4</span><span style="">规范的</span><span style="" lang="EN-US"> JMS Provider</span><span style="">实现,也是</span><span style="" lang="EN-US">Apache Geronimo</span><span style="">默认的</span><span style="" lang="EN-US">JMS provider</span><span style="">。</span><span style="" lang="EN-US"> </span></p>
<p class="MsoNormal" style="margin: 0cm 0cm 0pt;"><span style="" lang="EN-US">POJO withdout EJB Container</span><span style="">,不需要实现</span><span style="" lang="EN-US">EJB</span><span style="">繁琐复杂的</span><span style="" lang="EN-US">Message Bean</span><span style="">接口和配置。</span><span style="" lang="EN-US"> </span></p>
<p class="MsoNormal" style="margin: 0cm 0cm 0pt;"><span style="" lang="EN-US">Spring Base</span><span style="">,可以使用</span><span style="" lang="EN-US">Spring</span><span style="">的各种特性如</span><span style="" lang="EN-US">IOC</span><span style="">、</span><span style="" lang="EN-US">AOP </span><span style="">。</span><span style="" lang="EN-US"> </span></p>
<p class="MsoNormal" style="margin: 0cm 0cm 0pt;"><span style="" lang="EN-US">Effective</span><span style="">,基于</span><span style="" lang="EN-US">Jencks</span><span style="">的</span><span style="" lang="EN-US">JCA Container</span><span style="">实现</span><span style="" lang="EN-US"> pool connection</span><span style="">,</span><span style="" lang="EN-US">control transactions and manage security</span><span style="">。</span><span style="" lang="EN-US"><span style=""> </span></span></p>
<p class="MsoNormal" style="margin: 0cm 0cm 0pt;"><span style="">下面是它的特性列表</span><span style="" lang="EN-US"> (</span><span style="">详细的特性清单请参考</span><span style="" lang="EN-US"><a href="http://activemq.apache.org/features.html"><span style="color: black; text-decoration: none;">http://activemq.apache.org/features.html</span></a>)<br> 1.</span><span style="">多种语言和协议编写客户端</span><span style="" lang="EN-US"><br></span><span style="">语言</span><span style="" lang="EN-US">: Java, C, C++, C#, Ruby, Perl, Python, PHP <br></span><span style="">应用协议</span><span style="" lang="EN-US">: OpenWire,Stomp REST,WS Notification,XMPP,AMQP<br> 2.</span><span style="">完全支持</span><span style="" lang="EN-US">JMS1.1</span><span style="">和</span><span style="" lang="EN-US">J2EE 1.4</span><span style="">规范</span><span style="" lang="EN-US"> (</span><span style="">持久化</span><span style="" lang="EN-US">,XA</span><span style="">消息</span><span style="" lang="EN-US">,</span><span style="">事务</span><span style="" lang="EN-US">)<br> 3.</span><span style="">对</span><span style="" lang="EN-US">Spring</span><span style="">的支持</span><span style="" lang="EN-US">,ActiveMQ</span><span style="">可以很容得内嵌到使用</span><span style="" lang="EN-US">Spring</span><span style="">的系统里面去</span><span style="" lang="EN-US">,</span><span style="">而且也支持</span><span style="" lang="EN-US">Spring2.0</span><span style="">的特性</span><span style="" lang="EN-US"><br>4.</span><span style="">通过了常见</span><span style="" lang="EN-US">J2EE</span><span style="">服务器</span><span style="" lang="EN-US">(</span><span style="">如</span><span style="" lang="EN-US"> Geronimo,JBoss 4, GlassFish,WebLogic)</span><span style="">的测试</span><span style="" lang="EN-US">,</span><span style="">其中通过</span><span style="" lang="EN-US">JCA 1.5 resource adaptors</span><span style="">的配置</span><span style="" lang="EN-US">,<br></span><span style="">可以让</span><span style="" lang="EN-US">ActiveMQ</span><span style="">可以自动的部署到任何兼容</span><span style="" lang="EN-US">J2EE 1.4 </span><span style="">商业服务器上</span><span style="" lang="EN-US"><br>5.</span><span style="">支持多种传送协议</span><span style="" lang="EN-US">:in-VM,TCP,SSL,NIO,UDP,JGroups,JXTA<br>6.</span><span style="">支持通过</span><span style="" lang="EN-US">JDBC</span><span style="">和</span><span style="" lang="EN-US">journal</span><span style="">提供高速的消息持久化</span><span style="" lang="EN-US"><br>7.</span><span style="">从设计上保证了高性能的集群</span><span style="" lang="EN-US">,</span><span style="">客户端</span><span style="" lang="EN-US">-</span><span style="">服务器</span><span style="" lang="EN-US">,</span><span style="">点对点</span><span style="" lang="EN-US"><br>8.</span><span style="">支持</span><span style="" lang="EN-US">Ajax<br>9.</span><span style="">支持与</span><span style="" lang="EN-US">Axis</span><span style="">的整合</span><span style="" lang="EN-US"><br>10.</span><span style="">可以很容易得调用内嵌</span><span style="" lang="EN-US">JMS provider,</span><span style="">进行测试</span><span style="" lang="EN-US"></span></p>
<p class="MsoNormal" style="margin: 0cm 0cm 0pt;"><span lang="EN-US"><span style="font-size: small; font-family: Calibri;"></span></span></p>
<p class="MsoNormal" style="margin: 0cm 0cm 0pt;"><span style="">作为一个</span><span style="" lang="EN-US">JMS client </span><span style="">都需要以下的组成</span><span style="" lang="EN-US"><br>Administered Objects</span><span style="">:</span><span style="" lang="EN-US">ConnectionFactory</span><span style="">,</span><span style="" lang="EN-US">Destination<br>Connection</span><span style="">;</span><span style="" lang="EN-US"><br>Session<br>Message Producers/Message Cosumers<br>Messages</span></p>
<p class="MsoNormal" style="margin: 0cm 0cm 0pt;"><span lang="EN-US"><span style="font-size: small; font-family: Calibri;"></span></span></p>
<p class="MsoNormal" style="margin: 0cm 0cm 0pt;"><span style="font-size: small;"><span lang="EN-US"><span style="font-family: Calibri;">Hello</span></span><span style="">实例</span></span></p>
<p class="MsoNormal" style="margin: 0cm 0cm 0pt;"><span style="font-size: small;"><span lang="EN-US"><span style="font-family: Calibri;">=================================Hello.java===========================<br></span></span><span style=""> </span></span><span lang="EN-US"><br></span><span style=""><span style="font-size: small;"> </span></span><span style="font-size: small;"><span lang="EN-US"><span style="font-family: Calibri;">import java.io.Serializable;<br></span></span><span style=""> </span></span><span lang="EN-US"><br></span><span style="font-size: small;"><span style=""> </span><span lang="EN-US"><span style="font-family: Calibri;">/**Hello.java</span></span><span style="">用来传递</span><span lang="EN-US"><span style="font-family: Calibri;">JAVA</span></span><span style="">对象</span></span><span lang="EN-US"><br></span><span style=""><span style="font-size: small;"> </span></span><span style="font-size: small;"><span lang="EN-US"><span style="font-family: Calibri;">* Author: cjp<br></span></span><span style=""> </span></span><span style="font-size: small;"><span lang="EN-US"><span style="font-family: Calibri;">* Date: 2005-11-8<br></span></span><span style=""> </span></span><span style="font-size: small;"><span lang="EN-US"><span style="font-family: Calibri;">* Time: 22:24:02<br></span></span><span style=""> </span></span><span style="font-size: small;"><span lang="EN-US"><span style="font-family: Calibri;">*/<br></span></span><span style=""> </span></span><span style="font-size: small;"><span lang="EN-US"><span style="font-family: Calibri;">public class Hello implements Serializable {<br></span></span><span style=""> </span></span><span style="font-size: small;"><span lang="EN-US"><span style="font-family: Calibri;">private String id;<br></span></span><span style=""> </span></span><span style="font-size: small;"><span lang="EN-US"><span style="font-family: Calibri;">private Hello hello;<br></span></span><span style=""> </span></span><span style="font-size: small;"><span lang="EN-US"><span style="font-family: Calibri;">private PointList pointList;<br></span></span><span style=""> </span></span><span style="font-size: small;"><span lang="EN-US"><span style="font-family: Calibri;">public String getId() {<br></span></span><span style=""> </span></span><span style="font-size: small;"><span lang="EN-US"><span style="font-family: Calibri;">return id;<br></span></span><span style=""> </span></span><span style="font-size: small;"><span lang="EN-US"><span style="font-family: Calibri;">}<br></span></span><span style=""> </span></span><span lang="EN-US"><br></span><span style=""><span style="font-size: small;"> </span></span><span style="font-size: small;"><span lang="EN-US"><span style="font-family: Calibri;">public void setId(String id) {<br></span></span><span style=""> </span></span><span style="font-size: small;"><span lang="EN-US"><span style="font-family: Calibri;">this.id = id;<br></span></span><span style=""> </span></span><span style="font-size: small;"><span lang="EN-US"><span style="font-family: Calibri;">}<br></span></span><span style=""> </span></span><span lang="EN-US"><br></span><span style=""><span style="font-size: small;"> </span></span><span style="font-size: small;"><span lang="EN-US"><span style="font-family: Calibri;">public Hello getHello() {<br></span></span><span style=""> </span></span><span style="font-size: small;"><span lang="EN-US"><span style="font-family: Calibri;">return hello;<br></span></span><span style=""> </span></span><span style="font-size: small;"><span lang="EN-US"><span style="font-family: Calibri;">}<br></span></span><span style=""> </span></span><span lang="EN-US"><br></span><span style=""><span style="font-size: small;"> </span></span><span style="font-size: small;"><span lang="EN-US"><span style="font-family: Calibri;">public void setHello(Hello hello) {<br></span></span><span style=""> </span></span><span style="font-size: small;"><span lang="EN-US"><span style="font-family: Calibri;">this.hello = hello;<br></span></span><span style=""> </span></span><span style="font-size: small;"><span lang="EN-US"><span style="font-family: Calibri;">}<br></span></span><span style=""> </span></span><span style="font-size: small;"><span lang="EN-US"><span style="font-family: Calibri;">}<br></span></span><span style=""> </span></span><span lang="EN-US"><br></span><span style=""><span style="font-size: small;"> </span></span><span style="font-size: small;"><span lang="EN-US"><span style="font-family: Calibri;">=========================SpringTest .java========================<br></span></span><span style=""> </span></span><span lang="EN-US"><br></span><span style=""><span style="font-size: small;"> </span></span><span style="font-size: small;"><span lang="EN-US"><span style="font-family: Calibri;">import org.springframework.jms.core.JmsTemplate;<br></span></span><span style=""> </span></span><span style="font-size: small;"><span lang="EN-US"><span style="font-family: Calibri;">import org.springframework.jms.core.MessageCreator;<br></span></span><span style=""> </span></span><span style="font-size: small;"><span lang="EN-US"><span style="font-family: Calibri;">import org.springframework.test.AbstractDependencyInjectionSpringContextTests;<br></span></span><span style=""> </span></span><span lang="EN-US"><br></span><span style=""><span style="font-size: small;"> </span></span><span style="font-size: small;"><span lang="EN-US"><span style="font-family: Calibri;">import javax.jms.*;<br></span></span><span style=""> </span></span><span lang="EN-US"><br></span><span style=""><span style="font-size: small;"> </span></span><span style="font-size: small;"><span lang="EN-US"><span style="font-family: Calibri;">/**<br></span></span><span style=""> </span></span><span lang="EN-US"><br></span><span style="font-size: small;"><span style=""> </span><span lang="EN-US"><span style="font-family: Calibri;">*</span></span><span style="">发送</span><span lang="EN-US"><span style="font-family: Calibri;">JMS</span></span><span style="">消息</span></span><span lang="EN-US"><br></span><span style=""><span style="font-size: small;"> </span></span><span lang="EN-US"><br></span><span style=""><span style="font-size: small;"> </span></span><span style="font-size: small;"><span lang="EN-US"><span style="font-family: Calibri;">*/<br></span></span><span style=""> </span></span><span style="font-size: small;"><span lang="EN-US"><span style="font-family: Calibri;">public class SpringTest extends AbstractDependencyInjectionSpringContextTests<br></span></span><span style=""> </span></span><span style="font-size: small;"><span lang="EN-US"><span style="font-family: Calibri;">{<br></span></span><span style=""> </span></span><span style="font-size: small;"><span lang="EN-US"><span style="font-family: Calibri;">protected String[] getConfigLocations()<br></span></span><span style=""> </span></span><span style="font-size: small;"><span lang="EN-US"><span style="font-family: Calibri;">{<br></span></span><span style=""> </span></span><span style="font-size: small;"><span lang="EN-US"><span style="font-family: Calibri;">return new String[]{"file:D://wosame//test//com//wosame//room//jms//jms.xml"};<br></span></span><span style=""> </span></span><span style="font-size: small;"><span lang="EN-US"><span style="font-family: Calibri;">}<br></span></span><span style=""> </span></span><span lang="EN-US"><br></span><span style=""><span style="font-size: small;"> </span></span><span style="font-size: small;"><span lang="EN-US"><span style="font-family: Calibri;">public void testSendMessage() throws Exception<br></span></span><span style=""> </span></span><span style="font-size: small;"><span lang="EN-US"><span style="font-family: Calibri;">{<br></span></span><span style=""> </span></span><span style="font-size: small;"><span lang="EN-US"><span style="font-family: Calibri;">JmsTemplate jmsTemplate = (JmsTemplate) applicationContext.getBean("jmsTemplate");<br></span></span><span style=""> </span></span><span style="font-size: small;"><span lang="EN-US"><span style="font-family: Calibri;">jmsTemplate.send(new MessageCreator()<br></span></span><span style=""> </span></span><span style="font-size: small;"><span lang="EN-US"><span style="font-family: Calibri;">{<br></span></span><span style=""> </span></span><span style="font-size: small;"><span lang="EN-US"><span style="font-family: Calibri;">public Message createMessage(Session session) throws JMSException<br></span></span><span style=""> </span></span><span style="font-size: small;"><span lang="EN-US"><span style="font-family: Calibri;">{<br></span></span><span style=""> </span></span><span style="font-size: small;"><span lang="EN-US"><span style="font-family: Calibri;">ObjectMessage message=session.createObjectMessage();<br></span></span><span style=""> </span></span><span style="font-size: small;"><span lang="EN-US"><span style="font-family: Calibri;">Hello hello=new Hello();<br></span></span><span style=""> </span></span><span style="font-size: small;"><span lang="EN-US"><span style="font-family: Calibri;">hello.setId("test");<br></span></span><span style=""> </span></span><span style="font-size: small;"><span lang="EN-US"><span style="font-family: Calibri;">message.setObject(hello);<br></span></span><span style=""> </span></span><span style="font-size: small;"><span lang="EN-US"><span style="font-family: Calibri;">return message;<br></span></span><span style=""> </span></span><span style="font-size: small;"><span lang="EN-US"><span style="font-family: Calibri;">}<br></span></span><span style=""> </span></span><span style="font-size: small;"><span lang="EN-US"><span style="font-family: Calibri;">});<br></span></span><span style=""> </span></span><span style="font-size: small;"><span lang="EN-US"><span style="font-family: Calibri;">}<br></span></span><span style=""> </span></span><span style="font-size: small;"><span lang="EN-US"><span style="font-family: Calibri;">}<br></span></span><span style=""> </span></span><span lang="EN-US"><br></span><span style=""><span style="font-size: small;"> </span></span><span style="font-size: small;"><span lang="EN-US"><span style="font-family: Calibri;">================================HelloMDP .java==================================<br></span></span><span style=""> </span></span><span lang="EN-US"><br></span><span style=""><span style="font-size: small;"> </span></span><span style="font-size: small;"><span lang="EN-US"><span style="font-family: Calibri;">/**<br></span></span><span style=""> </span></span><span lang="EN-US"><br></span><span style="font-size: small;"><span style=""> 处理</span><span lang="EN-US"><span style="font-family: Calibri;">JMS</span></span><span style="">消息</span></span><span lang="EN-US"><br></span><span style=""><span style="font-size: small;"> </span></span><span lang="EN-US"><br></span><span style=""><span style="font-size: small;"> </span></span><span style="font-size: small;"><span lang="EN-US"><span style="font-family: Calibri;">*/<br></span></span><span style=""> </span></span><span lang="EN-US"><br></span><span style=""><span style="font-size: small;"> </span></span><span style="font-size: small;"><span lang="EN-US"><span style="font-family: Calibri;">import org.apache.commons.logging.Log;<br></span></span><span style=""> </span></span><span style="font-size: small;"><span lang="EN-US"><span style="font-family: Calibri;">import org.apache.commons.logging.LogFactory;<br></span></span><span style=""> </span></span><span style="font-size: small;"><span lang="EN-US"><span style="font-family: Calibri;">import javax.jms.*;<br></span></span><span style=""> </span></span><span lang="EN-US"><br></span><span style=""><span style="font-size: small;"> </span></span><span style="font-size: small;"><span lang="EN-US"><span style="font-family: Calibri;">public class HelloMDP implements MessageListener<br></span></span><span style=""> </span></span><span style="font-size: small;"><span lang="EN-US"><span style="font-family: Calibri;">{<br></span></span><span style=""> </span></span><span style="font-size: small;"><span lang="EN-US"><span style="font-family: Calibri;">protected Log log = LogFactory.getLog(HelloMDP.class);<br></span></span><span style=""> </span></span><span lang="EN-US"><br></span><span style=""><span style="font-size: small;"> </span></span><span style="font-size: small;"><span lang="EN-US"><span style="font-family: Calibri;">public void onMessage(Message message)<br></span></span><span style=""> </span></span><span style="font-size: small;"><span lang="EN-US"><span style="font-family: Calibri;">{<br></span></span><span style=""> </span></span><span style="font-size: small;"><span lang="EN-US"><span style="font-family: Calibri;">try<br></span></span><span style=""> </span></span><span style="font-size: small;"><span lang="EN-US"><span style="font-family: Calibri;">{<br></span></span><span style=""> </span></span><span style="font-size: small;"><span lang="EN-US"><span style="font-family: Calibri;">ObjectMessage objMessage = (ObjectMessage) message;<br></span></span><span style=""> </span></span><span style="font-size: small;"><span lang="EN-US"><span style="font-family: Calibri;">Hello hello= (Hello) objMessage.getObject();<br></span></span><span style=""> </span></span><span style="font-size: small;"><span lang="EN-US"><span style="font-family: Calibri;">System.out.println("hello.getId() = " + hello.getId());<br></span></span><span style=""> </span></span><span style="font-size: small;"><span lang="EN-US"><span style="font-family: Calibri;">} catch (JMSException e)<br></span></span><span style=""> </span></span><span style="font-size: small;"><span lang="EN-US"><span style="font-family: Calibri;">{<br></span></span><span style=""> </span></span><span style="font-size: small;"><span lang="EN-US"><span style="font-family: Calibri;">log.error("Parse failed", e);<br></span></span><span style=""> </span></span><span style="font-size: small;"><span lang="EN-US"><span style="font-family: Calibri;">}<br></span></span><span style=""> </span></span><span style="font-size: small;"><span lang="EN-US"><span style="font-family: Calibri;">}<br></span></span><span style=""> </span></span><span style="font-size: small;"><span lang="EN-US"><span style="font-family: Calibri;">}<br></span></span><span style=""> </span></span><span lang="EN-US"><br></span><span style=""><span style="font-size: small;"> </span></span><span style="font-size: small;"><span lang="EN-US"><span style="font-family: Calibri;">================================jms.xml==================================<br></span></span><span style=""> </span></span><span lang="EN-US"><br></span><span style=""><span style="font-size: small;"> </span></span><span style="font-size: small;"><span lang="EN-US"><span style="font-family: Calibri;"><?xml version="1.0" encoding="UTF-8"?><br></span></span><span style=""> </span></span><span style="font-size: small;"><span lang="EN-US"><span style="font-family: Calibri;"><!DOCTYPE beans PUBLIC "-//SPRING//DTD BEAN//EN"<br></span></span><span style=""> </span></span><span style="font-size: small;"><span lang="EN-US"><span style="font-family: Calibri;">"http://www.springframework.org/dtd/spring-beans.dtd"><br></span></span><span style=""> </span></span><span lang="EN-US"><br></span><span style=""><span style="font-size: small;"> </span></span><span style="font-size: small;"><span lang="EN-US"><span style="font-family: Calibri;"><beans><br></span></span><span style=""> </span></span><span lang="EN-US"><br></span><span style=""><span style="font-size: small;"> </span></span><span lang="EN-US"><span style="font-size: small; font-family: Calibri;"><!--</span><a href="http://embed.chinaitlab.com/" target="_blank"><span style="" lang="EN-US"><span lang="EN-US">嵌入式</span></span></a></span><span style="font-size: small;"><span style="">的</span><span lang="EN-US"><span style="font-family: Calibri;">JMS</span></span><span style="">连接,也就是跟随</span><span lang="EN-US"><span style="font-family: Calibri;">JVM</span></span><span style="">一起启动</span><span lang="EN-US"><span style="font-family: Calibri;">,</span></span><span style="">可以参看</span><span lang="EN-US"><span style="font-family: Calibri;">activemq</span></span><span style="">的文档</span></span><span style="font-size: small;"><span lang="EN-US"><span style="font-family: Calibri;">--><br></span></span><span style=""> </span></span><span style="font-size: small;"><span lang="EN-US"><span style="font-family: Calibri;"><bean id="connectionFactory" class="org.activemq.ActiveMQConnectionFactory"> <property name="brokerURL" value="vm://localhost"/><br></span></span><span style=""> </span></span><span style="font-size: small;"><span lang="EN-US"><span style="font-family: Calibri;"><property name="useEmbeddedBroker" value="true"/><br></span></span><span style=""> </span></span><span style="font-size: small;"><span lang="EN-US"><span style="font-family: Calibri;"></bean><br></span></span><span style=""> </span></span><span lang="EN-US"><br></span><span style="font-size: small;"><span style=""> </span><span lang="EN-US"><span style="font-family: Calibri;"><!--</span></span><span style="">消息监听器,也就是消息的具体的处理器</span></span><span style="font-size: small;"><span lang="EN-US"><span style="font-family: Calibri;">--><br></span></span><span style=""> </span></span><span style="font-size: small;"><span lang="EN-US"><span style="font-family: Calibri;"><bean id="HelloMDP" class="HelloMDP"/><br></span></span><span style=""> </span></span><span lang="EN-US"><br></span><span style="font-size: small;"><span style=""> </span><span lang="EN-US"><span style="font-family: Calibri;"><!--jms</span></span><span style="">监听需要</span><span lang="EN-US"><span style="font-family: Calibri;">JTA</span></span><span style="">容器的支持</span></span><span style="font-size: small;"><span lang="EN-US"><span style="font-family: Calibri;">--><br></span></span><span style=""> </span></span><span style="font-size: small;"><span lang="EN-US"><span style="font-family: Calibri;"><bean id="activeMQContainer" class="org.activemq.jca.JCAContainer"><br></span></span><span style=""> </span></span><span style="font-size: small;"><span lang="EN-US"><span style="font-family: Calibri;"><property name="workManager"><br></span></span><span style=""> </span></span><span style="font-size: small;"><span lang="EN-US"><span style="font-family: Calibri;"><bean id="workManager" class="org.activemq.work.SpringWorkManager"/><br></span></span><span style=""> </span></span><span style="font-size: small;"><span lang="EN-US"><span style="font-family: Calibri;"></property><br></span></span><span style=""> </span></span><span style="font-size: small;"><span lang="EN-US"><span style="font-family: Calibri;"><property name="resourceAdapter"><br></span></span><span style=""> </span></span><span style="font-size: small;"><span lang="EN-US"><span style="font-family: Calibri;"><bean id="activeMQResourceAdapter" class="org.activemq.ra.ActiveMQResourceAdapter"><br></span></span><span style=""> </span></span><span style="font-size: small;"><span lang="EN-US"><span style="font-family: Calibri;"><property name="serverUrl" value="vm://localhost"/><br></span></span><span style=""> </span></span><span style="font-size: small;"><span lang="EN-US"><span style="font-family: Calibri;"></bean><br></span></span><span style=""> </span></span><span style="font-size: small;"><span lang="EN-US"><span style="font-family: Calibri;"></property><br></span></span><span style=""> </span></span><span style="font-size: small;"><span lang="EN-US"><span style="font-family: Calibri;"></bean><br></span></span><span style=""> </span></span><span lang="EN-US"><br></span><span style="font-size: small;"><span style=""> </span><span lang="EN-US"><span style="font-family: Calibri;"><!--</span></span><span style="">消息的消费者,也就是将监听器与具体的队列关联</span></span><span style="font-size: small;"><span lang="EN-US"><span style="font-family: Calibri;">--><br></span></span><span style=""> </span></span><span style="font-size: small;"><span lang="EN-US"><span style="font-family: Calibri;"><bean id="HelloQueueConsumer" factory-method="addConnector" factory-bean="activeMQContainer"><br></span></span><span style=""> </span></span><span style="font-size: small;"><span lang="EN-US"><span style="font-family: Calibri;"><property name="activationSpec"><br></span></span><span style=""> </span></span><span style="font-size: small;"><span lang="EN-US"><span style="font-family: Calibri;"><bean class="org.activemq.ra.ActiveMQActivationSpec"><br></span></span><span style=""> </span></span><span style="font-size: small;"><span lang="EN-US"><span style="font-family: Calibri;"><property name="destination" value="Hello.Queue"/><br></span></span><span style=""> </span></span><span style="font-size: small;"><span lang="EN-US"><span style="font-family: Calibri;"><property name="destinationType" value="javax.jms.Queue"/><br></span></span><span style=""> </span></span><span style="font-size: small;"><span lang="EN-US"><span style="font-family: Calibri;"></bean><br></span></span><span style=""> </span></span><span style="font-size: small;"><span lang="EN-US"><span style="font-family: Calibri;"></property><br></span></span><span style=""> </span></span><span style="font-size: small;"><span lang="EN-US"><span style="font-family: Calibri;"><property name="ref" value="HelloMDP"/><br></span></span><span style=""> </span></span><span style="font-size: small;"><span lang="EN-US"><span style="font-family: Calibri;"></bean><br></span></span><span style=""> </span></span><span lang="EN-US"><br></span><span style="font-size: small;"><span style=""> </span><span lang="EN-US"><span style="font-family: Calibri;"><!--spring</span></span><span style="">的</span><span lang="EN-US"><span style="font-family: Calibri;">JMS template,</span></span><span style="">用来发送</span><span lang="EN-US"><span style="font-family: Calibri;">JMS</span></span><span style="">消息到指定的队列</span></span><span style="font-size: small;"><span lang="EN-US"><span style="font-family: Calibri;">--><br></span></span><span style=""> </span></span><span style="font-size: small;"><span lang="EN-US"><span style="font-family: Calibri;"><bean id="jmsTemplate" class="org.springframework.jms.core.JmsTemplate"><br></span></span><span style=""> </span></span><span style="font-size: small;"><span lang="EN-US"><span style="font-family: Calibri;"><property name="defaultDestinationName" value="Hello.Queue"/><br></span></span><span style=""> </span></span><span style="font-size: small;"><span lang="EN-US"><span style="font-family: Calibri;"><property name="connectionFactory" ref="connectionFactory"/><br></span></span><span style=""> </span></span><span style="font-size: small;"><span lang="EN-US"><span style="font-family: Calibri;"></bean><br></span></span><span style=""> </span><span lang="EN-US"><span style="font-family: Calibri;"></beans></span></span></span></p>
<p class="MsoNormal" style="margin: 0cm 0cm 0pt;"><span lang="EN-US"><span style="font-size: small; font-family: Calibri;"></span></span></p>
<p class="MsoNormal" style="margin: 0cm 0cm 0pt;"><span style="font-size: small;"><span style="">例子二</span><span lang="EN-US"><span style="font-family: Calibri;"> MQ</span></span><span style="">源码</span></span></p>
<p class="MsoNormal" style="margin: 0cm 0cm 0pt;"><span style="" lang="EN-US">import java.util.Arrays;<br>import java.util.Date;<br><br>import javax.jms.Connection;<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><br>import org.apache.activemq.ActiveMQConnection;<br>import org.apache.activemq.ActiveMQConnectionFactory;<br>import org.apache.activemq.util.IndentPrinter;<br><br>/**<br>* A simple tool for publishing messages<br>* <br>* @version $Revision: 1.2 $<br>*/<br>public class ProducerTool {<br><br> private Destination destination;<br> private int messageCount = 10;<br> private long sleepTime = 0L;<br> private boolean verbose = true;<br> private int messageSize = 255;<br> private long timeToLive;<br> private String user = ActiveMQConnection.DEFAULT_USER;<br> private String password = ActiveMQConnection.DEFAULT_PASSWORD;<br> private String url = ActiveMQConnection.DEFAULT_BROKER_URL;<br> private String subject = "TOOL.DEFAULT";<br> private boolean topic = false;<br> private boolean transacted = false;<br> private boolean persistent = false;<br><br> public static void main(String[] args) {<br> ProducerTool producerTool = new ProducerTool();<br> String[] unknonwn = CommnadLineSupport.setOptions(producerTool, args);<br> if( unknonwn.length > 0 ) {<br> System.out.println("Unknown options: "+Arrays.toString(unknonwn));<br> System.exit(-1);<br> } <br> producerTool.run();<br> }<br><br> public void run() {<br> Connection connection=null;<br> try {<br> System.out.println("Connecting to URL: " + url);<br> System.out.println("Publishing a Message with size " + messageSize+ " to " + (topic ? "topic" : "queue"<span style=""> </span>+ ": " + subject);<br> System.out.println("Using " + (persistent ? "persistent" : "non-persistent"<span style=""> </span>+ " messages"<span style=""> </span>;<br> System.out.println("Sleeping between publish " + sleepTime + " ms"<span style=""> </span>;<br> if (timeToLive != 0) {<br> System.out.println("Messages time to live " + timeToLive + " ms"<span style=""> </span>;<br> }<br><br> // Create the connection.<br> ActiveMQConnectionFactory connectionFactory = new ActiveMQConnectionFactory(user, password, url); <br> connection = connectionFactory.createConnection();<br> connection.start();<br><br> // Create the session<br> Session session = connection.createSession(transacted, Session.AUTO_ACKNOWLEDGE);<br> if (topic) {<br> destination = session.createTopic(subject);<br> } else {<br> destination = session.createQueue(subject);<br> }<br><br> // Create the producer.<br> MessageProducer producer = session.createProducer(destination);<br> if (persistent) {<br> producer.setDeliveryMode(DeliveryMode.PERSISTENT);<br> } else {<br> producer.setDeliveryMode(DeliveryMode.NON_PERSISTENT);<br> } <br> if (timeToLive != 0)<br> producer.setTimeToLive(timeToLive);<br><br> // Start sending messages<br> sendLoop(session, producer);<br><br> System.out.println("Done."<span style=""> </span>;<br><br> // Use the ActiveMQConnection interface to dump the connection stats.<br> ActiveMQConnection c = (ActiveMQConnection) connection;<br> c.getConnectionStats().dump(new IndentPrinter());<br><br> } catch (Exception e) {<br> System.out.println("Caught: " + e);<br> e.printStackTrace();<br> } finally {<br> try { <br> connection.close();<br> } catch (Throwable ignore) {<br> }<br> }<br> }<br><br> protected void sendLoop(Session session, MessageProducer producer)<br> throws Exception {<br><br> for (int i = 0; i < messageCount || messageCount == 0; i++) {<br><br> TextMessage message = session<br> .createTextMessage(createMessageText(i));<br><br> if (verbose) {<br> String msg = message.getText();<br> if (msg.length() > 50) {<br> msg = msg.substring(0, 50) + "...";<br> }<br> System.out.println("Sending message: " + msg);<br> }<br><br> producer.send(message);<br> if (transacted) {<br> session.commit();<br> }<br><br> Thread.sleep(sleepTime);<br><br> }<br><br> }<br><br> private String createMessageText(int index) {<br> StringBuffer buffer = new StringBuffer(messageSize);<br> buffer.append("Message: " + index + " sent at: " + new Date());<br> if (buffer.length() > messageSize) {<br> return buffer.substring(0, messageSize);<br> }<br> for (int i = buffer.length(); i < messageSize; i++) {<br> buffer.append(' ');<br> }<br> return buffer.toString();<br> }<br><br><br> public void setPersistent(boolean durable) {<br> this.persistent = durable;<br> }<br> public void setMessageCount(int messageCount) {<br> this.messageCount = messageCount;<br> }<br> public void setMessageSize(int messageSize) {<br> this.messageSize = messageSize;<br> }<br> public void setPassword(String pwd) {<br> this.password = pwd;<br> }<br> public void setSleepTime(long sleepTime) {<br> this.sleepTime = sleepTime;<br> }<br> public void setSubject(String subject) {<br> this.subject = subject;<br> }<br> public void setTimeToLive(long timeToLive) {<br> this.timeToLive = timeToLive;<br> }<br> public void setTopic(boolean topic) {<br> this.topic = topic;<br> }<br> public void setQueue(boolean queue) {<br> this.topic = !queue;<br> } <br> public void setTransacted(boolean transacted) {<br> this.transacted = transacted;<br> }<br> public void setUrl(String url) {<br> this.url = url;<br> }<br> public void setUser(String user) {<br> this.user = user;<br> }<br> public void setVerbose(boolean verbose) {<br> this.verbose = verbose;<br> }<br>}</span></p>
<p class="MsoNormal" style="margin: 0cm 0cm 0pt;"><span style="" lang="EN-US"></span></p>
<p class="MsoNormal" style="margin: 0cm 0cm 0pt;"><span style="">它可以把不影响用户执行结果又比较耗时的任务(比如发邮件通知管理员)异步的扔给</span><span style="" lang="EN-US">JMS </span><span style="">服务端去做,而尽快的把屏幕返还给用户。</span><span style="" lang="EN-US"> </span></p>
<p class="MsoNormal" style="margin: 0cm 0cm 0pt;"><span style="">服务端能够多线程排队响应高并发的请求。</span><span style="" lang="EN-US"> </span></p>
<p class="MsoNormal" style="margin: 0cm 0cm 0pt;"><span style="">可以在</span><span style="" lang="EN-US">Java</span><span style="">世界里达到最高的解耦。客户端与服务端无需直连,甚至无需知晓对方是谁、在哪里、有多少人,只要对流过的信息作响应就行了,在企业应用环境复杂时作用明显。</span><span style=""> <span lang="EN-US"></span></span></p>
<p class="MsoNormal" style="margin: 0cm 0cm 0pt;"><span lang="EN-US"><span style="font-size: small; font-family: Calibri;"></span></span></p>
<p class="MsoNormal" style="margin: 0cm 0cm 0pt;"><span style="" lang="EN-US">JMS(Java Message Service)</span><span style="">即</span><span style="" lang="EN-US"> java</span><span style="">消息服务</span><span style=""> </span><span style="">用于分布式系统得消息通信,</span><span style="" lang="EN-US">JMS</span><span style="">是</span><span style="" lang="EN-US">J2EE</span><span style="">技术中的一个部分,</span><span style="" lang="EN-US">Sun</span><span style="">公司定义了</span><span style="" lang="EN-US">JMS</span><span style="">的标准接口,即</span><span style="" lang="EN-US">javax.jms.*</span><span style="">。</span><span style="" lang="EN-US"><br><br>JMS</span><span style="">服务使得分布式系统的信息通信松散连接的,发送信息的客户端只需要负责发送信息,接收信息的客户端接收信息,两个客户端之间没有必要是同时可用的,甚至发送客户端都没有必要知道客户端的信息,只要负责发送到接收的服务端就可以,</span><span style="" lang="EN-US">JMS</span><span style="">可以说是低耦合的。</span><span style="" lang="EN-US"><br><br></span><span style="">同时</span><span style="" lang="EN-US">JMS API </span><span style="">做到了以下</span><span style="" lang="EN-US">2</span><span style="">点</span><span style="" lang="EN-US"><br></span><span style="">异步的,服务端可以发送信息到一个客户端,客户端不需要为了收到信息而请求信息。</span><span style="" lang="EN-US"><br><br></span><span style="">可靠的,</span><span style="" lang="EN-US">JMS API</span><span style="">保证了服务端所有发送的信息最少发送一次和只发送一次</span><span style="" lang="EN-US"><br><br></span><span style="">下面介绍一些</span><span style="" lang="EN-US">JMS API </span><span style="">的基本知识</span><span style="" lang="EN-US"><br><br>JMS API </span><span style="">基本组成</span><span style="" lang="EN-US"><br><br> JMS provider</span><span style="">服务者,是一个消息系统通过实现</span><span style="" lang="EN-US">JMS API </span><span style="">接口,用于管理和控制信息。</span><span style="font-size: 9pt; color: black; font-family: Symbol;" lang="EN-US"><span style="">l</span></span><span style="" lang="EN-US"><br></span><span style="font-size: 9pt; color: black; font-family: Symbol;" lang="EN-US"><span style="">l</span></span><span style="" lang="EN-US"> JMS client </span><span style="">客户端</span><span style="" lang="EN-US"></span><span style="">用于发送和接收信息,通常是用在</span><span style="" lang="EN-US">java</span><span style="">程序中用</span><span style="" lang="EN-US">java</span><span style="">编写的</span><span style="" lang="EN-US"><br> Messages</span><span style="font-size: 9pt; color: black; font-family: Symbol;" lang="EN-US"><span style="">l</span></span><span style="" lang="EN-US"> </span><span style="">用于在客户端信息通信的对象</span><span style="" lang="EN-US"><br> Administered Objects </span><span style="">由</span><span style="" lang="EN-US">JMS</span><span style="font-size: 9pt; color: black; font-family: Symbol;" lang="EN-US"><span style="">l</span></span><span style="" lang="EN-US"> provider</span><span style="">为了</span><span style="" lang="EN-US">client</span><span style="">创建的对象,通常是</span><span style="" lang="EN-US">connectionFactory</span><span style="">和</span><span style="" lang="EN-US">destination<br><br>Messages </span><span style="">通信方式</span><span style="" lang="EN-US"><br>JMS</span><span style="">通信方式分为点对点通信,和发布</span><span style="" lang="EN-US">/</span><span style="">订阅方式</span><span style="" lang="EN-US"><br><br></span><span style="">点对点方式(</span><span style="" lang="EN-US">point-to-point</span><span style="">)</span><span style="" lang="EN-US"><br></span><span style="">点对点的消息发送方式主要建立在</span><span style="" lang="EN-US"> Message Queue,Sender,reciever</span><span style="">上,</span><span style="" lang="EN-US">Message Queue </span><span style="">存贮消息,</span><span style="" lang="EN-US">Sneder </span><span style="">发送消息,</span><span style="" lang="EN-US">receive</span><span style="">接收消息,</span><span style="" lang="EN-US"><br><br></span><span style="">具体的信息就是</span><span style="" lang="EN-US"> Sender Client</span><span style="">发送</span><span style="" lang="EN-US">Message </span><span style="">到</span><span style="" lang="EN-US">Queue ,</span><span style="">而</span><span style="" lang="EN-US"> receiver Cliernt </span><span style="">从</span><span style="" lang="EN-US">Queue</span><span style="">中接收消息和发送消息已接受到</span><span style="" lang="EN-US">Quere,</span><span style="">确认消息接收。</span><span style="" lang="EN-US"><br><br></span><span style="">在使用点对点方式时需要注意,</span><span style="" lang="EN-US"><br><br></span><span style="">一条消息只有一个接收端,消息发送客户端与接收客户端没有时间上的依赖,发送客户端可以在任何时刻发送信息到</span><span style="" lang="EN-US">Queue</span><span style="">,而不需要知道接收客户端是不是在运行</span><span style="" lang="EN-US"><br><br></span><span style="">发布</span><span style="" lang="EN-US">/</span><span style="">订阅</span><span style=""> </span><span style="">方式(</span><span style="" lang="EN-US">publish/subscriber Messaging</span><span style="">)</span><span style="" lang="EN-US"><br></span><span style="">发布</span><span style="" lang="EN-US">/</span><span style="">订阅方式用于多接收客户端的方式</span><span style="" lang="EN-US"><br></span><span style="">作为发布订阅的方式,可能存在多个接收客户端,并且接收端客户端与发送客户端存在时间上的依赖。一个接收端只能接收他创建以后发送客户端发送的信息。</span><span style="" lang="EN-US"><br><br></span><span style="">作为</span><span style="" lang="EN-US">subscriber ,</span><span style="">在接收消息时有两种方法,</span><span style="" lang="EN-US">destination</span><span style="">的</span><span style="" lang="EN-US">receive</span><span style="">方法,和实现</span><span style="" lang="EN-US">message listener </span><span style="">接口的</span><span style="" lang="EN-US">onMessage </span><span style="">方法。</span><span style="" lang="EN-US"><br><br></span><span style="">基础知识这里说的很少,具体的可以参照</span><span style="" lang="EN-US">java.sun.com.</span><span style="">的</span><span style="" lang="EN-US">JMS API </span><span style="">指南</span></p>
<p class="MsoNormal" style="margin: 0cm 0cm 0pt;"><span style="font-size: small;"><span lang="EN-US"><span style="font-family: Calibri;"><span style=""> </span>ActiveMQ</span></span><span style="">的特性:</span></span></p>
<p class="MsoNormal" style="margin: 0cm 0cm 0pt;"><span lang="EN-US"><span style="font-size: small; font-family: Calibri;"></span></span></p>
<p class="MsoNormal" style="margin: 0cm 0cm 0pt;"><span style="">完全支持</span><span style="" lang="EN-US">JMS1.1</span><span style="">和</span><span style="" lang="EN-US">J2EE 1.4</span><span style="">规范的</span><span style="" lang="EN-US"> JMS Provider</span><span style="">实现,也是</span><span style="" lang="EN-US">Apache Geronimo</span><span style="">默认的</span><span style="" lang="EN-US">JMS provider</span><span style="">。</span><span style="" lang="EN-US"> </span></p>
<p class="MsoNormal" style="margin: 0cm 0cm 0pt;"><span style="" lang="EN-US">POJO withdout EJB Container</span><span style="">,不需要实现</span><span style="" lang="EN-US">EJB</span><span style="">繁琐复杂的</span><span style="" lang="EN-US">Message Bean</span><span style="">接口和配置。</span><span style="" lang="EN-US"> </span></p>
<p class="MsoNormal" style="margin: 0cm 0cm 0pt;"><span style="" lang="EN-US">Spring Base</span><span style="">,可以使用</span><span style="" lang="EN-US">Spring</span><span style="">的各种特性如</span><span style="" lang="EN-US">IOC</span><span style="">、</span><span style="" lang="EN-US">AOP </span><span style="">。</span><span style="" lang="EN-US"> </span></p>
<p class="MsoNormal" style="margin: 0cm 0cm 0pt;"><span style="" lang="EN-US">Effective</span><span style="">,基于</span><span style="" lang="EN-US">Jencks</span><span style="">的</span><span style="" lang="EN-US">JCA Container</span><span style="">实现</span><span style="" lang="EN-US"> pool connection</span><span style="">,</span><span style="" lang="EN-US">control transactions and manage security</span><span style="">。</span><span style="" lang="EN-US"><span style=""> </span></span></p>
<p class="MsoNormal" style="margin: 0cm 0cm 0pt;"><span style="">下面是它的特性列表</span><span style="" lang="EN-US"> (</span><span style="">详细的特性清单请参考</span><span style="" lang="EN-US"><a href="http://activemq.apache.org/features.html"><span style="color: black; text-decoration: none;">http://activemq.apache.org/features.html</span></a>)<br> 1.</span><span style="">多种语言和协议编写客户端</span><span style="" lang="EN-US"><br></span><span style="">语言</span><span style="" lang="EN-US">: Java, C, C++, C#, Ruby, Perl, Python, PHP <br></span><span style="">应用协议</span><span style="" lang="EN-US">: OpenWire,Stomp REST,WS Notification,XMPP,AMQP<br> 2.</span><span style="">完全支持</span><span style="" lang="EN-US">JMS1.1</span><span style="">和</span><span style="" lang="EN-US">J2EE 1.4</span><span style="">规范</span><span style="" lang="EN-US"> (</span><span style="">持久化</span><span style="" lang="EN-US">,XA</span><span style="">消息</span><span style="" lang="EN-US">,</span><span style="">事务</span><span style="" lang="EN-US">)<br> 3.</span><span style="">对</span><span style="" lang="EN-US">Spring</span><span style="">的支持</span><span style="" lang="EN-US">,ActiveMQ</span><span style="">可以很容得内嵌到使用</span><span style="" lang="EN-US">Spring</span><span style="">的系统里面去</span><span style="" lang="EN-US">,</span><span style="">而且也支持</span><span style="" lang="EN-US">Spring2.0</span><span style="">的特性</span><span style="" lang="EN-US"><br>4.</span><span style="">通过了常见</span><span style="" lang="EN-US">J2EE</span><span style="">服务器</span><span style="" lang="EN-US">(</span><span style="">如</span><span style="" lang="EN-US"> Geronimo,JBoss 4, GlassFish,WebLogic)</span><span style="">的测试</span><span style="" lang="EN-US">,</span><span style="">其中通过</span><span style="" lang="EN-US">JCA 1.5 resource adaptors</span><span style="">的配置</span><span style="" lang="EN-US">,<br></span><span style="">可以让</span><span style="" lang="EN-US">ActiveMQ</span><span style="">可以自动的部署到任何兼容</span><span style="" lang="EN-US">J2EE 1.4 </span><span style="">商业服务器上</span><span style="" lang="EN-US"><br>5.</span><span style="">支持多种传送协议</span><span style="" lang="EN-US">:in-VM,TCP,SSL,NIO,UDP,JGroups,JXTA<br>6.</span><span style="">支持通过</span><span style="" lang="EN-US">JDBC</span><span style="">和</span><span style="" lang="EN-US">journal</span><span style="">提供高速的消息持久化</span><span style="" lang="EN-US"><br>7.</span><span style="">从设计上保证了高性能的集群</span><span style="" lang="EN-US">,</span><span style="">客户端</span><span style="" lang="EN-US">-</span><span style="">服务器</span><span style="" lang="EN-US">,</span><span style="">点对点</span><span style="" lang="EN-US"><br>8.</span><span style="">支持</span><span style="" lang="EN-US">Ajax<br>9.</span><span style="">支持与</span><span style="" lang="EN-US">Axis</span><span style="">的整合</span><span style="" lang="EN-US"><br>10.</span><span style="">可以很容易得调用内嵌</span><span style="" lang="EN-US">JMS provider,</span><span style="">进行测试</span><span style="" lang="EN-US"></span></p>
<p class="MsoNormal" style="margin: 0cm 0cm 0pt;"><span lang="EN-US"><span style="font-size: small; font-family: Calibri;"></span></span></p>
<p class="MsoNormal" style="margin: 0cm 0cm 0pt;"><span style="">作为一个</span><span style="" lang="EN-US">JMS client </span><span style="">都需要以下的组成</span><span style="" lang="EN-US"><br>Administered Objects</span><span style="">:</span><span style="" lang="EN-US">ConnectionFactory</span><span style="">,</span><span style="" lang="EN-US">Destination<br>Connection</span><span style="">;</span><span style="" lang="EN-US"><br>Session<br>Message Producers/Message Cosumers<br>Messages</span></p>
<p class="MsoNormal" style="margin: 0cm 0cm 0pt;"><span lang="EN-US"><span style="font-size: small; font-family: Calibri;"></span></span></p>
<p class="MsoNormal" style="margin: 0cm 0cm 0pt;"><span style="font-size: small;"><span lang="EN-US"><span style="font-family: Calibri;">Hello</span></span><span style="">实例</span></span></p>
<p class="MsoNormal" style="margin: 0cm 0cm 0pt;"><span style="font-size: small;"><span lang="EN-US"><span style="font-family: Calibri;">=================================Hello.java===========================<br></span></span><span style=""> </span></span><span lang="EN-US"><br></span><span style=""><span style="font-size: small;"> </span></span><span style="font-size: small;"><span lang="EN-US"><span style="font-family: Calibri;">import java.io.Serializable;<br></span></span><span style=""> </span></span><span lang="EN-US"><br></span><span style="font-size: small;"><span style=""> </span><span lang="EN-US"><span style="font-family: Calibri;">/**Hello.java</span></span><span style="">用来传递</span><span lang="EN-US"><span style="font-family: Calibri;">JAVA</span></span><span style="">对象</span></span><span lang="EN-US"><br></span><span style=""><span style="font-size: small;"> </span></span><span style="font-size: small;"><span lang="EN-US"><span style="font-family: Calibri;">* Author: cjp<br></span></span><span style=""> </span></span><span style="font-size: small;"><span lang="EN-US"><span style="font-family: Calibri;">* Date: 2005-11-8<br></span></span><span style=""> </span></span><span style="font-size: small;"><span lang="EN-US"><span style="font-family: Calibri;">* Time: 22:24:02<br></span></span><span style=""> </span></span><span style="font-size: small;"><span lang="EN-US"><span style="font-family: Calibri;">*/<br></span></span><span style=""> </span></span><span style="font-size: small;"><span lang="EN-US"><span style="font-family: Calibri;">public class Hello implements Serializable {<br></span></span><span style=""> </span></span><span style="font-size: small;"><span lang="EN-US"><span style="font-family: Calibri;">private String id;<br></span></span><span style=""> </span></span><span style="font-size: small;"><span lang="EN-US"><span style="font-family: Calibri;">private Hello hello;<br></span></span><span style=""> </span></span><span style="font-size: small;"><span lang="EN-US"><span style="font-family: Calibri;">private PointList pointList;<br></span></span><span style=""> </span></span><span style="font-size: small;"><span lang="EN-US"><span style="font-family: Calibri;">public String getId() {<br></span></span><span style=""> </span></span><span style="font-size: small;"><span lang="EN-US"><span style="font-family: Calibri;">return id;<br></span></span><span style=""> </span></span><span style="font-size: small;"><span lang="EN-US"><span style="font-family: Calibri;">}<br></span></span><span style=""> </span></span><span lang="EN-US"><br></span><span style=""><span style="font-size: small;"> </span></span><span style="font-size: small;"><span lang="EN-US"><span style="font-family: Calibri;">public void setId(String id) {<br></span></span><span style=""> </span></span><span style="font-size: small;"><span lang="EN-US"><span style="font-family: Calibri;">this.id = id;<br></span></span><span style=""> </span></span><span style="font-size: small;"><span lang="EN-US"><span style="font-family: Calibri;">}<br></span></span><span style=""> </span></span><span lang="EN-US"><br></span><span style=""><span style="font-size: small;"> </span></span><span style="font-size: small;"><span lang="EN-US"><span style="font-family: Calibri;">public Hello getHello() {<br></span></span><span style=""> </span></span><span style="font-size: small;"><span lang="EN-US"><span style="font-family: Calibri;">return hello;<br></span></span><span style=""> </span></span><span style="font-size: small;"><span lang="EN-US"><span style="font-family: Calibri;">}<br></span></span><span style=""> </span></span><span lang="EN-US"><br></span><span style=""><span style="font-size: small;"> </span></span><span style="font-size: small;"><span lang="EN-US"><span style="font-family: Calibri;">public void setHello(Hello hello) {<br></span></span><span style=""> </span></span><span style="font-size: small;"><span lang="EN-US"><span style="font-family: Calibri;">this.hello = hello;<br></span></span><span style=""> </span></span><span style="font-size: small;"><span lang="EN-US"><span style="font-family: Calibri;">}<br></span></span><span style=""> </span></span><span style="font-size: small;"><span lang="EN-US"><span style="font-family: Calibri;">}<br></span></span><span style=""> </span></span><span lang="EN-US"><br></span><span style=""><span style="font-size: small;"> </span></span><span style="font-size: small;"><span lang="EN-US"><span style="font-family: Calibri;">=========================SpringTest .java========================<br></span></span><span style=""> </span></span><span lang="EN-US"><br></span><span style=""><span style="font-size: small;"> </span></span><span style="font-size: small;"><span lang="EN-US"><span style="font-family: Calibri;">import org.springframework.jms.core.JmsTemplate;<br></span></span><span style=""> </span></span><span style="font-size: small;"><span lang="EN-US"><span style="font-family: Calibri;">import org.springframework.jms.core.MessageCreator;<br></span></span><span style=""> </span></span><span style="font-size: small;"><span lang="EN-US"><span style="font-family: Calibri;">import org.springframework.test.AbstractDependencyInjectionSpringContextTests;<br></span></span><span style=""> </span></span><span lang="EN-US"><br></span><span style=""><span style="font-size: small;"> </span></span><span style="font-size: small;"><span lang="EN-US"><span style="font-family: Calibri;">import javax.jms.*;<br></span></span><span style=""> </span></span><span lang="EN-US"><br></span><span style=""><span style="font-size: small;"> </span></span><span style="font-size: small;"><span lang="EN-US"><span style="font-family: Calibri;">/**<br></span></span><span style=""> </span></span><span lang="EN-US"><br></span><span style="font-size: small;"><span style=""> </span><span lang="EN-US"><span style="font-family: Calibri;">*</span></span><span style="">发送</span><span lang="EN-US"><span style="font-family: Calibri;">JMS</span></span><span style="">消息</span></span><span lang="EN-US"><br></span><span style=""><span style="font-size: small;"> </span></span><span lang="EN-US"><br></span><span style=""><span style="font-size: small;"> </span></span><span style="font-size: small;"><span lang="EN-US"><span style="font-family: Calibri;">*/<br></span></span><span style=""> </span></span><span style="font-size: small;"><span lang="EN-US"><span style="font-family: Calibri;">public class SpringTest extends AbstractDependencyInjectionSpringContextTests<br></span></span><span style=""> </span></span><span style="font-size: small;"><span lang="EN-US"><span style="font-family: Calibri;">{<br></span></span><span style=""> </span></span><span style="font-size: small;"><span lang="EN-US"><span style="font-family: Calibri;">protected String[] getConfigLocations()<br></span></span><span style=""> </span></span><span style="font-size: small;"><span lang="EN-US"><span style="font-family: Calibri;">{<br></span></span><span style=""> </span></span><span style="font-size: small;"><span lang="EN-US"><span style="font-family: Calibri;">return new String[]{"file:D://wosame//test//com//wosame//room//jms//jms.xml"};<br></span></span><span style=""> </span></span><span style="font-size: small;"><span lang="EN-US"><span style="font-family: Calibri;">}<br></span></span><span style=""> </span></span><span lang="EN-US"><br></span><span style=""><span style="font-size: small;"> </span></span><span style="font-size: small;"><span lang="EN-US"><span style="font-family: Calibri;">public void testSendMessage() throws Exception<br></span></span><span style=""> </span></span><span style="font-size: small;"><span lang="EN-US"><span style="font-family: Calibri;">{<br></span></span><span style=""> </span></span><span style="font-size: small;"><span lang="EN-US"><span style="font-family: Calibri;">JmsTemplate jmsTemplate = (JmsTemplate) applicationContext.getBean("jmsTemplate");<br></span></span><span style=""> </span></span><span style="font-size: small;"><span lang="EN-US"><span style="font-family: Calibri;">jmsTemplate.send(new MessageCreator()<br></span></span><span style=""> </span></span><span style="font-size: small;"><span lang="EN-US"><span style="font-family: Calibri;">{<br></span></span><span style=""> </span></span><span style="font-size: small;"><span lang="EN-US"><span style="font-family: Calibri;">public Message createMessage(Session session) throws JMSException<br></span></span><span style=""> </span></span><span style="font-size: small;"><span lang="EN-US"><span style="font-family: Calibri;">{<br></span></span><span style=""> </span></span><span style="font-size: small;"><span lang="EN-US"><span style="font-family: Calibri;">ObjectMessage message=session.createObjectMessage();<br></span></span><span style=""> </span></span><span style="font-size: small;"><span lang="EN-US"><span style="font-family: Calibri;">Hello hello=new Hello();<br></span></span><span style=""> </span></span><span style="font-size: small;"><span lang="EN-US"><span style="font-family: Calibri;">hello.setId("test");<br></span></span><span style=""> </span></span><span style="font-size: small;"><span lang="EN-US"><span style="font-family: Calibri;">message.setObject(hello);<br></span></span><span style=""> </span></span><span style="font-size: small;"><span lang="EN-US"><span style="font-family: Calibri;">return message;<br></span></span><span style=""> </span></span><span style="font-size: small;"><span lang="EN-US"><span style="font-family: Calibri;">}<br></span></span><span style=""> </span></span><span style="font-size: small;"><span lang="EN-US"><span style="font-family: Calibri;">});<br></span></span><span style=""> </span></span><span style="font-size: small;"><span lang="EN-US"><span style="font-family: Calibri;">}<br></span></span><span style=""> </span></span><span style="font-size: small;"><span lang="EN-US"><span style="font-family: Calibri;">}<br></span></span><span style=""> </span></span><span lang="EN-US"><br></span><span style=""><span style="font-size: small;"> </span></span><span style="font-size: small;"><span lang="EN-US"><span style="font-family: Calibri;">================================HelloMDP .java==================================<br></span></span><span style=""> </span></span><span lang="EN-US"><br></span><span style=""><span style="font-size: small;"> </span></span><span style="font-size: small;"><span lang="EN-US"><span style="font-family: Calibri;">/**<br></span></span><span style=""> </span></span><span lang="EN-US"><br></span><span style="font-size: small;"><span style=""> 处理</span><span lang="EN-US"><span style="font-family: Calibri;">JMS</span></span><span style="">消息</span></span><span lang="EN-US"><br></span><span style=""><span style="font-size: small;"> </span></span><span lang="EN-US"><br></span><span style=""><span style="font-size: small;"> </span></span><span style="font-size: small;"><span lang="EN-US"><span style="font-family: Calibri;">*/<br></span></span><span style=""> </span></span><span lang="EN-US"><br></span><span style=""><span style="font-size: small;"> </span></span><span style="font-size: small;"><span lang="EN-US"><span style="font-family: Calibri;">import org.apache.commons.logging.Log;<br></span></span><span style=""> </span></span><span style="font-size: small;"><span lang="EN-US"><span style="font-family: Calibri;">import org.apache.commons.logging.LogFactory;<br></span></span><span style=""> </span></span><span style="font-size: small;"><span lang="EN-US"><span style="font-family: Calibri;">import javax.jms.*;<br></span></span><span style=""> </span></span><span lang="EN-US"><br></span><span style=""><span style="font-size: small;"> </span></span><span style="font-size: small;"><span lang="EN-US"><span style="font-family: Calibri;">public class HelloMDP implements MessageListener<br></span></span><span style=""> </span></span><span style="font-size: small;"><span lang="EN-US"><span style="font-family: Calibri;">{<br></span></span><span style=""> </span></span><span style="font-size: small;"><span lang="EN-US"><span style="font-family: Calibri;">protected Log log = LogFactory.getLog(HelloMDP.class);<br></span></span><span style=""> </span></span><span lang="EN-US"><br></span><span style=""><span style="font-size: small;"> </span></span><span style="font-size: small;"><span lang="EN-US"><span style="font-family: Calibri;">public void onMessage(Message message)<br></span></span><span style=""> </span></span><span style="font-size: small;"><span lang="EN-US"><span style="font-family: Calibri;">{<br></span></span><span style=""> </span></span><span style="font-size: small;"><span lang="EN-US"><span style="font-family: Calibri;">try<br></span></span><span style=""> </span></span><span style="font-size: small;"><span lang="EN-US"><span style="font-family: Calibri;">{<br></span></span><span style=""> </span></span><span style="font-size: small;"><span lang="EN-US"><span style="font-family: Calibri;">ObjectMessage objMessage = (ObjectMessage) message;<br></span></span><span style=""> </span></span><span style="font-size: small;"><span lang="EN-US"><span style="font-family: Calibri;">Hello hello= (Hello) objMessage.getObject();<br></span></span><span style=""> </span></span><span style="font-size: small;"><span lang="EN-US"><span style="font-family: Calibri;">System.out.println("hello.getId() = " + hello.getId());<br></span></span><span style=""> </span></span><span style="font-size: small;"><span lang="EN-US"><span style="font-family: Calibri;">} catch (JMSException e)<br></span></span><span style=""> </span></span><span style="font-size: small;"><span lang="EN-US"><span style="font-family: Calibri;">{<br></span></span><span style=""> </span></span><span style="font-size: small;"><span lang="EN-US"><span style="font-family: Calibri;">log.error("Parse failed", e);<br></span></span><span style=""> </span></span><span style="font-size: small;"><span lang="EN-US"><span style="font-family: Calibri;">}<br></span></span><span style=""> </span></span><span style="font-size: small;"><span lang="EN-US"><span style="font-family: Calibri;">}<br></span></span><span style=""> </span></span><span style="font-size: small;"><span lang="EN-US"><span style="font-family: Calibri;">}<br></span></span><span style=""> </span></span><span lang="EN-US"><br></span><span style=""><span style="font-size: small;"> </span></span><span style="font-size: small;"><span lang="EN-US"><span style="font-family: Calibri;">================================jms.xml==================================<br></span></span><span style=""> </span></span><span lang="EN-US"><br></span><span style=""><span style="font-size: small;"> </span></span><span style="font-size: small;"><span lang="EN-US"><span style="font-family: Calibri;"><?xml version="1.0" encoding="UTF-8"?><br></span></span><span style=""> </span></span><span style="font-size: small;"><span lang="EN-US"><span style="font-family: Calibri;"><!DOCTYPE beans PUBLIC "-//SPRING//DTD BEAN//EN"<br></span></span><span style=""> </span></span><span style="font-size: small;"><span lang="EN-US"><span style="font-family: Calibri;">"http://www.springframework.org/dtd/spring-beans.dtd"><br></span></span><span style=""> </span></span><span lang="EN-US"><br></span><span style=""><span style="font-size: small;"> </span></span><span style="font-size: small;"><span lang="EN-US"><span style="font-family: Calibri;"><beans><br></span></span><span style=""> </span></span><span lang="EN-US"><br></span><span style=""><span style="font-size: small;"> </span></span><span lang="EN-US"><span style="font-size: small; font-family: Calibri;"><!--</span><a href="http://embed.chinaitlab.com/" target="_blank"><span style="" lang="EN-US"><span lang="EN-US">嵌入式</span></span></a></span><span style="font-size: small;"><span style="">的</span><span lang="EN-US"><span style="font-family: Calibri;">JMS</span></span><span style="">连接,也就是跟随</span><span lang="EN-US"><span style="font-family: Calibri;">JVM</span></span><span style="">一起启动</span><span lang="EN-US"><span style="font-family: Calibri;">,</span></span><span style="">可以参看</span><span lang="EN-US"><span style="font-family: Calibri;">activemq</span></span><span style="">的文档</span></span><span style="font-size: small;"><span lang="EN-US"><span style="font-family: Calibri;">--><br></span></span><span style=""> </span></span><span style="font-size: small;"><span lang="EN-US"><span style="font-family: Calibri;"><bean id="connectionFactory" class="org.activemq.ActiveMQConnectionFactory"> <property name="brokerURL" value="vm://localhost"/><br></span></span><span style=""> </span></span><span style="font-size: small;"><span lang="EN-US"><span style="font-family: Calibri;"><property name="useEmbeddedBroker" value="true"/><br></span></span><span style=""> </span></span><span style="font-size: small;"><span lang="EN-US"><span style="font-family: Calibri;"></bean><br></span></span><span style=""> </span></span><span lang="EN-US"><br></span><span style="font-size: small;"><span style=""> </span><span lang="EN-US"><span style="font-family: Calibri;"><!--</span></span><span style="">消息监听器,也就是消息的具体的处理器</span></span><span style="font-size: small;"><span lang="EN-US"><span style="font-family: Calibri;">--><br></span></span><span style=""> </span></span><span style="font-size: small;"><span lang="EN-US"><span style="font-family: Calibri;"><bean id="HelloMDP" class="HelloMDP"/><br></span></span><span style=""> </span></span><span lang="EN-US"><br></span><span style="font-size: small;"><span style=""> </span><span lang="EN-US"><span style="font-family: Calibri;"><!--jms</span></span><span style="">监听需要</span><span lang="EN-US"><span style="font-family: Calibri;">JTA</span></span><span style="">容器的支持</span></span><span style="font-size: small;"><span lang="EN-US"><span style="font-family: Calibri;">--><br></span></span><span style=""> </span></span><span style="font-size: small;"><span lang="EN-US"><span style="font-family: Calibri;"><bean id="activeMQContainer" class="org.activemq.jca.JCAContainer"><br></span></span><span style=""> </span></span><span style="font-size: small;"><span lang="EN-US"><span style="font-family: Calibri;"><property name="workManager"><br></span></span><span style=""> </span></span><span style="font-size: small;"><span lang="EN-US"><span style="font-family: Calibri;"><bean id="workManager" class="org.activemq.work.SpringWorkManager"/><br></span></span><span style=""> </span></span><span style="font-size: small;"><span lang="EN-US"><span style="font-family: Calibri;"></property><br></span></span><span style=""> </span></span><span style="font-size: small;"><span lang="EN-US"><span style="font-family: Calibri;"><property name="resourceAdapter"><br></span></span><span style=""> </span></span><span style="font-size: small;"><span lang="EN-US"><span style="font-family: Calibri;"><bean id="activeMQResourceAdapter" class="org.activemq.ra.ActiveMQResourceAdapter"><br></span></span><span style=""> </span></span><span style="font-size: small;"><span lang="EN-US"><span style="font-family: Calibri;"><property name="serverUrl" value="vm://localhost"/><br></span></span><span style=""> </span></span><span style="font-size: small;"><span lang="EN-US"><span style="font-family: Calibri;"></bean><br></span></span><span style=""> </span></span><span style="font-size: small;"><span lang="EN-US"><span style="font-family: Calibri;"></property><br></span></span><span style=""> </span></span><span style="font-size: small;"><span lang="EN-US"><span style="font-family: Calibri;"></bean><br></span></span><span style=""> </span></span><span lang="EN-US"><br></span><span style="font-size: small;"><span style=""> </span><span lang="EN-US"><span style="font-family: Calibri;"><!--</span></span><span style="">消息的消费者,也就是将监听器与具体的队列关联</span></span><span style="font-size: small;"><span lang="EN-US"><span style="font-family: Calibri;">--><br></span></span><span style=""> </span></span><span style="font-size: small;"><span lang="EN-US"><span style="font-family: Calibri;"><bean id="HelloQueueConsumer" factory-method="addConnector" factory-bean="activeMQContainer"><br></span></span><span style=""> </span></span><span style="font-size: small;"><span lang="EN-US"><span style="font-family: Calibri;"><property name="activationSpec"><br></span></span><span style=""> </span></span><span style="font-size: small;"><span lang="EN-US"><span style="font-family: Calibri;"><bean class="org.activemq.ra.ActiveMQActivationSpec"><br></span></span><span style=""> </span></span><span style="font-size: small;"><span lang="EN-US"><span style="font-family: Calibri;"><property name="destination" value="Hello.Queue"/><br></span></span><span style=""> </span></span><span style="font-size: small;"><span lang="EN-US"><span style="font-family: Calibri;"><property name="destinationType" value="javax.jms.Queue"/><br></span></span><span style=""> </span></span><span style="font-size: small;"><span lang="EN-US"><span style="font-family: Calibri;"></bean><br></span></span><span style=""> </span></span><span style="font-size: small;"><span lang="EN-US"><span style="font-family: Calibri;"></property><br></span></span><span style=""> </span></span><span style="font-size: small;"><span lang="EN-US"><span style="font-family: Calibri;"><property name="ref" value="HelloMDP"/><br></span></span><span style=""> </span></span><span style="font-size: small;"><span lang="EN-US"><span style="font-family: Calibri;"></bean><br></span></span><span style=""> </span></span><span lang="EN-US"><br></span><span style="font-size: small;"><span style=""> </span><span lang="EN-US"><span style="font-family: Calibri;"><!--spring</span></span><span style="">的</span><span lang="EN-US"><span style="font-family: Calibri;">JMS template,</span></span><span style="">用来发送</span><span lang="EN-US"><span style="font-family: Calibri;">JMS</span></span><span style="">消息到指定的队列</span></span><span style="font-size: small;"><span lang="EN-US"><span style="font-family: Calibri;">--><br></span></span><span style=""> </span></span><span style="font-size: small;"><span lang="EN-US"><span style="font-family: Calibri;"><bean id="jmsTemplate" class="org.springframework.jms.core.JmsTemplate"><br></span></span><span style=""> </span></span><span style="font-size: small;"><span lang="EN-US"><span style="font-family: Calibri;"><property name="defaultDestinationName" value="Hello.Queue"/><br></span></span><span style=""> </span></span><span style="font-size: small;"><span lang="EN-US"><span style="font-family: Calibri;"><property name="connectionFactory" ref="connectionFactory"/><br></span></span><span style=""> </span></span><span style="font-size: small;"><span lang="EN-US"><span style="font-family: Calibri;"></bean><br></span></span><span style=""> </span><span lang="EN-US"><span style="font-family: Calibri;"></beans></span></span></span></p>
<p class="MsoNormal" style="margin: 0cm 0cm 0pt;"><span lang="EN-US"><span style="font-size: small; font-family: Calibri;"></span></span></p>
<p class="MsoNormal" style="margin: 0cm 0cm 0pt;"><span style="font-size: small;"><span style="">例子二</span><span lang="EN-US"><span style="font-family: Calibri;"> MQ</span></span><span style="">源码</span></span></p>
<p class="MsoNormal" style="margin: 0cm 0cm 0pt;"><span style="" lang="EN-US">import java.util.Arrays;<br>import java.util.Date;<br><br>import javax.jms.Connection;<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><br>import org.apache.activemq.ActiveMQConnection;<br>import org.apache.activemq.ActiveMQConnectionFactory;<br>import org.apache.activemq.util.IndentPrinter;<br><br>/**<br>* A simple tool for publishing messages<br>* <br>* @version $Revision: 1.2 $<br>*/<br>public class ProducerTool {<br><br> private Destination destination;<br> private int messageCount = 10;<br> private long sleepTime = 0L;<br> private boolean verbose = true;<br> private int messageSize = 255;<br> private long timeToLive;<br> private String user = ActiveMQConnection.DEFAULT_USER;<br> private String password = ActiveMQConnection.DEFAULT_PASSWORD;<br> private String url = ActiveMQConnection.DEFAULT_BROKER_URL;<br> private String subject = "TOOL.DEFAULT";<br> private boolean topic = false;<br> private boolean transacted = false;<br> private boolean persistent = false;<br><br> public static void main(String[] args) {<br> ProducerTool producerTool = new ProducerTool();<br> String[] unknonwn = CommnadLineSupport.setOptions(producerTool, args);<br> if( unknonwn.length > 0 ) {<br> System.out.println("Unknown options: "+Arrays.toString(unknonwn));<br> System.exit(-1);<br> } <br> producerTool.run();<br> }<br><br> public void run() {<br> Connection connection=null;<br> try {<br> System.out.println("Connecting to URL: " + url);<br> System.out.println("Publishing a Message with size " + messageSize+ " to " + (topic ? "topic" : "queue"<span style=""> </span>+ ": " + subject);<br> System.out.println("Using " + (persistent ? "persistent" : "non-persistent"<span style=""> </span>+ " messages"<span style=""> </span>;<br> System.out.println("Sleeping between publish " + sleepTime + " ms"<span style=""> </span>;<br> if (timeToLive != 0) {<br> System.out.println("Messages time to live " + timeToLive + " ms"<span style=""> </span>;<br> }<br><br> // Create the connection.<br> ActiveMQConnectionFactory connectionFactory = new ActiveMQConnectionFactory(user, password, url); <br> connection = connectionFactory.createConnection();<br> connection.start();<br><br> // Create the session<br> Session session = connection.createSession(transacted, Session.AUTO_ACKNOWLEDGE);<br> if (topic) {<br> destination = session.createTopic(subject);<br> } else {<br> destination = session.createQueue(subject);<br> }<br><br> // Create the producer.<br> MessageProducer producer = session.createProducer(destination);<br> if (persistent) {<br> producer.setDeliveryMode(DeliveryMode.PERSISTENT);<br> } else {<br> producer.setDeliveryMode(DeliveryMode.NON_PERSISTENT);<br> } <br> if (timeToLive != 0)<br> producer.setTimeToLive(timeToLive);<br><br> // Start sending messages<br> sendLoop(session, producer);<br><br> System.out.println("Done."<span style=""> </span>;<br><br> // Use the ActiveMQConnection interface to dump the connection stats.<br> ActiveMQConnection c = (ActiveMQConnection) connection;<br> c.getConnectionStats().dump(new IndentPrinter());<br><br> } catch (Exception e) {<br> System.out.println("Caught: " + e);<br> e.printStackTrace();<br> } finally {<br> try { <br> connection.close();<br> } catch (Throwable ignore) {<br> }<br> }<br> }<br><br> protected void sendLoop(Session session, MessageProducer producer)<br> throws Exception {<br><br> for (int i = 0; i < messageCount || messageCount == 0; i++) {<br><br> TextMessage message = session<br> .createTextMessage(createMessageText(i));<br><br> if (verbose) {<br> String msg = message.getText();<br> if (msg.length() > 50) {<br> msg = msg.substring(0, 50) + "...";<br> }<br> System.out.println("Sending message: " + msg);<br> }<br><br> producer.send(message);<br> if (transacted) {<br> session.commit();<br> }<br><br> Thread.sleep(sleepTime);<br><br> }<br><br> }<br><br> private String createMessageText(int index) {<br> StringBuffer buffer = new StringBuffer(messageSize);<br> buffer.append("Message: " + index + " sent at: " + new Date());<br> if (buffer.length() > messageSize) {<br> return buffer.substring(0, messageSize);<br> }<br> for (int i = buffer.length(); i < messageSize; i++) {<br> buffer.append(' ');<br> }<br> return buffer.toString();<br> }<br><br><br> public void setPersistent(boolean durable) {<br> this.persistent = durable;<br> }<br> public void setMessageCount(int messageCount) {<br> this.messageCount = messageCount;<br> }<br> public void setMessageSize(int messageSize) {<br> this.messageSize = messageSize;<br> }<br> public void setPassword(String pwd) {<br> this.password = pwd;<br> }<br> public void setSleepTime(long sleepTime) {<br> this.sleepTime = sleepTime;<br> }<br> public void setSubject(String subject) {<br> this.subject = subject;<br> }<br> public void setTimeToLive(long timeToLive) {<br> this.timeToLive = timeToLive;<br> }<br> public void setTopic(boolean topic) {<br> this.topic = topic;<br> }<br> public void setQueue(boolean queue) {<br> this.topic = !queue;<br> } <br> public void setTransacted(boolean transacted) {<br> this.transacted = transacted;<br> }<br> public void setUrl(String url) {<br> this.url = url;<br> }<br> public void setUser(String user) {<br> this.user = user;<br> }<br> public void setVerbose(boolean verbose) {<br> this.verbose = verbose;<br> }<br>}</span></p>
相关推荐
全面覆盖ActiveMQ相关知识点:这份面试专题资料涵盖了ActiveMQ的多个方面,包括基础知识、消息生产者和消费者、消息传递模式等等。它可以帮助应聘者全面掌握ActiveMQ的相关知识点,提高应聘的成功率。 精简易懂:每...
ActiveMQ是Apache软件基金会开发的一款开源消息中间件,它遵循开放消息传递协议(Open Message Broker Protocol,即AMQP)和Java消息服务...理解并熟练掌握这些知识点,对于构建基于ActiveMQ的点对点消息系统至关重要。
在Java环境中,可以通过添加ActiveMQ的依赖(如在pom.xml中配置)并编写相关代码,实现与ActiveMQ的通信。这通常涉及创建连接工厂、创建生产者和消费者对象,以及发送和接收消息等步骤。 总之,ActiveMQ作为一款...
### Apache ActiveMQ 相关知识点 #### 一、引言:Apache ActiveMQ 概述 - **Apache ActiveMQ**:一款高性能的消息中间件,用于在分布式系统之间进行远程通信,支持Java Message Service (JMS) 规范。尽管ActiveMQ...
使用这个工具,用户可以实现以下关键知识点: 1. **JMS接口**:了解Java Message Service(JMS)规范,它是Java平台上的标准API,用于在不同的应用之间发送和接收消息。ActiveMQ收发工具利用JMS接口与ActiveMQ...
### ActiveMQ-CPP 开发手册知识点详述 #### 一、引言 - **编写目的**:本手册旨在帮助开发者快速掌握 CMS (C++ Messaging Service) 的使用方法,提高 C++ 开发者在消息传递系统方面的开发效率,并作为 CMS 开发的...
### ActiveMQ 入门知识点详解 #### 一、ActiveMQ 概述 **ActiveMQ** 是由 Apache 软件基金会开发的一款免费且开源的消息中间件。与重量级且需付费的 IBM MQ 相比,ActiveMQ 更适合初学者及预算有限的项目使用。 *...
以下是关于如何在Linux上安装和配置ActiveMQ的详细步骤,以及相关的知识点: 1. **了解ActiveMQ**: - ActiveMQ是Apache软件基金会的顶级项目,用于处理消息传输,提供点对点和发布/订阅模式的消息传递。 - JMS...
在实际的ActiveMQ demo中,通常会包含以下关键知识点: 1. **安装与配置**:首先,你需要下载并安装ActiveMQ,然后启动其内置的Broker服务。配置文件(如`activemq.xml`)用于定义消息队列、主题和其他高级设置。 ...
作为一个专业的IT行业大师,我很高兴为你深入解析ActiveMQ的相关知识点。 1. **消息中间件的基本概念** 消息中间件是一种软件,它允许应用程序之间通过发送和接收消息来通信,而不是直接调用彼此。这种异步通信...
根据给定的文件信息,以下是从“ActiveMQ In Action精简版”中提炼出的关键IT知识点,主要聚焦于ActiveMQ的介绍、配置、使用及高级功能。 ### ActiveMQ概述 ActiveMQ是Apache软件基金会下的一个开源项目,它是一款...
在使用ActiveMQ时,了解以下几个关键知识点是至关重要的: 1. **Java Message Service (JMS)**:JMS是一种API,用于在不同的应用程序之间通过消息传递进行通信。它定义了生产、存储、消费和传输消息的标准接口,...
以下是关于ActiveMQ的一些核心知识点: 1. **ActiveMQ简介**:ActiveMQ是Apache软件基金会开发的一个开源项目,它支持多种协议,包括OpenWire、STOMP、AMQP、MQTT、XMPP和WS-Notification,适用于多种环境和语言。 ...
在ActiveMQ 5.12.0版本中,我们可以期待以下关键知识点: 1. **JMS支持**:ActiveMQ作为JMS提供商,实现了JMS 1.1规范,支持点对点(Queue)和发布/订阅(Topic)两种消息模型,允许应用程序发送和接收消息。 2. *...
以上就是关于 ActiveMQ-P2P 文本消息以及 Spring 和 ActiveMQ 整合实例的相关知识点。通过这些知识,开发者可以构建起可靠的点对点消息传递系统,提高应用的可扩展性和解耦性。在实际项目中,根据具体需求,还可以...
ActiveMQ的核心知识点包括: 1. **消息模型**:ActiveMQ支持点对点(Point-to-Point)和发布/订阅(Publish/Subscribe)两种消息传递模式,前者通过队列进行通信,后者通过主题进行广播。 2. **JMS接口**:...
### ActiveMQ 快速上手知识点详解 #### 一、ActiveMQ简介 - **定义**:ActiveMQ 是 Apache 软件基金会所研发的一款开源消息中间件,它完全支持 JMS 1.1 和 J2EE 1.4 规范,能够作为 JMS Provider 实现消息传递功能...
ActiveMQ是一款非常流行的开源消息队列中间件,它实现了JMS...了解和掌握这些知识点,有助于面试者在面试中展示对ActiveMQ的深入理解和实际应用能力,同时也是确保在日常开发工作中正确、高效使用消息队列的重要基础。
在本文中,我们将深入探讨ActiveMQ的配置及其入门知识。 一、ActiveMQ简介 ActiveMQ是Apache软件基金会的顶级项目,它的核心功能是作为消息代理,负责接收、存储和转发消息。它支持多种协议,如OpenWire、AMQP、...
在ActiveMQ 5.16.1中,你可以找到以下关键知识点: 1. **消息模型**:ActiveMQ支持点对点(Queue)和发布/订阅(Topic)两种消息模型,满足不同类型的通信需求。点对点模式确保每个消息只被一个消费者接收,而发布/...