python 使用 stomp.py 连接 activemq

python 连接 activemq 需要使用到 stomp 包,使用的端口号 61613,见文档 https://stomppy.readthedocs.io/en/latest/quickstart.html#command-line-client

安装方式:pip install stomp.py,安装完成后可以使用自带的 shell 命令行

$ stomp --help
Stomp.py command-line client

Usage: stomp [options]

Options:
  --version                    Show the version number and exit
  -h, --help                   Show this help message and exit
  -H <host>, --host=<port>     Hostname or IP address to connect to. [default: localhost]
  -P <port>, --port=<port>     Port providing stomp protocol connections. [default: 61613]
  -U <user>, --user=<user>     Username for the connection
  -W <password>, --password=<password>
                               Password for the connection
  -F <filename>, --file=<filename>
                               File containing commands to be executed, instead of
                               prompting from the command prompt.
  -S <protocol version>, --protocol=<protocol version>
                               Set the STOMP protocol version (1.0, 1.1, 1.2) [default: 1.1]
  -L <queue>, --listen=<queue> Listen for messages on a queue/destination
  -V, --verbose                Verbose logging "on" or "off" (if on, full headers
                               from stomp server responses are printed)
  --heartbeats=<heartbeats>    Heartbeats to request when connecting with protocol >=
                               1.1 (two comma separated integers required) [default: 0,0]
  --ssl                        Enable SSL connection
  --ssl-key-file=<key-file>    ssl key file
  --ssl-cert-file=<cert-file>  ssl cert file
  --ssl-ca-file=<ca-file>      ssl ca certs file

eg:

stomp -H 172.17.0.2

> subscribe /queue/test
Subscribing to '/queue/test' with acknowledge set to 'auto', id set to '1'

> send /queue/test 1
>
message-id: ID:3ef8b0e81b2f-40709-1620135904353-3:1:-1:1:1
subscription: 1

可以从 web 中看到 queue 中多了一条 test 的消息

stomp.py 的使用脚本

脚本 1

import stomp

conn = stomp.Connection()
conn.set_listener('', MyListener())
conn.start()
conn.connect('admin', 'password', wait=True)
conn.send(body=' '.join(sys.argv[1:]), destination='/queue/test')
conn.disconnect()

 脚本2

发送消息到队列Queue属于 点对点模式,不可以重复消费;

发送消息到主题Topic属于 发布/订阅模式,可以重复消费;

# Send a Message to an Apache ActiveMQ Queue
import stomp

conn = stomp.Connection10()

conn.start()

conn.connect()

conn.send('SampleQueue', 'Simples Assim')

conn.disconnect()

# Receive a Message from an Apache ActiveMQ Queue
import stomp
import time

class SampleListener(object):
  def on_message(self, headers, msg):
    print(msg)

conn = stomp.Connection10()

conn.set_listener('SampleListener', SampleListener())

conn.start()

conn.connect()

conn.subscribe('SampleQueue')

time.sleep(1) # secs

conn.disconnect()

# Send a Message to an Apache ActiveMQ Topic
import stomp

conn = stomp.Connection10()

conn.start()

conn.connect()

conn.send('/topic/SampleTopic', 'Simples Assim')

conn.disconnect()

# Receive a Message from an Apache ActiveMQ Topic (1)
import stomp
import time

class SampleListener(object):
  def on_message(self, headers, msg):
    print(msg)

conn = stomp.Connection10()

conn.set_listener('SampleListener', SampleListener())

conn.start()

conn.connect()

conn.subscribe('/topic/SampleTopic')

time.sleep(1) # secs

conn.disconnect()

# Receive a Message from an Apache ActiveMQ Topic (2)
import stomp
import time

class SampleListener(object):
  def on_message(self, headers, msg):
    print(msg)

conn = stomp.Connection10()

conn.set_listener('SampleListener', SampleListener())

conn.start()

conn.connect(headers={'client-id':'SampleClient'})

conn.subscribe(destination='/topic/SampleTopic', headers={'activemq.subscriptionName':'SampleSubscription'})

time.sleep(1) # secs

conn.disconnect()

 

 

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值