8

How can we move a service bus queue message to the dead letter through service bus queue trigger function

jaas
  • 131
  • 1
  • 5

4 Answers4

11

https://github.com/Azure/azure-webjobs-sdk/issues/1986#issuecomment-433960534

In v3, you can bind to the MessageReceiver class, which exposes methods like DeadLetter, Abaondon, Complete, etc. Example:

public static async Task ProcessMessage(
   [ServiceBusTrigger("myqueue")] string message, int deliveryCount,
   MessageReceiver messageReceiver,
   string lockToken)
{
   . . .
   await messageReceiver.DeadLetterAsync(lockToken);
   . . .
}

In this example, the message is bound as a string and the various message properties including lockToken are bound as params. You can also bind the message as a Message Type and access the requisite message properties from there. In v2 the ServiceBus SDK exposed these message methods directly on the BrokeredMessage class itself, but in the latest version of their SDK those methods no longer exist, meaning you have to bind to MessageReceiver to access them.

Edit you also need to set AutoComplete to false when you do this. https://learn.microsoft.com/en-us/azure/azure-functions/functions-bindings-service-bus-trigger?tabs=csharp#configuration

Alex AIT
  • 17,361
  • 3
  • 36
  • 73
  • any java example? – Musa Nov 26 '20 at 15:04
  • Only works for C# unfortunately. https://learn.microsoft.com/en-us/azure/azure-functions/functions-bindings-service-bus-trigger?tabs=csharp#usage – Alex AIT Nov 26 '20 at 15:22
  • It works, but an error pops up _"Message processing error (Action=Complete). The lock supplied is invalid. Either the lock expired, or the message has already been removed from the queue, or was received by a different receiver instance."_ Not sure, but to me, it seems like Azure Function still tries to complete/abandon the message which is already dead lettered and not on the queue anymore. It's like it knows nothing about me handling the message manually inside the function. – Prolog Aug 27 '21 at 12:20
  • 1
    You need to set `AutoComplete` to false when you do this. https://learn.microsoft.com/en-us/azure/azure-functions/functions-bindings-service-bus-trigger?tabs=csharp#configuration – Alex AIT Aug 27 '21 at 12:55
  • Thank you! Had to bump `Microsoft.Azure.WebJobs.Extensions.ServiceBus` package to `4.3.0` as `AutoComplete` property was made available only recently. It works flawlessly now, thank you very much. Saved me hours of searching for this undoubtedly. – Prolog Aug 27 '21 at 13:36
2

In latest versions (5.5.1 for me), you must use the ServiceBusMessageActions class from the Microsoft.Azure.WebJobs.ServiceBus namespace. It looks like this:

[FunctionName("MyFunction")]
public static async Task Run(
    [ServiceBusTrigger("myQueue", Connection = "myConnection")]
    ServiceBusReceivedMessage message,
    ServiceBusMessageActions messageActions)
{
    ...
    await messageActions.DeadLetterMessageAsync(message);
    ...
}

The NuGet package you want to use is Microsoft.Azure.WebJobs.Extensions.ServiceBus.

Martin Wickman
  • 19,662
  • 12
  • 82
  • 106
1

I have not tried it but what you can do is set the MaxDeliveryCount property on the queue to 1 and then throw an exception in the function as soon as it is triggered. That way the message's delivery count will increase by 1 and Service Bus will automatically dead letter the message.

Gaurav Mantri
  • 128,066
  • 12
  • 206
  • 241
-2

Reading the Dead Letter Queue messages is by creating the Azure Function Trigger in the Azure portal. In the Function, provide the name of the DLQ as “QueueName/$DeadLetterQueue” as shown in the below image

enter image description here

Note: If you want to access the undelivered message from the Topic then, the syntax of reading the Dead Letter Queue will be “TopicName/Subscriptions/SubscriptionName/$DeadLetterQueue”.

Now, define what should be done with the DLQ messages. Here, as shown in the below screenshot, we are sending the DLQ messages of “myqueue” to the Topic named of “queue” using the Azure Service Bus

enter image description here

enter image description here

In this manner, we can handle the DLQ messages as required very easily using the Azure Functions.

DixitArora-MSFT
  • 1,768
  • 1
  • 5
  • 8
  • 2
    I believe the question is not about reading messages from DLQ but to send them to DLQ. – Gaurav Mantri Apr 13 '20 at 16:03
  • Just an observation: the wording and screenshots here seem to exactly match part 2 of https://www.inkeysolutions.com/blogs/handling-dead-letter-queuedlq-using-azure-functions/ – Richardissimo Dec 08 '22 at 14:18