如何将消息重定向到 Azure 服务总线的死信队列

huangapple go评论81阅读模式
英文:

How to redirect message to dead-letter queue Azure Service Bus

问题

我正在使用隔离的 Azure 函数来接收队列中的消息。我需要验证接收到的消息,如果无效,将其发送到死信队列。我找到的唯一方法是引发异常,经过 10 次重试后,消息将被移动到死信队列。当然,这不是一个好的解决方案。也许有人遇到过同样的任务?谢谢!

[Function("Example")]
public async Task ExampleAsync([ServiceBusTrigger("example", Connection = "ServiceBusConnection")] string entityId)
{
    if (!int.TryParse(entityId, out var id))
    {
        // TODO: 移动到死信队列
    }
}
英文:

I'm using isolated azure function to receive message from the queue. I need to validate received message and if it invalid, send it to the dead-letter queue. The only way how to do it I found is to throw exception and after 10 retries the message will be moved to dead letter queue. Of course it is not good solution. Maybe anyone faced the same task? Thanks!

 [Function("Example")]
 public async Task ExampleAsync([ServiceBusTrigger("example", Connection = "ServiceBusConnection")] string entityId)
 {
     if (!int.TryParse(entityId, out var id))
     {
         // TODO: Move to dead-letter queue
     }
 }  

答案1

得分: 2

如果您正在使用内部处理工作程序,可以通过将绑定到函数中的 ServiceBusMessageActions 并调用其 DeadLetterMessageAsync 方法来将消息移至死信。 这还要求您绑定到 ServiceBusMessage 而不仅仅是 string entityId

例如:

[FunctionName("BindingToMessageActions")]
public static async Task Run(
    [ServiceBusTrigger("<queue_name>", Connection = "<connection_name>")]
    ServiceBusReceivedMessage message,
    ServiceBusMessageActions messageActions)
{
    if (!int.TryParse(message.Body.ToString(), out var id))
    {
        await messageActions.DeadLetterMessageAsync(message);
    }
}

更多示例可在包的 README 中找到。

遗憾的是,在隔离的处理模型中暂时还不可用。

英文:

If you're using the in-process worker, you can dead-letter by binding to ServiceBusMessageActions in your function and then calling its DeadLetterMessageAsync method. This would also require that you bind to ServiceBusMessage rather than just string entityId.

For example:

[FunctionName(&quot;BindingToMessageActions&quot;)]
public static async Task Run(
    [ServiceBusTrigger(&quot;&lt;queue_name&gt;&quot;, Connection = &quot;&lt;connection_name&gt;&quot;)]
    ServiceBusReceivedMessage message,
    ServiceBusMessageActions messageActions)
{
    if (!int.TryParse(message.Body.ToString()), out var id))
    {
        await messageActions.DeadLetterMessageAsync(message);
    }
}

More examples can be found in the package README.

Unfortunately, this is not yet available in the isolated process model.

huangapple
  • 本文由 发表于 2023年7月27日 21:10:35
  • 转载请务必保留本文链接:https://go.coder-hub.com/76780091.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定