5

I have an Azure Function written in Python that has an Service Bus (Topic) output binding. The function is triggered by another queue, we process some files from a blobl storage and then put another message in a queue.

My function.json file looks like that:

{
"bindings": [
    {
    "type": "serviceBus",
    "connection": "Omnibus_Input_Send_Servicebus",
    "name": "outputMessage",
    "queueName": "validation-output-queue",
    "accessRights": "send",
    "direction": "out"
    }
],
"disabled": false
}

In my function, I can send a message to another queue like that:

with open(os.environ['outputMessage'], 'w') as output_message:
    output_message.write('This is my output test message !')

It is working fine. Now I'd like to send a message to a topic. I've created a subscription with an SQLFilter and I need to set some custom properties to the BrokeredMessage.

From the azure sdk for python, I've found that I can add custom properties like that (I've installed the azure module using pip):

from azure.servicebus import Message
sent_msg = Message(b'This is the third message',
    broker_properties={'Label': 'M3'},
    custom_properties={'Priority': 'Medium',
                        'Customer': 'ABC'}
)

My new function.json file looks like that:

{
"bindings": [
    {
    "type": "serviceBus",
    "connection": "Omnibus_Input_Send_Servicebus",
    "name": "outputMessage",
    "topicName": "validation-output-topic",
    "accessRights": "send",
    "direction": "out"
    }
],
"disabled": false
}

And I've modify my function like that:

from azure.servicebus import Message
sent_msg = Message(b'This is the third message',
    broker_properties={'Label': 'M3'},
    custom_properties={'Priority': 'Medium',
                        'Customer': 'ABC'}
)

with open(os.environ['outputMessage'], 'w') as output_message:
    output_message.write(sent_msg)

When I run the function, I get this exception:

TypeError: expected a string or other character buffer object

I tried to use the buffer and the memoryview function but still get another exception:

TypeError: cannot make memory view because object does not have the buffer interface

I am wondering if the actual binding supports BrokeredMessage and how to deal with it ?

3
  • Have you tried [broker_properties = '{"ForcePersistence": false, "Label": "My label"}' sent_msg = Message(b'receive message', broker_properties = broker_properties )] Commented Oct 10, 2017 at 8:11
  • Sorry not working Commented Oct 10, 2017 at 19:21
  • Please leverage the addiotional_properties in the ServiceBusMessage in the v7 of servicebus learn.microsoft.com/en-us/python/api/azure-servicebus/… Commented Jan 11, 2021 at 6:53

2 Answers 2

4

The ServiceBus output binding for Python (and other script languages) only supports a simple string mapping, where the string you specify becomes the content of the BrokeredMessage created behind the scenes. To set any extended properties or do anything more sophisticated, you'll have to drop down to using the Azure Python SDK yourself in your function.

Sign up to request clarification or add additional context in comments.

2 Comments

Is it something that's gonna be implemented in the future ? Should I open an issue in git repo ?
Is it the same for input binding ? Can I access the brokeredmessage using python ?
1

In the same situation, where I need to add user properties in the output service bus queue/topic, I used azure.servicebus.ServiceBusClient directly.

sb.Message class has a user_properties setter:

def main(
    httpreq: func.HttpRequest,
    context: func.Context  ):

sbClient : sb.ServiceBusClient = sb.ServiceBusClient.from_connection_string( os.getenv("AzureWebJobsServiceBus") )
topicClient : sb.TopicClient = sbClient.get_topic('scoring-testtopic')

message = sb.Message( httpreq.get_body().decode( 'UTF-8' ))

message.user_properties = { 
    '@AzureWebJobsParentId' : context.invocation_id,
    'Prom' : '31000001'
    }

topicClient.send( message )

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.