title: Python stomp.py connection gets disconnected and listener stops working
body: <p>I am writing a python script using the python stomp library to connect and subscribe to an ActiveMQ message queue.</p>
  <p>My code is very similar to the examples in the documentation &quot;<a href="http://jasonrbriggs.github.io/stomp.py/api.html#dealing-with-disconnects" rel="nofollow noreferrer">Dealing with disconnects</a>&quot; with the addition of the timer being placed in a loop for a long running listener.</p>
  <p>The listener class is working to receive and process messages. However after a few minutes, the connection gets disconnected and then the listener stops picking up messages.</p>
  <p><strong>Problem:</strong></p>
  <p>The on_disconnected method is getting called which runs the connect_and_subscribe() method, however it seems the listener stops working after this happens. Perhaps the listener needs to be re-initialized? After the script is run again, the listener is re-created, it starts picking up messages again, but this is not practical to keep running the script again periodically.</p>
  <p><strong>Question 1:</strong> How can I set this up to re-connect and re-create the listener automatically?</p>
  <p><strong>Question 2:</strong> Is there a better way to initialize a long-running listener rather than <a href="https://github.com/jasonrbriggs/stomp.py/issues/206" rel="nofollow noreferrer">the timeout loop</a>?</p>
  <pre><code>import os, time, datetime, stomp
  
  _host = os.getenv('MQ_HOST')
  _port = os.getenv('MQ_PORT')
  _user = os.getenv('MQ_USER')
  _password = os.getenv('MQ_PASSWORD')
  _queue = os.getenv('QUEUE_NAME')
  # Subscription id is unique to the subscription in this case there is only one subscription per connection
  sub_id = 1
  
  def connect_and_subscribe(conn):
    conn.connect(_user, _password, wait=True)
    conn.subscribe(destination=_queue, id=sub_id, ack='client-individual')
    print('connect_and_subscribe connecting {} to with connection id {}'.format(_queue, sub_id), flush=True)
  
  class MqListener(stomp.ConnectionListener):
    def __init__(self, conn):
      self.conn = conn
      self.sub_id = sub_id
      print('MqListener init')
  
    def on_error(self, frame):
      print('received an error &quot;%s&quot;' % frame.body)
  
    def on_message(self, headers, body):
      print('received a message headers &quot;%s&quot;' % headers)
      print('message body &quot;%s&quot;' % body)
      time.sleep(1)
      print('processed message')
      print('Acknowledging')
      self.conn.ack(headers['message-id'], self.sub_id)
  
    def on_disconnected(self):
      print('disconnected! reconnecting...')
      connect_and_subscribe(self.conn)
  
  def initialize_mqlistener():
    conn = stomp.Connection([(_host, _port)], heartbeats=(4000, 4000))
    conn.set_listener('', MqListener(conn))
    connect_and_subscribe(conn)
    # https://github.com/jasonrbriggs/stomp.py/issues/206
    while conn.is_connected():
      time.sleep(2)
    conn.disconnect()
  
  if __name__ == '__main__':
    initialize_mqlistener()
  </code></pre>