ActiveMQ的PHP、Python用戶端

來源:互聯網
上載者:User

標籤:unset   top   php5   des   configure   設定   .so   body   nec   

ActiveMQ這款開源Message Service器提供了多語言支援,除了一般的Java用戶端以外,還可以使用C/C++、PHP、Python、JavaScript(Ajax)等語言開發用戶端。最近由於項目需要,需要提供PHP和Python的主題訂閱用戶端。這裡作為總結,列出這兩種語言用戶端的簡單安裝和使用。

對於PHP和Python,可以通過使用STOMP協議與Message Service器進行通訊。在ActiveMQ的設定檔activemq.xml中,需要添加以下語句,來提供基於STOMP協議的連接器。

  1. <transportConnectors> 
  2.     <transportConnector name="openwire" uri="tcp://0.0.0.0:61616"/> 
  3.     <transportConnector name="stomp" uri="stomp://0.0.0.0:61613"/><!--添加stomp連接器--> 
  4. </transportConnectors> 

Python
安裝Python27,並安裝stomppy(http://code.google.com/p/stomppy/)這一用戶端庫:

基於stomppy訪問ActiveMQ的Python代碼:

  1. import time, sys 
  2. import stomp 
  3.  
  4. #訊息接聽程式 
  5. class MyListener(object): 
  6.     def on_error(self, headers, message): 
  7.         print ‘received an error %s‘ % message 
  8.  
  9.     def on_message(self, headers, message): 
  10.         print ‘%s‘ % message 
  11.          
  12. #建立串連 
  13. conn = stomp.Connection([(‘127.0.0.1‘,61613)]) 
  14.  
  15. #設定訊息接聽程式 
  16. conn.set_listener(‘‘, MyListener()) 
  17.  
  18. #啟動串連 
  19. conn.start() 
  20. conn.connect() 
  21.  
  22. #訂閱主題,並採用訊息自動確認機制 
  23. conn.subscribe(destination=‘/topic/all_news‘, ack=‘auto‘) 

PHP

安裝PHP5,並安裝STOMP的用戶端庫(http://php.net/manual/zh/book.stomp.php):

tar -zxf stomp-1.0.5.tgz 
cd stomp-1.0.5/
/usr/local/php/bin/phpize 
./configure --enable-stomp --with-php-config=/usr/local/php/bin/php-config
make
make install

安裝完成後,將產生的stomp.so移入php.ini中指定的extension_dir目錄下,並在php.ini中添加該用戶端庫:

extension=stomp.so

訪問ActiveMQ的PHP代碼:

  1. <?php 
  2.  
  3. $topic  = ‘/topic/all_news‘; 
  4.  
  5. /* connection */ 
  6. try { 
  7.     $stomp = new Stomp(‘tcp://127.0.0.1:61613‘); 
  8. } catch(StompException $e) { 
  9.     die(‘Connection failed: ‘ . $e->getMessage()); 
  10.  
  11. /* subscribe to messages from the queue ‘foo‘ */ 
  12. $stomp->subscribe($topic); 
  13.  
  14. /* read a frame */ 
  15. while(true) { 
  16.         $frame = $stomp->readFrame(); 
  17.          
  18.         if ($frame != null) { 
  19.             echo $frame->body; 
  20.  
  21.             /* acknowledge that the frame was received */ 
  22.             $stomp->ack($frame); 
  23.         } 
  24.  
  25. /* close connection */ 
  26. unset($stomp); 
  27.  
  28. ?> 

 

本文出自 “學習文檔” 部落格,請務必保留此出處http://zephiruswt.blog.51cto.com/5193151/1109606

ActiveMQ的PHP、Python用戶端

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.