ACTIVEMQ This open source messaging server provides multi-lingual support, in addition to the General Java client, you can also use the C + +, PHP, Python, JavaScript (Ajax) and other languages to develop the client. Recently, because of project needs, we need to provide PHP and Python theme subscription client. Here, as a summary, lists the simple installation and use of the client for both languages.
For PHP and Python, you can communicate with the messaging server by using the STOMP protocol. In ACTIVEMQ configuration file activemq.xml, you need to add the following statement to provide a connector based on the STOMP protocol.
- <transportconnectors>
- <transportconnector name="Openwire" uri="tcp://0.0.0.0:61616"/>
- <transportconnector name="Stomp" uri="stomp://0.0.0.0:61613"/><!-- Add Stomp connector --
- </transportconnectors>
Python
Install the Python27 and install the Stomppy (http://code.google.com/p/stomppy/) client library:
Python code to access ACTIVEMQ based on Stomppy:
- 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 ()
- #订阅主题, and uses the automatic message acknowledgement mechanism
- Conn.subscribe (destination='/topic/all_news ', ack=' auto ')
Php
Install PHP5, and install the Stomp Client library (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 |
After the installation is complete, move the generated stomp.so into the Extension_dir directory specified in php.ini and add the client library in php.ini:
To access the PHP code for ACTIVEMQ:
- <?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 frame is received * /
- $stomp->ack ($frame);
- }
- }
- /* Close Connection */
- unset ($stomp);
- ?>
This article is from the "Learning Documents" blog, so be sure to keep this source http://zephiruswt.blog.51cto.com/5193151/1109606
PHP, Python Client for ACTIVEMQ