I am consuming messages from service bus topic/subscription. The ProcessMessage
adding messages to one capacity bounded queue (not more than 200 messages at a time). Once one message processing is done then I made it CompleteAsync
, only when ProcessMessage
gives true
.
Now if some exception occurs, say queue is full and not ready to take any new messages then I am generating exception and ProcessMessage
gives false
. In this case _subscriptionClient.CompleteAsync
not called, but message is going to deadletter
queue.
How to prevent this? message should not go to deadletter
queue and it's should wait for sometime to process ?
Note - I added AbandonAsync
logic as per comment suggestion, but still the message going to deadletter and not re-appearing in topic subscription. Please suggest!
Max delivery count = 5, It's tried 5 times then it's moved to deadletters
_subscriptionClient = new SubscriptionClient(connectionString, topicName, subscriptionName);
_subscriptionClient.RegisterMessageHandler(
async (message, token) =>
{
if (await ProcessMessage(message, token))
{
await _subscriptionClient.CompleteAsync(message.SystemProperties.LockToken);
}
else
{
await _subscriptionClient.AbandonAsync(message.SystemProperties.LockToken);
}
},
new MessageHandlerOptions(ExceptionReceivedHandler) { MaxConcurrentCalls = 1, AutoComplete = false });
private async Task<bool> ProcessMessage(Message message, CancellationToken token)
{
var processed = false;
try
{
//adding message to queue for further process
processed = true;
}
catch
{
//in case queue is full, generating exception and return false
processed = false;
}
return processed;
}