<rt id="bn8ez"></rt>
<label id="bn8ez"></label>

  • <span id="bn8ez"></span>

    <label id="bn8ez"><meter id="bn8ez"></meter></label>

    hk2000c技術專欄

    技術源于哲學,哲學來源于生活 關心生活,關注健康,關心他人

      BlogJava :: 首頁 :: 新隨筆 :: 聯系 :: 聚合  :: 管理 ::
      111 隨筆 :: 1 文章 :: 28 評論 :: 0 Trackbacks

    本篇主要講解在未使用其他框架(Spring)整合情況下,獨立基于ActiveMQ,使用JMS規范進行消息通信。
        
         一.JMS回顧
           因為ActiveMQ是一個JMS Provider的實現,因此在開始實作前,有必要復習下JMS的基礎知識
        Java Message Service (JMS)是sun提出來的為J2EE提供企業消息處理的一套規范,JMS目前有2套規范還在使用JMS 1.0.2b和1.1. 1.1已經成為主流的JMS Provider事實上的標準了.
          *1.1主要在session上面有一些重要改變,比如支持建立同一session上的transaction,讓他支持同時發送P2P(Queue)消息和接受
    Topic消息。
          
           在JMS中間主要定義了2種消息模式Point-to-Point (點對點),Publich/Subscribe Model (發布/訂閱者),
        其中在Publich/Subscribe 模式下又有Nondurable subscription和durable subscription (持久化訂閱)2種消息處理方式。
        
         下面是JMS規范基本的接口和實現
         JMS Common Interfacse PTP-Specific Interface   Pub/Sub-specific interfaces
         ConnectionFactory     QueueConnectionFactory   TopicConnectionFactory
         Connection            QueueConnection          TopicConnection
         Destination           Queue                    Topic
         Session               QueueSession             TopiSession
         MessageProducer       QueueSender              TopicPublisher
         MessageConsumer       QueueReceiver/QueueBrwer TopicSubscriber


         二.使用Queue

             下面以ActiveMQ example的代碼為主進行說明
            
            1. 使用ActiveMQ的Connection,ConnectionFactory 建立連接,注意這里沒有用到pool
           

    java 代碼
    1. import org.apache.activemq.ActiveMQConnection   
    2. import org.apache.activemq.ActiveMQConnectionFactory   

            //建立Connection

    java 代碼
    1. protected Connection createConnection() throws JMSException, Exception {   
    2.      ActiveMQConnectionFactory connectionFactory = new ActiveMQConnectionFactory(user, pwd, url);   
    3.      Connection connection = connectionFactory.createConnection();   
    4.      if (durable && clientID!=null) {   
    5.          connection.setClientID(clientID);   
    6.      }   
    7.      connection.start();   
    8.      return connection;   
    9.     }  

            //建立Session
      

    java 代碼
    1. protected Session createSession(Connection connection) throws Exception {   
    2.          Session session = connection.createSession(transacted, ackMode);   
    3.          return session;   
    4.         }   

            2。發送消息的代碼
     //建立QueueSession
     

    java 代碼
    1. protected MessageProducer createProducer(Session session) throws JMSException {   
    2.         Destincation destination = session.createQueue("queue.hello");   
    3.         MessageProducer producer = session.createProducer(destination);   
    4.         producer.setDeliveryMode(DeliveryMode.NON_PERSISTENT);   
    5.            
    6.         if( timeToLive!=0 )   
    7.             producer.setTimeToLive(timeToLive);   
    8.         return producer;   
    9.         }   

             //使用Producer發送消息到Queue
        

    java 代碼
    1. producer.send(message);   

           
            3。接受消息,在JMS規范里面,你可以使用
      

    java 代碼
    1. QueueReceiver/QueueBrowser直接接受消息,但是更多的情況下我們采用消息通知方式,即實現MessageListener接口   
    2.  public void onMessage(Message message) {   
    3.  //process message   
    4.  }   
    5.           
    6.  //set MessageListner ,receive message   
    7.  Destincation destination = session.createQueue("queue.hello");   
    8.  consumer = session.createConsumer(destination);   
    9.  consumer.setMessageListener(this);   

           
            以上就是使用jms queue發送接受消息的基本方式

     
         三 Topic

            1. 建立連接
       

    java 代碼
    1. protected Connection createConnection() throws JMSException, Exception {      
    2.         ActiveMQConnectionFactory connectionFactory = new ActiveMQConnectionFactory(user, pwd, url);      
    3.         Connection connection = connectionFactory.createConnection();      
    4.         //如果你要使用DurableSubScription 方式,你必須為connection設置一個ClientID      
    5.         if (durable && clientID!=null) {      
    6.             connection.setClientID(clientID);      
    7.         }      
    8.         connection.start();      
    9.         return connection;      
    10.        }      

           2. 建立Session

    java 代碼
    1. protected Session createSession(Connection connection) throws Exception {      
    2.         Session session = connection.createSession(transacted, ackMode);      
    3.         return session;      
    4.         }    

     3.創建Producer 發送消息到Topic   
           

    java 代碼
    1. //create topic on  session   
    2.        topic = session.createTopic("topic.hello");   
    3.  producer = session.createProducer(topic);   
    4.        //send message    
    5.        producer.setDeliveryMode(DeliveryMode.NON_PERSISTENT);   
    6.  producer.send(message);   


     4.創建Consumer接受消息(基本上和Queue相同)

    java 代碼
    1. Destincation destination  = session.createTopic("topic.hello");      
    2. MessageConsumer consumer = session.createConsumer(destination);      
    3. consumer.setMessageListener(this);      
    4.            
    5.      //如果你使用的是Durable Subscription方式,你必須在建立connection的時候      
    6.      //設置ClientID,而且建立comsumer的時候使用createDurableSubscriber方法,為他指定一個consumerName。      
    7.  //connection.setClientID(clientId);      
    8.  //consumer = session.createDurableSubscriber((Topic) destination, consumerName);   

           
     四:連接ActiveMQ的方式
            ActiveMQConnectionFactory 提供了多種連接到Broker的方式activemq.apache.org/uri-protocols.html

     常見的有
     vm://host:port     //vm
     tcp://host:port    //tcp
     ssl://host:port    //SSL
     stomp://host:port  //stomp協議可以跨語言,目前有很多種stomp client 庫(java,c#,c/c++,ruby,python...);

    posted on 2007-11-16 17:05 hk2000c 閱讀(8329) 評論(2)  編輯  收藏 所屬分類: JMS

    評論

    # re: ActiveMQ 實踐之路(二) 使用Queue或者Topic發送/接受消息 2009-06-03 16:43 藍雨
    寫的太簡單了, 初學者肯定看不懂!  回復  更多評論
      

    # re: ActiveMQ 實踐之路(二) 使用Queue或者Topic發送/接受消息 2009-10-21 09:08 READ
    確實如樓上所說...  回復  更多評論
      

    主站蜘蛛池模板: 在线免费观看亚洲| 美女裸免费观看网站| 免费日本一区二区| 亚洲αv久久久噜噜噜噜噜| 国产三级在线免费观看| 亚洲熟妇无码另类久久久| 久久久精品国产亚洲成人满18免费网站| 免费h黄肉动漫在线观看| 久久免费观看国产精品88av| 亚洲一本一道一区二区三区| 亚洲综合AV在线在线播放| 一个人看www在线高清免费看| 色婷婷亚洲一区二区三区| 97在线观免费视频观看| 亚洲av成人综合网| 午夜一区二区免费视频| 日韩a毛片免费观看| 亚洲永久无码3D动漫一区| 一级毛片**不卡免费播| 亚洲a∨无码男人的天堂| 日美韩电影免费看| 一区二区三区视频免费观看| 国产精品亚洲成在人线| 免费福利视频导航| 天天综合亚洲色在线精品| 久久亚洲2019中文字幕| 在线免费观看国产| 爱情岛亚洲论坛在线观看| 亚洲成a人片在线观看无码| 国产在线观看免费观看不卡| 亚洲Av永久无码精品黑人| 亚洲日韩欧洲乱码AV夜夜摸| 久久成人国产精品免费软件| 国产精品亚洲专区无码唯爱网| 国产L精品国产亚洲区久久| 成人毛片免费播放| 91久久成人免费| 一级毛片免费视频| 国产成人精品无码免费看| 黄色网址在线免费观看| 精品久久久久久无码免费|