2
votes

I'm using the code to implement Azure service bus topics that can be found here: https://docs.microsoft.com/en-us/azure/service-bus-messaging/service-bus-dotnet-how-to-use-topics-subscriptions

I've tried to run two instances of the subscriber program, which holds these methods:

private static void RegisterOnMessageHandlerAndReceiveMessages()
{
    var messageHandlerOptions = new MessageHandlerOptions(ExceptionReceivedHandler)
    {
        MaxConcurrentCalls = 1,
        AutoComplete = false
    };
    _subscriptionClient.RegisterMessageHandler(ProcessMessagesAsync, messageHandlerOptions);
}

private static async Task ProcessMessagesAsync(Message message, CancellationToken token)
{
    Console.WriteLine($"Received #{message.SystemProperties.SequenceNumber} message: {Encoding.UTF8.GetString(message.Body)}");
    await _subscriptionClient.CompleteAsync(message.SystemProperties.LockToken);
}

enter image description here

However, this doesn't allow both subscribers to recieve the message, it is recieved one message each split across both subscribers.

How does I ensure both subscribers get all the messages coming from the bus?

1

1 Answers

3
votes

Azure Service Bus is a broker with Competing Consumer pattern when it comes to retrieving messages. This is expected behaviour. What you have here is a scaled-out processing endpoint, where both are trying to process messages from the same subscription. The messages are distributed between these two competing consumers. If you need to distribute the same message to more than a single subscriber, you should have different subscription entities created and listened to.