python使用stomp连接activemq

python使用stomp连接activemq

       本篇内容为大家提供的是python使用stomp连接activemq和stomp简介,详细而全面,感兴趣的朋友,可以参考学习一下。


STOMP即Simple (or Streaming) Text Orientated Messaging Protocol,简单(流)文本定向消息协议,它提供了一个可互操作的连接格式,允许STOMP客户端与任意STOMP消息代理(Broker)进行交互。STOMP协议由于设计简单,易于开发客户端,因此在多种语言和多种平台上得到广泛地应用。

STOMP协议的前身是TTMP协议(一个简单的基于文本的协议),专为消息中间件设计。

STOMP是一个非常简单和容易实现的协议,其设计灵感源自于HTTP的简单性。尽管STOMP协议在服务器端的实现可能有一定的难度,但客户端的实现却很容易。例如,可以使用Telnet登录到任何的STOMP代理,并与STOMP代理进行交互。

业界已经有很多优秀的STOMP的服务器/客户端的开源实现,下面就介绍一下这方面的情况。

STOMP服务器

项目名                                  兼容STOMP的版本                                描述
Apache Apollo                      1.0 1.1 1.2                               ActiveMQ的继承者 http://activemq.apache.org/apollo
Apache ActiveMQ                1.0 1.1                                    流行的开源消息服务器 http://activemq.apache.org/
HornetQ                               1.0                                           来自JBoss的消息中间件 http://www.jboss.org/hornetq
RabbitMQ                           1.0 1.1 1.2                                基于Erlang、支持多种协议的消息Broker,通过插件支持STOMP协议 http://www.rabbitmq.com/plugins.html#rabbitmq-stomp
Stampy                               1.2                                            STOMP 1.2规范的一个Java实现 http://mrstampy.github.com/Stampy/
StompServer                       1.0                                          一个轻量级的纯Ruby实现的STOMP服务器 http://stompserver.rubyforge.org/
 

2、STOMP客户端库

项目名                             兼容STOMP的版本                描述
activemessaging                1.0                        Ruby客户端库 http://code.google.com/p/activemessaging/
onstomp                             1.0 1.1                  Ruby客户端库 https://rubygems.org/gems/onstomp
Apache CMS                      1.0                        C++客户端库 http://activemq.apache.org/cms/
Net::STOMP::Client            1.0 1.1 1.2            Perl客户端库 http://search.cpan.org/dist/Net-STOMP-Client/
Gozirra                                1.0                        Java客户端库 http://www.germane-software.com/software/Java/Gozirra/
libstomp                              1.0                        C客户端库,基于APR库 http://stomp.codehaus.org/C
Stampy                                1.2                        Java客户端库 http://mrstampy.github.com/Stampy/
stomp.js                              1.0 1.1                   JavaScript客户端库 http://jmesnil.net/stomp-websocket/doc/
stompest                             1.0 1.1 1.2             Python客户端库,全功能实现,包括同步和异步 https://github.com/nikipore/stompest
StompKit                            1.2                          Objective-C客户端库,事件驱动 https://github.com/mobile-web-messaging/StompKit/
stompngo                           1.0 1.1 1.2              Go客户端库 https://github.com/gmallard/stompngo
stomp.py                             1.0 1.1 1.2              Python客户端库 https://github.com/jasonrbriggs/stomp.py
tStomp                                1.1                           TCL客户端库 https://github.com/siemens/tstomp

下面介绍python使用stomp与activemq完成stomp通信

安装stomp.py:

https://github.com/jasonrbriggs/stomp.py

下载后安装:

python setup.py install

很简单,然后一个简单的示例:

import time
import 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('received a message %s' % message)

#官方示例的连接代码也落后了,现在分协议版本
conn = stomp.Connection10([('ip...',61613)])  
conn.set_listener('', MyListener())
conn.start()
conn.connect()

conn.subscribe(destination='/queue/test', id=1, ack='auto')
#注意,官方示例这样发送消息是有问题的
#conn.send(body='hello,garfield! this is '.join(sys.argv[1:]), destination='/queue/test')
conn.send(body='hello,garfield!', destination='/queue/test')

time.sleep(2)
conn.disconnect()

好了,可以发送、接收消息了!

  • 0
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 2
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值