简体   繁体   中英

How to avoid message going to `deadletter` queue azure service bus

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;
    }

My understanding is that you would want messages to go to dead letter, if left unprocessed/not completed.

Whenever a message has been delivered to the client - but it has not been completed for any reasons - the Delivery count will be automatically increased by 1 for that message. When the delivery count matches the count of the MaxDeliveryCount - the message is automatically moved to the dead letter queue.

Having said that, there is no way to get a message delivered without increasing the Delivery nor disable the movement of the message to the Dead letter queue on reaching the Max delivery count for the specific message.

Having said that, there can be two probable solutions that could closely match your requirement :

  • Have a very large MaxDelivery Count.
  • Consume the messages from the Deadletter queue - Resend the data to the queue - this may not be useful in your scenario. but just wanted to add it as a suggestion - just in case.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM