1

I have a stomp listener on an Activemq queue which just drops itself after being up for a while. The program itself shows no error and shows in running state but listener listed on Activemq UI just shows 0 after some time. I am using this code

class MyListener(stomp.ConnectionListener):
    def __init__(self, conn):
        self.conn = conn
        self.msg = []

    def on_error(self, frame):
        print('received an error "%s"' % frame.body)

    def on_message(self, frame):
        print('received a message "%s" from queue' % frame.body)
        headers = frame.headers
        self.conn.ack(id=headers["message-id"], subscription=headers["subscription"])

    def on_disconnected(self):
        print('disconnected')
        connect_and_subscribe(self.conn)
def main():
    conn = stomp.Connection([('localhost', 61613)])
    a = MyListener(conn)
    conn.set_listener('', a)
    connect_and_subscribe(conn)
    try:
        while True:
            if not conn.is_connected():
               print('disconnected... connecting again')
               connect_and_subscribe(conn)
            time.sleep(5)
    except KeyboardInterrupt:
        print('interrupted - so exiting!')
    conn.disconnect()
Mig B
  • 637
  • 1
  • 11
  • 19

1 Answers1

1

Since you aren't using STOMP heart-beating it's not terribly surprising that your application would not detect the dead connection.

You can configure heart-beating like so:

conn = stomp.Connection([('localhost', 61613)], heartbeats=(4000, 4000))

See the stomp.py documentation for more details.

Justin Bertram
  • 29,372
  • 4
  • 21
  • 43
  • Thanks for the reply. I tried this but after adding heartbeats my consumer started to connect and disconnect on a frequent basis.(per 12 seconds to be exact). I have used 4000,4000 in heartbeats. – Aviral Tyagi Aug 12 '21 at 11:26
  • That indicates to me that heart-beating is failing. Do you have any idea why heart-beating would fail? – Justin Bertram Aug 12 '21 at 13:49
  • AcitveMQ server is up all the time. I don't really know why would it fail. Are there any other reasons for this. Thanks. – Aviral Tyagi Aug 16 '21 at 05:24
  • A network delay or failure could cause this as well as a heavy load on the broker. I'm sure there are other potential causes as well, but those are the most likely off the top of my head. – Justin Bertram Aug 16 '21 at 12:21
  • @AviralTyagi Did you find out what is the problem? I am running into the same issue – Jane Jun 15 '22 at 06:37
  • If the ActiveMQ is hosted on EKS (as a pod) then this problem is still there. I had to create a middleware which can detect dropped consumers and restart the services. If ActiveMQ is running on a server then this solution did stabilize the connections. (for longer time periods ie. days) – Aviral Tyagi Jun 16 '22 at 07:34
  • @AviralTyagi Sorry, I didn't get a notification on your reply. ActiveMQ is running on a server. Yes, I notice that it runs at least for 2-3 days. Can you point me to any possibility of resolving the issue? Or is restarting the app only the solution? – Jane Jun 20 '22 at 08:56
  • @Jane No, I did not find any solution for this. I had to create a middleware which can restart the app when the consumer goes down. – Aviral Tyagi Jun 21 '22 at 17:01
  • If your application is using heart-beats with a competent STOMP client then it should detect any connection failure and allow the application to respond appropriately (e.g. stop, reconnect, etc.). You shouldn't have to write _another_ application to restart it. – Justin Bertram Jun 21 '22 at 17:14