Not able to connect to AWS Active MQ broker

1.2k Views Asked by At

We are trying to connect to AWS Amazon MQ using python but facing an issue. Please find below code and error.

code:

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={})

Error:

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

Any help appreciated.

2

There are 2 best solutions below

0
On

This is what I use with stand-alone ActiveMQ Artemis, not AWS. I also filter message subscription on a selector field called "key". Remove that selector from the headers if you want. I'm using 30 second heartbeats here.

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

There's a handful of problems (and potential problems) with your code.

First, you're using this as the hostname:

stomp+ssl://xxxxxxxxxxxxxxxxxx.mq.xxxxxxxxxxxxxxxx.amazonaws.com

The stomp+ssl:// should not be included in the hostname.

Second, you're using this as the port:

61616

By default the broker will not accept STOMP connection on port 61616. You should likely use 61613 instead.

Third, you're subscribing to # which looks like something you might do with an MQTT client (although even that is discouraged with MQTT). The destination # has no special meaning in STOMP as the STOMP specification defines no wildcards. Your STOMP client will subscribe to a destination literally named # which is likely not what you want.