ICode9

精准搜索请尝试: 精确搜索
首页 > 编程语言> 文章详细

使用Python绑定在Qpid Proton中设置自定义消息属性

2019-07-08 20:55:23  阅读:241  来源: 互联网

标签:python amqp qpid


我正在尝试使用Qpid Proton的Python绑定发送带有自定义属性的消息,但我找不到正确的方法来执行此操作…

  message = Message()
  message.body = u"hello body"
  data = Data()
  data.put_map()
  data.enter()
  data.put_string("key")
  data.put_string("value")
  data.exit()
  message.properties = data
  messenger.put(message)
  messenger.send()

结果是…

Traceback (most recent call last):
  File "./candy_ingest.py", line 37, in <module>
    messenger.put(message)
  File "/usr/lib/python2.7/dist-packages/proton.py", line 473, in put
    message._pre_encode()
  File "/usr/lib/python2.7/dist-packages/proton.py", line 781, in _pre_encode
    props.put_object(self.properties)
  File "/usr/lib/python2.7/dist-packages/proton.py", line 2036, in put_object
    putter = self.put_mappings[obj.__class__]
KeyError: <class proton.Data at 0x2320420>

欢迎任何帮助!

TIA,
托马斯.

解决方法:

这是我用于将RaspBerryPi连接到Windows Azure Service Bus的代码

import sys
from proton import *

#This code is for initiating the AMQP messenger
amqpmng = Messenger()
amqpmng._set_timeout(2000L) #Set timeout for sending and receiving at 2000 ms
address = "amqp://owner:<longpassword>@<namespace>.servicebus.windows.net/<queuename>" 

#This code is for creating messages
msg = Message()
msg.subject = "This is a testmessage"
msg.body = "Lorem ipsum dolor sit amet, consectetuer adipiscing elit."

#This code is for sending messages
try:
    msg.address = address
    amqpmng.put(msg)
    amqpmng.send()
except:
    e = sys.exc_info()[0]
    print e, "Waited for 2s to send messages, nothing send, connection timed out"

amqpmng.stop();

#This code is for receiving messages
amqpmng.subscribe(address)
amqpmng.start()
try:
    amqpmng.recv(1) #receive exactly 1 message (you can enter any value)
    msg = Message()
    while amqpmng.incoming > 0:
        amqpmng.get(msg)
        print(msg.body)
except:
    e = sys.exc_info()[0]
    print e, "Waited for 2s to receive messages, nothing received, connection timed out"

amqpmng.stop()

此外,消息类只有两个将数据作为参数的方法.

message.decode(data)
message.load(data)

我认为你应该使用message.load(data).

我从API参考中理解的是,message.properties使用python类dict来映射他的类属性.

如果它有用,请告诉我!

标签:python,amqp,qpid
来源: https://codeday.me/bug/20190708/1405879.html

本站声明: 1. iCode9 技术分享网(下文简称本站)提供的所有内容,仅供技术学习、探讨和分享;
2. 关于本站的所有留言、评论、转载及引用,纯属内容发起人的个人观点,与本站观点和立场无关;
3. 关于本站的所有言论和文字,纯属内容发起人的个人观点,与本站观点和立场无关;
4. 本站文章均是网友提供,不完全保证技术分享内容的完整性、准确性、时效性、风险性和版权归属;如您发现该文章侵犯了您的权益,可联系我们第一时间进行删除;
5. 本站为非盈利性的个人网站,所有内容不会用来进行牟利,也不会利用任何形式的广告来间接获益,纯粹是为了广大技术爱好者提供技术内容和技术思想的分享性交流网站。

专注分享技术,共同学习,共同进步。侵权联系[81616952@qq.com]

Copyright (C)ICode9.com, All Rights Reserved.

ICode9版权所有