python 发送和接收ActiveMQ消息的实例
2019-03-28 10:58
507 查看
ActiveMQ是java开发的消息中间件服务。可以支持多种协议(AMQP,MQTT,OpenWire,Stomp),默认的是OpenWire。而python与ActiveMQ的通信使用的是Stomp协议。而如果你的服务没有开启则需要配置开启。
首先需要安装python的stomp库。
命令如下:
pip install stomp.py
接着,就是上代码了具体如下:
# -*-coding:utf-8-*- import stomp import time queue_name = '/queue/SampleQueue' topic_name = '/topic/SampleTopic' listener_name = 'SampleListener' class SampleListener(object): def on_message(self, headers, message): print 'headers: %s' % headers print 'message: %s' % message # 推送到队列queue def send_to_queue(msg): conn = stomp.Connection10([('127.0.0.1',61613)]) conn.start() conn.connect() conn.send(queue_name, msg) conn.disconnect() #推送到主题 def send_to_topic(msg): conn = stomp.Connection10([('127.0.0.1',61613)]) conn.start() conn.connect() conn.send(topic_name, msg) conn.disconnect() ##从队列接收消息 def receive_from_queue(): conn = stomp.Connection10([('127.0.0.1',61613)]) conn.set_listener(listener_name, SampleListener()) conn.start() conn.connect() conn.subscribe(queue_name) time.sleep(1) # secs conn.disconnect() ##从主题接收消息 def receive_from_topic(): conn = stomp.Connection10([('127.0.0.1',61613)]) conn.set_listener(listener_name, SampleListener()) conn.start() conn.connect() conn.subscribe(topic_name) while 1: send_to_topic('topic') time.sleep(3) # secs conn.disconnect() if __name__=='__main__': # send_to_queue('len 123') # receive_from_queue() receive_from_topic()
但是上述只是发送文本类型的消息,除此之外,ActiveMQ还支持MapMessage、ObjectMessage、BytesMessage、和StreamMessage等多个消息类型。
以上这篇python 发送和接收ActiveMQ消息的实例就是小编分享给大家的全部内容了,希望能给大家一个参考,也希望大家多多支持脚本之家。
您可能感兴趣的文章:
相关文章推荐
- ActiveMQ实例1--简单的发送和接收消息
- activemq安装与简单消息发送接收实例
- ActiveMQ接收消息+发送消息的简单实例
- python、rabbitmq、pika、消息发送和接收实例
- 利用Spring与ActiveMQ整合发送、接收消息实例(Queue与Topic模式)
- activemq安装与简单消息发送接收实例
- activemq安装与简单消息发送接收实例
- activeMQ之点对点(p2p)发送与接收消息
- python 微信企业号-回调模式接收微信端客户端发送消息并被动返回消息
- JMS_使用ActiveMQ实现消息的发送和接收
- TCP网络通信实例--消息的发送与接收
- Activemq消息的发送与接收
- java调用rabbitmq消息队列发送和接收消息实例
- ActiveMQ 部署及发送接收消息
- Bluemix结合RabbitMq实现消息发送与接收实例
- spring整合activemq发送MQ消息[Topic模式]实例,activemqmq
- activemq集群搭建与多线程消息发送接收案例
- python基于protobu+websocket+tornado实现多种二进制协议消息的接收发送和解析demo
- ActiveMQ消息发送和接收
- ActiveMQ 整合 Spring 发送和接收消息