0

我在 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()
4

1 回答 1

1

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

您可以像这样配置心跳:

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

有关更多详细信息,请参阅stomp.py 文档

于 2021-08-03T17:00:51.503 回答