I'm using Azure service bus topic with C# and ASP.NET Core. I have created session enabled subscription and I'm using ServiceBusSessionProcessor to receive messages from this topic.
I would like to set SessionIdleTimeout to 15 seconds and handle an event after this time has passed. If there is no message received I would like to update a record in my database and stop processing.
Is this possible? Is there anyway I can know that there was no message received in those 15 seconds?
Some of the code:
public BaseSessionMessageProcessor(
IMessageHandler<T> handler,
string topicName,
string subscriptionName,
string sessionId,
IServiceBusPersisterConnection serviceBusPersisterConnection,
IMessageHelper messageHelper,
IAppLogger<T> logger)
{
_handler = handler;
_messageHelper = messageHelper;
_logger = logger;
var options = new ServiceBusSessionProcessorOptions
{
MaxConcurrentSessions = 2,
MaxConcurrentCallsPerSession = 1,
AutoCompleteMessages = false,
SessionIds = { sessionId },
SessionIdleTimeout = TimeSpan.FromSeconds(15)
};
_processor = serviceBusPersisterConnection.ServiceBusClient.CreateSessionProcessor(topicName, subscriptionName, options);
RegisterSessionMessageProcessor().GetAwaiter().GetResult();
}
public async Task RegisterSessionMessageProcessor()
{
_processor.ProcessMessageAsync += SessionMessageHandler;
_processor.ProcessErrorAsync += ErrorHandler;
await _processor.StartProcessingAsync();
}
public async Task SessionMessageHandler(ProcessSessionMessageEventArgs args)
{
_logger.LogInformation($"log-info: process started");
}
public Task ErrorHandler(ProcessErrorEventArgs args)
{
var exception = args.Exception;
var context = args.ErrorSource;
_logger.LogError($"log-exception: An error occurred while trying to handle a message.", exception, context);
return Task.CompletedTask;
}
ServiceBusSessionProcessor
with the specifiedSessionIdleTimeout
property. It defines the maximum amount of time the processor will wait for a message to be received for the currently active session. If no message is received within this time frame, the processor will close the session and attempt to process another session.Output:
Listen for a specific session with processing:
Enable sessions
when creating a subscription and add various filters with properties.