使用python和stomp激活activemq

问题描述 投票:0回答:1

我设置了activemq,并向Zendesk进行了大量的api调用。我需要检索这些呼叫,然后将其从队列中完全删除。 conn.ack似乎不起作用!

我正在使用python3和stomp的最新版本。我用它来制作初始连接脚本:https://github.com/jasonrbriggs/stomp.py/wiki/Simple-Example

https://jasonrbriggs.github.io/stomp.py/api.html在此文档中,您似乎必须标记.subscribe方法的“ id”标签。您使用该ID调用conn.ack,但还添加了消息ID作为参数。我发现消息的标题是使用侦听器功能检索的。我将它们打印出来,它们看起来像这样:

ID:[我的工作站ID] .local-49557-1560302581785-5:58:-1:1:61592

我尝试过正则表达式将ID:之后的整个字符串都进行正则表达式,然后尝试仅对字符串末尾的数字进行正则表达式(它看起来可能是唯一的数字),但是当我执行conn.ack(matchObj .group(1),4),队列计数没有改变,我也没有收到关于为什么没有改变的反馈。

到目前为止,连接绝对正常,我只是无法发送这些信号。

import stomp
import time
import re

class SampleListener(object):
    def on_message(self, headers, msg):
      regex = r"ID:.*1:1:(.*)"
      print(msg)
      print(headers['message-id'])

      matchObj = re.match ( regex, headers['message-id'], re.M|re.I)
      print (matchObj.group(1))

      conn.ack (matchObj.group(1), 4)

conn = stomp.Connection10()

conn.set_listener('SampleListener', SampleListener())

conn.start()

conn.connect()

conn.subscribe('zendeskqueue', id=4, ack='client')

time.sleep(1) # secs

conn.disconnect()

上面的代码没有错误,它只是存在而没有输出。

python activemq stomp
1个回答
0
投票

使用stomp.py和ActiveMQ确认消息使用ack ID headers['ack']

并且您可以通过在侦听器上实现on_error并启用调试日志记录来获得更多详细的输出。

同时使用两者,您的代码将如下所示:

import stomp
import time
import logging

class SampleListener(object):
  def on_message(self, headers, msg):
    conn.ack(headers['ack'])

  def on_error(self, headers, body):
    print("An error occurred: headers=" + str(headers) + " ; body=" + str(body))


logging.basicConfig(level=logging.INFO)
logging.getLogger('stomp').setLevel(logging.DEBUG)

conn = stomp.Connection12()

conn.set_listener('SampleListener', SampleListener())

conn.start()

conn.connect()

conn.subscribe('zendeskqueue', id=4, ack='client')

time.sleep(1) # secs

conn.disconnect()
© www.soinside.com 2019 - 2024. All rights reserved.