【发布时间】: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.'
我的测试和调试中的一些有趣的笔记:
- 使用选择器运行订阅者会收到一条从 Java JMS 但不是从 Python 发送的匹配消息。
- 在没有选择器的情况下运行订阅者会收到一条来自 Java 的消息以及一条来自 Python 的消息。
【问题讨论】: