Stomp 监听器在一段时间后下降,但程序 运行 没有显示任何错误

Stomp listeners are dropping after some time but program is running without showing any errors

我在 Activemq 队列上有一个 stomp 侦听器,它在启动一段时间后自行退出。 该程序本身没有显示错误,并显示为 运行 状态,但 Activemq UI 上列出的侦听器在一段时间后仅显示 0。 我正在使用此代码

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

由于您没有使用 STOMP heart-beating,您的应用程序无法检测到死连接也就不足为奇了。

您可以像这样配置心跳:

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

有关详细信息,请参阅 stomp.py documentation