PHP, Python Client for ACTIVEMQ

Source: Internet
Author: User
Tags ack stomp vars

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.

  1. <transportconnectors>
  2. <transportconnector name="Openwire" uri="tcp://0.0.0.0:61616"/>
  3. <transportconnector name="Stomp" uri="stomp://0.0.0.0:61613"/><!-- Add Stomp connector --
  4. </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:

  1. Import time, Sys
  2. Import Stomp
  3. #消息侦听器
  4. Class MyListener (object):
  5. def on_error (self, headers, message):
  6. Print ' received an error%s '% message
  7. def on_message (self, headers, message):
  8. print '%s '% message
  9. #创建连接
  10. conn = Stomp. Connection ([(' 127.0.0.1 ',61613)])
  11. #设置消息侦听器
  12. Conn.set_listener (", MyListener ())
  13. #启动连接
  14. Conn.start ()
  15. Conn.connect ()
  16. #订阅主题, and uses the automatic message acknowledgement mechanism
  17. 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:

Extension=stomp.so

To access the PHP code for ACTIVEMQ:

  1. <?php
  2. $topic = '/topic/all_news ';
  3. /* Connection */
  4. try {
  5. $stomp = New Stomp (' tcp://127.0.0.1:61613 ');
  6. } catch (Stompexception $e) {
  7. Die (' Connection failed: '. $e->getmessage ());
  8. }
  9. /* Subscribe to messages from the queue ' foo ' */
  10. $stomp->subscribe ($topic);
  11. /* Read a frame */
  12. while (true) {
  13. $frame = $stomp->readframe ();
  14. if ($frame! = null) {
  15. echo $frame->body;
  16. / * Acknowledge that frame is received * /
  17. $stomp->ack ($frame);
  18. }
  19. }
  20. /* Close Connection */
  21. unset ($stomp);
  22. ?>

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

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

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.