从python脚本发送回复消息

时间:2016-05-16 00:38:32

标签: python activemq

我开发了一个camel应用程序,它具有通过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知识非常有限,所以非常欢迎一些解释。

提前感谢您的意见。

1 个答案:

答案 0 :(得分:0)

问题出在这一行:

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

一旦我添加body=response,一切都完美无缺。因此,固定的send调用如下所示:

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