2016-05-16 110 views
1

我開發了一個駱駝應用程序,該應用程序具有通過Active MQ代理與外部系統進行通信的功能,目前我正在組織一個簡短的演示文稿以展示它的工作原理。從python腳本發送回覆消息

爲此,我選擇了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客戶端收到我的所有消息。但是隻要我試圖來響應IA m如果此錯誤消息:

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

UPDATE: 這是一個完整的堆棧跟蹤我越來越

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' 

能否請你發現了我在這裏做錯了什麼,我該如何解決它。 我的蟒蛇知識真的非常有限,所以有點解釋會非常受歡迎。

非常感謝您的意見。

+0

請將完整的錯誤追溯添加到您的問題。 –

回答

0

的問題是在這條線:

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

一旦我加入body=response一切完美。因此,固定的send調用如下所示:

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