Niclas Gleesborg
Niclas Gleesborg

Reputation: 566

Retrieve message properties from Azure Service bus topics withn Python

Using Python 2.7 I need to get the properties of the message. I know the message contains 3 properties: cdId, active and alarm:

In C# I have this client which sends the message;

 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);

I have made a subscription and I am able to receive the messages using python but I have no clue how to get the properties of the message.

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)

I can get the "activated" part working because I send "Alarm Deactive" or "Alarm Active" as the message text but the is just hack I made to get it at least working partially. I need to be able to read the properties. I have tried msg.properties but that returns with an error that the properties attribute doesn't exists.

Upvotes: 1

Views: 1376

Answers (1)

rakshith91
rakshith91

Reputation: 752

In the v7 of the azure-servicebus, you can utilize the application_properties.

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

Upvotes: 1

Related Questions