繁体   English   中英

使用选择器的订阅在python-stomp.py中不起作用

[英]Subscription with selector does not work from python - stomp.py

我遇到了一个问题,即使用带有消息选择器的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发送的消息。

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

首先, 根据文档 ,您可以提供一个名为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发送的任何消息,但经过大量阅读后,我发现这里有一个字段名称JMSType 我将代码更改为

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

有了那个JMSType,一切都像预期的那样工作。 希望能帮到别人

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM