工具使用篇——python操作ActiveMq

准备

1、python需要安装stomp,stomp包的安装比较麻烦,建议在网上直接下载.whl文件,然后本地安装
2、本机需要安装acitvemq,安装过程参考[https://blog.csdn.net/weixin_41806489/article/details/104997519]
(https://www.csdn.net/).
注意: 一般用python是通过stomp协议进行通讯

操作过程

1、生产方
prublishi.py

import time
import sys
import os
import stomp

import time
import sys
import os
import stomp
# user = os.getenv("ACTIVEMQ_USER") or "admin"
user = "admin"
# password = os.getenv("ACTIVEMQ_PASSWORD") or "password"
password = "admin"
# host = os.getenv("ACTIVEMQ_HOST") or "localhost"
host = "localhost"
# port = os.getenv("ACTIVEMQ_PORT") or 61613
# port = os.getenv("ACTIVEMQ_PORT") or 61613
port = 61613
dest="test"
messages = 10000
data = "test from BeijingJiaotong"

conn = stomp.Connection(host_and_ports=[(host, port)])
# conn.start()
conn.connect(login=user, passcode=password)

for i in range(0, messages):
    conn.send(body=str(i), destination=dest, persistent='false')

# conn.send(body="SHUTDOWN", destination=dest, persistent='false')

conn.disconnect()

2、消费者
listener.py

import sys
import os
import stomp
user = "admin"
password = "admin"
host = "127.0.0.1"
port = 61613
dest="test"

destination = sys.argv[1:2] or ["/topic/event"]
print(sys.argv)
destination = destination[0]
class MyListener(object):
    def __init__(self, conn):
        self.conn = conn
        self.count = 0
        self.start = time.time()
    def on_error(self, headers, message):
        print('received an error %s' % message)
    def on_message(self, headers, message):
        print(message)
        if message == "SHUTDOWN":
            conn.disconnect()
            sys.exit(0)
        else:
            print(message)

conn = stomp.Connection(host_and_ports=[("localhost", 61613)])
conn.set_listener('', MyListener(conn))
# conn.start()
conn.connect(login=user, passcode=password)
conn.subscribe(destination=dest, ack='auto', id="")
print("Waiting for messages...")
while 1:
    time.sleep(10)
  • 4
    点赞
  • 7
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值