使用Python从Azure Service总线主题检索消息属性

时间:2014-10-28 13:12:16

标签: python azure azureservicebus

使用Python 2.7我需要获取消息的属性。我知道该消息包含3个属性:cdId,active和alarm:

在C#中,我有这个发送消息的客户端;

 string connectionString = CloudConfigurationManager.GetSetting("Microsoft.ServiceBus.ConnectionString");
            TopicClient newClient = TopicClient.CreateFromConnectionString(connectionString, "cdMessages");

 var serviceMsg = new BrokeredMessage("Alarm Deactive");
            serviceMsg.Properties["cdId"] = message.Properties["cdId"];
            serviceMsg.Properties["active"] = false;
            serviceMsg.Properties["alarm"] = false;
            newClient.Send(serviceMsg);

我订阅了,我可以使用python接收消息,但我不知道如何获取消息的属性。

key_name = '******'
key_value ='******'
service_namespace1 = '******' 
sbs = ServiceBusService(service_namespace=service_namespace1,
                        shared_access_key_name=key_name,
                        shared_access_key_value=key_value)
Active = "active"
Deactive = "Deactivate"


 sbs.create_subscription('cdmessages', 'AllMessages')
 while True: 

msg = sbs.receive_subscription_message('cdmessages', 'AllMessages', peek_lock=False)
print(msg.body)
MessageString = str(msg.body)

if MessageString.find(Active) == True
    newState = "Activated"
    return(newState)

我可以让“激活”部分正常工作,因为我发送了“Alarm Deactive”或“Alarm Active”作为消息文本,但这只是我为了让它至少部分工作而做的。我需要能够阅读属性。我已经尝试了msg.properties但是返回时出现了属性属性不存在的错误。

1 个答案:

答案 0 :(得分:0)

在 azure-servicebus 的 v7 中,您可以使用 /Library/Frameworks/Python.framework/Versions /System/Library/Frameworks/Python.framework/Versions

https://docs.microsoft.com/en-us/python/api/azure-servicebus/azure.servicebus.servicebusmessage?view=azure-python