【问题标题】:Sending reply message from python script从 python 脚本发送回复消息
【发布时间】:2016-09-11 17:27:52
【问题描述】:

我开发了一个骆驼应用程序,它能够通过 Active MQ 代理与外部系统进行通信,目前我正在整理一个简短的演示文稿来展示它的工作原理。

为此,我选择了 python 作为外部系统,因为它是免费的、易于安装的,并且过去曾使用过一些 jython 脚本。 我想在演示文稿中展示的是,每当我的系统通过某个队列向外部客户端发送消息时,该客户端都会让它进行一些处理并回复消息头中指定的“回复”队列.

所以我对 Active MQ 发行版附带的 python 示例脚本进行了一些更改。这是我修改后的脚本:

import time
import sys
import os
import stomp

user = os.getenv("ACTIVEMQ_USER") or "admin"
password = os.getenv("ACTIVEMQ_PASSWORD") or "password"
host = os.getenv("ACTIVEMQ_HOST") or "localhost"
port = os.getenv("ACTIVEMQ_PORT") or 61613
destination = sys.argv[1]

class MyListener(stomp.ConnectionListener):

  def __init__(self, receiver, sender):
    self.receiver = receiver
    self.sender = sender
    self.count = 0
    self.start = time.time()

  def on_error(self, headers, message):
    print('received an error %s' % message)

  def on_message(self, headers, message):
    if message == "SHUTDOWN":

      diff = time.time() - self.start
      print("Received %s in %f seconds" % (self.count, diff))
      self.receiver.disconnect()
      self.sender.disconnect()
      sys.exit(0)

    else:
      if self.count==0:
        self.start = time.time()

      self.count += 1
      if self.count % 1000 == 0:
         print("Received %s messages." % self.count)

      if 'reply-to' in headers:
        replyTo = headers['reply-to']
        response = '%s Python says: this is very good indeed' % self.count
        self.sender.send(response, destination=replyTo, persistent='false')

sender = stomp.Connection(host_and_ports = [(host, port)])
sender.start()
sender.connect(login=user, passcode=password)

receiver = stomp.Connection(host_and_ports = [(host, port)])
receiver.set_listener('', MyListener(receiver, sender))
receiver.start()
receiver.connect(login=user, passcode=password)
receiver.subscribe(destination=destination, id=1, ack='auto')

print("Waiting for messages...")
while 1: 
  time.sleep(10)

然后我从我的系统内部发出一万条消息。 如果我注释掉发件人部分,我可以在控制台输出中看到 python 客户端接收到我的所有消息。但是,当我尝试回复时,我会收到此错误消息:

TypeError: send() got multiple values for argument 'destination'

更新: 这是我得到的完整堆栈跟踪

Waiting for messages...
Exception in thread Thread-2:
Traceback (most recent call last):
  File "D:\Dev\python\lib\threading.py", line 920, in _bootstrap_inner
    self.run()
  File "D:\Dev\python\lib\threading.py", line 868, in run
    self._target(*self._args, **self._kwargs)
  File "D:\Dev\python\lib\site-packages\stomp\transport.py", line 317, in __receiver_loop
    self.process_frame(f, frame)
  File "D:\Dev\python\lib\site-packages\stomp\transport.py", line 166, in process_frame
    self.notify(frame_type, f.headers, f.body)
  File "D:\Dev\python\lib\site-packages\stomp\transport.py", line 227, in notify
    rtn = notify_func(headers, body)
  File "D:/work/cls-message-router/gradle/scripts/listener4.py", line 42, in on_message
    self.sender.send(response, destination=replyTo, persistent='false')
TypeError: send() got multiple values for argument 'destination'

请您指出我在这里做错了什么以及我应该如何解决它。 我的python知识真的非常有限,所以非常欢迎一些解释。

提前感谢您的意见。

【问题讨论】:

  • 请将完整的错误回溯添加到您的问题中。

标签: python activemq


【解决方案1】:

问题出在这一行:

self.sender.send(response, destination=replyTo, persistent='false')

一旦我添加了body=response,一切都运行良好。所以固定的send 调用看起来像这样:

self.sender.send(body=response, destination=replyTo, persistent='false')

【讨论】:

    猜你喜欢
    • 2015-05-03
    • 1970-01-01
    • 1970-01-01
    • 2020-10-12
    • 1970-01-01
    • 2015-05-14
    • 2023-01-29
    • 2014-11-23
    • 1970-01-01
    相关资源
    最近更新 更多