I working on a system in which I need to manage ordered messages throw a service bus. In my company we have everything on Azure cloud and I'm using Azure service bus for messaging. I read about the session and it seems that it could solve my challenge out of the box with Azure service bus but it is working only with a single instance. Once I scale out the we job to multiple instances, more than one instance is consuming the same messages from the same session. In my POC I using a Topic with a subscription.
Is someone able to make it work with scaling to multiple instances of the receiver? The second option I have is to implement by myself the Resequencer enterprise pattern, do you have any suggestions?
Here is the code I'm using to receive the messages:
static async Task InitializeReceiver(string connectionString, string queueName, CancellationToken ct)
{
var receiverFactory = MessagingFactory.CreateFromConnectionString(connectionString);
ct.Register(() => receiverFactory.Close());
var client = receiverFactory.CreateSubscriptionClient(queueName, "parallel", ReceiveMode.ReceiveAndDelete);
client.RegisterSessionHandler(
typeof(SessionHandler),
new SessionHandlerOptions
{
AutoRenewTimeout = TimeSpan.FromMinutes(5),
MessageWaitTimeout = TimeSpan.FromSeconds(120),
MaxConcurrentSessions = 100,
AutoComplete = false
});
}
Here is the code I use for processing:
public async Task OnMessageAsync(MessageSession session, BrokeredMessage message)
{
await ProcessMessage(session, message, currentInstanceId, recipeStep);
// If I process the last message of the session
if (message.Sequence == numberOfMsgPerSession)
{
// end of the session!
await session.CloseAsync();
logger.LogInformation($"Session with id {message.SessionId} is completed.");
}
}
Thanks. Regards.