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 閱讀(8328) 評論(2)  編輯  收藏 所屬分類: JMS

          評論

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

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

          主站蜘蛛池模板: 万荣县| 永靖县| 资阳市| 桑日县| 牡丹江市| 长白| 平山县| 武邑县| 江华| 昭觉县| 武宣县| 剑川县| 荆州市| 泰安市| 嘉荫县| 原阳县| 响水县| 淳化县| 峨眉山市| 石景山区| 昂仁县| 寻甸| 鄂尔多斯市| 阳东县| 定西市| 霍州市| 定南县| 新田县| 万山特区| 当雄县| 定西市| 隆子县| 凭祥市| 安阳市| 凌海市| 凉山| 田阳县| 土默特右旗| 合水县| 聊城市| 卓资县|