如何在服务总线队列触发器函数中将服务总线消息移动到死信队列?

8
我们如何通过服务总线队列触发器函数将服务总线队列消息移动到死信中?
4个回答

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.

编辑时,您还需要将AutoComplete设置为false。https://learn.microsoft.com/en-us/azure/azure-functions/functions-bindings-service-bus-trigger?tabs=csharp#configuration

有任何Java示例吗? - Musa
很遗憾,这个只适用于C#。https://learn.microsoft.com/en-us/azure/azure-functions/functions-bindings-service-bus-trigger?tabs=csharp#usage - Alex AIT
它能运行,但弹出一个错误:“消息处理错误(Action = Complete)。提供的锁是无效的。锁定要么已过期,要么消息已从队列中删除,要么被不同接收器实例接收。” 我不确定,但对我来说,似乎Azure Function仍然尝试完成/放弃已经死信且不再在队列中的消息。就像它完全不知道我在函数内手动处理消息一样。 - Prolog
1
当您执行此操作时,需要将“AutoComplete”设置为false。https://learn.microsoft.com/en-us/azure/azure-functions/functions-bindings-service-bus-trigger?tabs=csharp#configuration - Alex AIT
谢谢!最近才推出了“AutoComplete”属性,因此不得不将Microsoft.Azure.WebJobs.Extensions.ServiceBus包升级到4.3.0。现在它完美地工作了,非常感谢。毫无疑问,这为我节省了数小时的搜索时间。 - Prolog

2
在最新版本中(对我来说是5.5.1),您必须使用Microsoft.Azure.WebJobs.ServiceBus命名空间中的ServiceBusMessageActions类。它看起来像这样:

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

您想要使用的 NuGet 包是 Microsoft.Azure.WebJobs.Extensions.ServiceBus

1
我没有尝试过,但你可以将队列上的MaxDeliveryCount属性设置为1,然后在函数触发时立即抛出异常。这样,消息的传递次数将增加1次,Service Bus会自动将该消息死信化。

-2

通过在Azure门户中创建Azure函数触发器来读取死信队列消息。在函数中,将DLQ的名称提供为“QueueName/$DeadLetterQueue”,如下图所示。

enter image description here

注意:如果您想访问主题中未投递的消息,则读取死信队列的语法应为“TopicName/Subscriptions/SubscriptionName/$DeadLetterQueue”。

现在,定义应该如何处理DLQ消息。在这里,如下面的截图所示,我们使用Azure Service Bus将“myqueue”的DLQ消息发送到名为“queue”的主题。

enter image description here

enter image description here

以这种方式,我们可以使用 Azure Functions 非常轻松地处理所需的 DLQ 消息。

2
我认为这个问题不是关于从DLQ中读取消息,而是将它们发送到DLQ。 - Gaurav Mantri
只是一个观察:这里的措辞和截图似乎与 https://www.inkeysolutions.com/blogs/handling-dead-letter-queuedlq-using-azure-functions/ 的第二部分完全相同。 - Richardissimo

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接