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()