Home > Net >  Is it possible to listen to a service bus with multiple azure functions?
Is it possible to listen to a service bus with multiple azure functions?

Time:04-06

I'm trying to listen to a service bus that can contain messages with multiple event types. When a message arrives I want to process it based on the event type. I have 2 azure functions to handle the different event type messages, but when one function receives the message the other one is not triggered. Is it possible to trigger both of them and let them decide which one processes the message?

Here is some sample code, these are in 2 separate projects:

[FunctionName("Function1")]
public async Task RunAsync(
    [ServiceBusTrigger("queueName", Connection = "connectionName")]
    Message message,
    MessageReceiver messageReceiver,
    ILogger log)
{
    var body = Encoding.Default.GetString(message.Body);
    var messageType = _helper.GetMessageType(body);

    if (messageType is not MessageType.Type1)
        return;

    // business logic
    await messageReceiver.CompleteAsync(message.SystemProperties.LockToken);
}

[FunctionName("Function2")]
public async Task RunAsync(
    [ServiceBusTrigger("queueName", Connection = "connectionName")]
    Message message,
    MessageReceiver messageReceiver,
    ILogger log)
{
    var body = Encoding.Default.GetString(message.Body);
    var messageType = _helper.GetMessageType(body);

    if (messageType is not MessageType.Type2)
        return;

    // business logic

    await messageReceiver.CompleteAsync(message.SystemProperties.LockToken);
}

CodePudding user response:

With Azure Service Bus Queues, it is not possible as a Queue can only have a single consumer.

A better approach would be to use enter image description here

  • Related