无法连接到 AWS Active MQ 代理
python 255
原文标题 :Not able to connect to AWS Active MQ broker
我们正在尝试使用 python 连接到 AWS Amazon MQ 但遇到问题。请在下面找到代码和错误。
代码:
import stomp
#Establish a connection
con = stomp.Connection([('stomp+ssl://xxxxxxxxxxxxxxxxxx.mq.xxxxxxxxxxxxxxxx.amazonaws.com',61616)])
#listener class to be instantiated.
class Listener(stomp.ConnectionListener):
def on_error(self, headers, message):
print('received an error "%s"' % message)
def on_message(self, headers, message):
print('received a message "%s"' % message)
con.set_listener('', Listener())
#wait will ensure it waits till connection is established and acknowledged.
# con.start()
con.connect('xxxxxxxx', 'xxxxxxxxx', wait=True)
#subscribe to a particular topic or queue by giving the path and headers if required by the server.
con.subscribe('#', headers={})
错误:
Could not connect to host stomp+ssl://xxxxxxxxxxxxxxxxxxxx.mq.xxxxxxx.amazonaws.com, port 61616
Could not connect to host stomp+ssl://xxxxxxxxxxxxxxxxxxxx.mq.xxxxxxx.amazonaws.com, port 61616
Could not connect to host stomp+ssl://xxxxxxxxxxxxxxxxxxxx.mq.xxxxxxx.amazonaws.com, port 61616
Traceback (most recent call last):
File "stomp_mqtt_subscribe.py", line 34, in <module>
con.connect('xxxxxxxx', 'xxxxxxxx', wait=True)
File "/home/ubuntu/.local/lib/python3.8/site-packages/stomp/connect.py", line 150, in connect
self.transport.start()
File "/home/ubuntu/.local/lib/python3.8/site-packages/stomp/transport.py", line 130, in start
self.attempt_connection()
File "/home/ubuntu/.local/lib/python3.8/site-packages/stomp/transport.py", line 834, in attempt_connection
raise exception.ConnectFailedException()
stomp.exception.ConnectFailedException
任何帮助表示赞赏。
回复
我来回复-
Andrew 评论
这是我与独立 ActiveMQ Artemis 一起使用的,而不是 AWS。我还在一个名为“key”的选择器字段上过滤消息订阅。如果需要,请从标题中删除该选择器。我在这里使用 30 秒的心跳。
import stomp def connect_and_subscribe(conn): conn.connect('username', 'p@$$w0rd', wait=True) conn.subscribe(destination='my-topic', id=12345, ack='auto', headers={'subscription-type': 'MULTICAST', 'selector': "key like '"+key+"'"}) class MyListener(stomp.ConnectionListener): def __init__(self): self.counter=0 def on_message(self, frame): self.counter+=1 print('counter', self.counter) print('got:', frame) def on_disconnected(self): print('disconnected') connect_and_subscribe(self.conn) key='test' hosts = [('mqserver', 61616)] conn = stomp.Connection(host_and_ports=hosts, heartbeats=(30000, 30000)) conn.set_listener('', MyListener()) connect_and_subscribe(conn) while True: pass conn.disconnect()
2年前