I have service bus queue configured in azure which will trigger a azure function when a message has been sent.I want to leverage it , based on the key present in the Queue message , it should automatically trigger the different azure functions.
I have tried implementing this below Code :
import azure.functions as func
import logging
import json
app = func.FunctionApp()
@app.service_bus_queue_trigger(arg_name="azservicebus", queue_name="queue-asb",
connection="azuretopicnamespace_SERVICEBUS")
def servicebus_queue_trigger(azservicebus: func.ServiceBusMessage):
message_body = azservicebus.get_body().decode('utf-8')
message_data = json.loads(message_body)
if message_data.get('key_val','') =='service_bus_queue':
print("Inside the if statment")
result = service_bus_queue(azservicebus)
print("Result",result)
else:
print("Inside the else statement")
servicebus_trigger(azservicebus)
@app.service_bus_queue_trigger(arg_name="azservicebus", queue_name="queue-asb",
connection="azuretopicnamespace_SERVICEBUS")
def service_bus_queue(azservicebus:func.ServiceBusMessage):
a = 1+2
print(a)
print("This is the first function will be triggered")
return a
@app.service_bus_queue_trigger(arg_name="azservicebus", queue_name="queue-asb",
connection="azuretopicnamespace_SERVICEBUS")
def servicebus_trigger(azservicebus: func.ServiceBusMessage):
print("This is the second function will be trigered")
I am not sure whether this is right approach or not ?
An Azure Function is invoked by a queue or subscription and you can't trigger a different function based on the payload since a function is always tied to a single entity.
Depending on how many keys you've got, you could use a fan out strategy, where you publish a message to a topic and have subscriptions that subscribe to the topic and filter out messages based on the keys. This approach will require the following.
This approach is feasible for a small number of keys. If the number of keys is large, or it's dynamic in its nature, you'll need to have a different approach.