4
votes

I have several WebJobs with ServiceBus triggers, and I have a custom MessageProcessor to do some processing after the message has finished processing.

I'd like to be able to do something different (specifically, log an Error instead of a Warning) if the message is on its last retry, i.e. it's just about to be sent to the deadletter queue. The BrokeredMessage sent into the CompleteProcessingMessageAsync function has the DeliveryCount, but I can't see a way to get back to the original queue to find the MaxDeliveryCount. Any ideas? Different queues have different MaxDeliveryCounts, so setting a constant isn't really an option. The only other thing I can think of is to create a separate job for each queue's dead letter queue, but I'd like to be able to do it at the WebJob level rather than for each individual job.

public class CustomMessageProcessor : MessageProcessor
{
    public CustomMessageProcessor(OnMessageOptions messageOptions) : base(messageOptions)
    {
    }

    public override async Task CompleteProcessingMessageAsync(BrokeredMessage message, FunctionResult result, CancellationToken cancellationToken)
    {
        if (result.Succeeded)
        {
            if (!MessageOptions.AutoComplete)
            {
                cancellationToken.ThrowIfCancellationRequested();
                await message.CompleteAsync();
            }
        }
        else
        {
            cancellationToken.ThrowIfCancellationRequested();

            //some other processing

            //If message.DeliveryCount < maxDeliveryCount 
            //  log warning
            //else
            //  log error

            await message.AbandonAsync();
        }
    }
}
2

2 Answers

1
votes

You should be able to obtain Microsoft.ServiceBus.Messaging.QueueDescription from the parent queue, and then use MaxDeliveryCount off it. The only thing is that getting queue description is a relatively expensive operation, so I'd recommend doing it once initially, and cache the value for each queue.

0
votes

Different queues have different MaxDeliveryCounts, so setting a constant isn't really an option

As Slava Asipenko mentioned that we could set MaxDeliveryCount for servicebus queue.

how do I work out which queue the message came from

We could get the message properties to know the queue name if we set it during sending. Take Label for example:

var message = new BrokeredMessage(object);
message.Label = "queue name";
client.Send(message);