【问题标题】:Subscription with selector does not work from python - stomp.py使用选择器订阅在 python 中不起作用 - stomp.py
【发布时间】:2017-01-05 15:20:58
【问题描述】:

我遇到了一个问题,即使用带有消息选择器的 stomp (stomp.py) 的 Python 订阅者没有收到它应该收到的消息。有趣的是,至少在我看来,问题出在发送消息而不是订阅上。

我正在使用 ActiveMQ。

这是订阅者代码:

class Listener(object):

    def __init__(self, count):
        if count <= 0:
            count = float('inf')
        self.count = count

    def on_error(self, headers, message):
        print("=" * 72)
        print('RECEIVED AN ERROR.')
        print('Message headers:')
        pp = pprint.PrettyPrinter(indent=4)
        pp.pprint(headers)
        print('Message body:')
        print(message)

    def on_message(self, headers, message):
        print("=" * 72)
        print('Message headers:')
        pp = pprint.PrettyPrinter(indent=4)
        pp.pprint(headers)
        print('Message body:')
        print(message)

def main():
    global conn

    args = parse_args()
    conn = stomp.Connection([(args.host, args.port)])
    conn.set_listener('Listener', Listener(args.count))
    conn.start()
    conn.connect(login=args.user, passcode=args.password)

    if (args.selector):
        conn.subscribe(
            destination=args.destination,
            id=1,
            ack='auto',
            headers={'selector': args.selector}
        )
    else:
        conn.subscribe(
            destination=args.destination,
            id=1,
            ack='auto'
        )

现在我可以使用“type = 'test'”之类的选择器运行此订阅者。

如果我使用 Java JMS 发布消息,则可以正常接收消息。但是,如果我从 Python 发布相同的消息,则不是。

以下是相关的 Python 发布代码:

headers = {}
headers['type'] = 'test'

conn = stomp.Connection12([(args.host, args.port)], auto_content_length=False)
conn.start()
conn.connect(login=args.user, passcode=args.password)
conn.send(body=body, headers=headers, destination=args.destination)
conn.disconnect()
print 'Message sent.'

我的测试和调试中的一些有趣的笔记:

  1. 使用选择器运行订阅者会收到一条从 Java JMS 但不是从 Python 发送的匹配消息。
  2. 在没有选择器的情况下运行订阅者会收到一条来自 Java 的消息以及一条来自 Python 的消息。

【问题讨论】:

    标签: python stomp


    【解决方案1】:

    相当老,但我目前面临同样的问题,所以我想在这里留下一个可能的解决方案。

    首先,according to the documentation,您可以提供一个名为 selector 的字段,其语法类似于 SQL,并且应该是 headers 的一部分。在您的示例中:

    headers = {}
    headers['selector'] = "type='test'"
    conn = stomp.Connection12([(args.host, args.port)], auto_content_length=False)
    conn.start()
    conn.connect(login=args.user, passcode=args.password)
    conn.send(body=body, headers=headers, destination=args.destination)
    conn.disconnect()
    print 'Message sent.'
    

    我也遇到了错误,我无法接收到JMS发送的任何消息,但经过大量阅读,我发现here,有一个字段名称JMSType。我将代码更改为

    headers['selector'] = "type='test' OR JMSType='test'"
    

    有了那个 JMSType,一切都像预期的那样工作。希望对某人有所帮助

    【讨论】:

      猜你喜欢
      • 2020-03-24
      • 1970-01-01
      • 2017-08-12
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2022-07-05
      相关资源
      最近更新 更多