標籤: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協議的連接器。
- <transportConnectors>
- <transportConnector name="openwire" uri="tcp://0.0.0.0:61616"/>
- <transportConnector name="stomp" uri="stomp://0.0.0.0:61613"/><!--添加stomp連接器-->
- </transportConnectors>
Python
安裝Python27,並安裝stomppy(http://code.google.com/p/stomppy/)這一用戶端庫:
基於stomppy訪問ActiveMQ的Python代碼:
- import time, sys
- import stomp
-
- #訊息接聽程式
- class MyListener(object):
- def on_error(self, headers, message):
- print ‘received an error %s‘ % message
-
- def on_message(self, headers, message):
- print ‘%s‘ % message
-
- #建立串連
- conn = stomp.Connection([(‘127.0.0.1‘,61613)])
-
- #設定訊息接聽程式
- conn.set_listener(‘‘, MyListener())
-
- #啟動串連
- conn.start()
- conn.connect()
-
- #訂閱主題,並採用訊息自動確認機制
- 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中添加該用戶端庫:
訪問ActiveMQ的PHP代碼:
- <?php
-
- $topic = ‘/topic/all_news‘;
-
- /* connection */
- try {
- $stomp = new Stomp(‘tcp://127.0.0.1:61613‘);
- } catch(StompException $e) {
- die(‘Connection failed: ‘ . $e->getMessage());
- }
-
- /* subscribe to messages from the queue ‘foo‘ */
- $stomp->subscribe($topic);
-
- /* read a frame */
- while(true) {
- $frame = $stomp->readFrame();
-
- if ($frame != null) {
- echo $frame->body;
-
- /* acknowledge that the frame was received */
- $stomp->ack($frame);
- }
- }
-
- /* close connection */
- unset($stomp);
-
- ?>
本文出自 “學習文檔” 部落格,請務必保留此出處http://zephiruswt.blog.51cto.com/5193151/1109606
ActiveMQ的PHP、Python用戶端