ActiveMQ解析.docx

上传人:b****8 文档编号:9124631 上传时间:2023-02-03 格式:DOCX 页数:28 大小:29.82KB
下载 相关 举报
ActiveMQ解析.docx_第1页
第1页 / 共28页
ActiveMQ解析.docx_第2页
第2页 / 共28页
ActiveMQ解析.docx_第3页
第3页 / 共28页
ActiveMQ解析.docx_第4页
第4页 / 共28页
ActiveMQ解析.docx_第5页
第5页 / 共28页
点击查看更多>>
下载资源
资源描述

ActiveMQ解析.docx

《ActiveMQ解析.docx》由会员分享,可在线阅读,更多相关《ActiveMQ解析.docx(28页珍藏版)》请在冰豆网上搜索。

ActiveMQ解析.docx

ActiveMQ解析

ActiveMQ解析

JMS始终在JavaEE五花八门的协议里,WebService满天飞的时候占一位置,是因为:

 

它可以把不影响用户执行结果又比较耗时的任务(比如发邮件通知管理员)异步的扔给JMS服务端去做,而尽快的把屏幕返还给用户。

服务端能够多线程排队响应高并发的请求。

可以在Java世界里达到最高的解耦。

客户端与服务端无需直连,甚至无需知晓对方是谁、在哪里、有多少人,只要对流过的信息作响应就行了,在企业应用环境复杂时作用明显。

 

JMS(JavaMessageService)即java消息服务用于分布式系统得消息通信,JMS是J2EE技术中的一个部分,Sun公司定义了JMS的标准接口,即javax.jms.*。

JMS服务使得分布式系统的信息通信松散连接的,发送信息的客户端只需要负责发送信息,接收信息的客户端接收信息,两个客户端之间没有必要是同时可用的,甚至发送客户端都没有必要知道客户端的信息,只要负责发送到接收的服务端就可以,JMS可以说是低耦合的。

同时JMSAPI做到了以下2点

异步的,服务端可以发送信息到一个客户端,客户端不需要为了收到信息而请求信息。

可靠的,JMSAPI保证了服务端所有发送的信息最少发送一次和只发送一次

下面介绍一些JMSAPI的基本知识

JMSAPI基本组成

    JMSprovider服务者,是一个消息系统通过实现JMSAPI接口,用于管理和控制信息。

λ

   λ JMSclient  客户端  用于发送和接收信息,通常是用在java程序中用java编写的

    Messagesλ用于在客户端信息通信的对象

    AdministeredObjects由JMSλprovider为了client创建的对象,通常是connectionFactory和destination

Messages通信方式

JMS通信方式分为点对点通信,和发布/订阅方式

点对点方式(point-to-point)

点对点的消息发送方式主要建立在MessageQueue,Sender,reciever上,MessageQueue存贮消息,Sneder发送消息,receive接收消息,

具体的信息就是SenderClient发送Message到Queue,而receiverCliernt从Queue中接收消息和发送消息已接受到Quere,确认消息接收。

在使用点对点方式时需要注意,

一条消息只有一个接收端,消息发送客户端与接收客户端没有时间上的依赖,发送客户端可以在任何时刻发送信息到Queue,而不需要知道接收客户端是不是在运行

发布/订阅方式(publish/subscriberMessaging)

发布/订阅方式用于多接收客户端的方式

作为发布订阅的方式,可能存在多个接收客户端,并且接收端客户端与发送客户端存在时间上的依赖。

一个接收端只能接收他创建以后发送客户端发送的信息。

作为subscriber,在接收消息时有两种方法,destination的receive方法,和实现messagelistener接口的onMessage方法。

基础知识这里说的很少,具体的可以参照.的JMSAPI指南

   ActiveMQ的特性:

 

完全支持JMS1.1和J2EE1.4规范的JMSProvider实现,也是ApacheGeronimo默认的JMSprovider。

POJOwithdoutEJBContainer,不需要实现EJB繁琐复杂的MessageBean接口和配置。

SpringBase,可以使用Spring的各种特性如IOC、AOP。

Effective,基于Jencks的JCAContainer实现poolconnection,controltransactionsandmanagesecurity。

 

下面是它的特性列表(详细的特性清单请参考http:

//activemq.apache.org/features.html)

   1.多种语言和协议编写客户端

    语言:

Java,C,C++,C#,Ruby,Perl,Python,PHP

       应用协议:

OpenWire,StompREST,WSNotification,XMPP,AMQP

   2.完全支持JMS1.1和J2EE1.4规范 (持久化,XA消息,事务)

   3.对Spring的支持,ActiveMQ可以很容得内嵌到使用Spring的系统里面去,而且也支持Spring2.0的特性

   4.通过了常见J2EE服务器(如Geronimo,JBoss4,GlassFish,WebLogic)的测试,其中通过JCA1.5resourceadaptors的配置,

     可以让ActiveMQ可以自动的部署到任何兼容J2EE1.4商业服务器上

    5.支持多种传送协议:

in-VM,TCP,SSL,NIO,UDP,JGroups,JXTA

    6.支持通过JDBC和journal提供高速的消息持久化

    7.从设计上保证了高性能的集群,客户端-服务器,点对点

     8.支持Ajax

     9.支持与Axis的整合

     10.可以很容易得调用内嵌JMSprovider,进行测试

 

作为一个JMSclient都需要以下的组成

AdministeredObjects:

ConnectionFactory,Destination

Connection;

Session

MessageProducers/MessageCosumers

Messages

 

Hello实例

=================================Hello.java===========================

  

  importjava.io.Serializable;

  

  /**Hello.java用来传递JAVA对象

  *Author:

cjp

  *Date:

2005-11-8

  *Time:

22:

24:

02

  */

  publicclassHelloimplementsSerializable{

  privateStringid;

  privateHellohello;

  privatePointListpointList;

  publicStringgetId(){

  returnid;

  }

  

  publicvoidsetId(Stringid){

  this.id=id;

  }

  

  publicHellogetHello(){

  returnhello;

  }

  

  publicvoidsetHello(Hellohello){

  this.hello=hello;

  }

  }

  

  =========================SpringTest.java========================

  

  importorg.springframework.jms.core.JmsTemplate;

  importorg.springframework.jms.core.MessageCreator;

  importorg.springframework.test.AbstractDependencyInjectionSpringContextTests;

  

  importjavax.jms.*;

  

  /**

  

  *发送JMS消息

  

  */

  publicclassSpringTestextendsAbstractDependencyInjectionSpringContextTests

  {

  protectedString[]getConfigLocations()

  {

  returnnewString[]{"file:

D:

\\wosame\\test\\com\\wosame\\room\\jms\\jms.xml"};

  }

  

  publicvoidtestSendMessage()throwsException

  {

  JmsTemplatejmsTemplate=(JmsTemplate)applicationContext.getBean("jmsTemplate");

  jmsTemplate.send(newMessageCreator()

  {

  publicMessagecreateMessage(Sessionsession)throwsJMSException

  {

  ObjectMessagemessage=session.createObjectMessage();

  Hellohello=newHello();

  hello.setId("test");

  message.setObject(hello);

  returnmessage;

  }

  });

  }

  }

  

  ================================HelloMDP.java==================================

  

  /**

  

  处理JMS消息

  

  */

  

  importmons.logging.Log;

  importmons.logging.LogFactory;

  importjavax.jms.*;

  

  publicclassHelloMDPimplementsMessageListener

  {

  protectedLoglog=LogFactory.getLog(HelloMDP.class);

  

  publicvoidonMessage(Messagemessage)

  {

  try

  {

  ObjectMessageobjMessage=(ObjectMessage)message;

  Hellohello=(Hello)objMessage.getObject();

  System.out.println("hello.getId()="+hello.getId());

  }catch(JMSExceptione)

  {

  log.error("Parsefailed",e);

  }

  }

  }

  

  ================================jms.xml==================================

  

  

xmlversion="1.0"encoding="UTF-8"?

>

  

DOCTYPEbeansPUBLIC"-//SPRING//DTDBEAN//EN"

  "http:

//www.springframework.org/dtd/spring-beans.dtd">

  

  

  

  

--嵌入式的JMS连接,也就是跟随JVM一起启动,可以参看activemq的文档-->

  

//localhost"/>

  

  

  

  

--消息监听器,也就是消息的具体的处理器-->

  

  

  

--jms监听需要JTA容器的支持-->

  

  

  

  

  

  

  

//localhost"/>

  

  

  

  

  

--消息的消费者,也就是将监听器与具体的队列关联-->

  

  

  

  

  

  

  

  

  

  

  

--spring的JMStemplate,用来发送JMS消息到指定的队列-->

  

  

  

  

  

 

例子二MQ源码

importjava.util.Arrays;

importjava.util.Date;

importjavax.jms.Connection;

importjavax.jms.DeliveryMode;

importjavax.jms.Destination;

importjavax.jms.MessageProducer;

importjavax.jms.Session;

importjavax.jms.TextMessage;

importorg.apache.activemq.ActiveMQConnection;

importorg.apache.activemq.ActiveMQConnectionFactory;

importorg.apache.activemq.util.IndentPrinter;

/**

*Asimpletoolforpublishingmessages

*

*@version$Revision:

1.2$

*/

publicclassProducerTool{

    privateDestinationdestination;

    privateintmessageCount=10;

    privatelongsleepTime=0L;

    privatebooleanverbose=true;

    privateintmessageSize=255;

    privatelongtimeToLive;

    privateStringuser=ActiveMQConnection.DEFAULT_USER;

    privateStringpassword=ActiveMQConnection.DEFAULT_PASSWORD;

    privateStringurl=ActiveMQConnection.DEFAULT_BROKER_URL;

    privateStringsubject="TOOL.DEFAULT";

    privatebooleantopic=false;

    privatebooleantransacted=false;

    privatebooleanpersistent=false;

    publicstaticvoidmain(String[]args){

        ProducerToolproducerTool=newProducerTool();

      String[]unknonwn=CommnadLineSupport.setOptions(producerTool,args);

      if(unknonwn.length>0){

          System.out.println("Unknownoptions:

"+Arrays.toString(unknonwn));

            System.exit(-1);

      }          

      producerTool.run();

    }

    publicvoidrun(){

        Connectionconnection=null;

        try{

            System.out.println("ConnectingtoURL:

"+url);

            System.out.println("PublishingaMessagewithsize"+messageSize+"to"+(topic?

"topic":

"queue"+":

"+subject);

            System.out.println("Using"+(persistent?

"persistent":

"non-persistent"+"messages";

            System.out.println("Sleepingbetweenpublish"+sleepTime+"ms";

            if(timeToLive!

=0){

                System.out.println("Messagestimetolive"+timeToLive+"ms";

            }

            

            //Createtheconnection.

            ActiveMQConnectionFactoryconnectionFactory=newActiveMQConnectionFactory(user,password,url);            

            connection=connectionFactory.createConnection();

            connection.start();

            

            //Createthesession

            Sessionsession=connection.createSession(transacted,Session.AUTO_ACKNOWLEDGE);

            if(topic){

                destination=session.createTopic(subject);

            }else{

                destination=session.createQueue(subject);

            }

            

            //Createtheproducer.

            MessageProducerproducer=session.createProducer(destination);

            if(persistent){

                producer.setDeliveryMode(DeliveryMode.PERSISTENT);

            }else{

                producer.setDeliveryMode(DeliveryMode.NON_PERSISTENT);

  

展开阅读全文
相关资源
猜你喜欢
相关搜索

当前位置:首页 > 高等教育 > 医学

copyright@ 2008-2022 冰豆网网站版权所有

经营许可证编号:鄂ICP备2022015515号-1