c# - 如何将消息放入死信并在azure函数中经过一段时间后处理它

标签 c# azure azure-functions azureservicebus

我有azure函数,当我们将新消息保存到服务总线主题中时会触发该函数。

在 azure 函数中,我通过调用客户 API 并检查其状态代码检查其是否可用

如果状态代码为 200,我需要处理该消息,否则将此消息放入死信中,并在一段时间间隔后,当客户 api 可用时,也处理所有死信消息。

 public static class Function1
    {
        [FunctionName("Function1")]
        public static void Run([ServiceBusTrigger("customer-order", "customer-order", Connection = "")]string mySbMsg, ILogger log)
        {
            // 1.call customer api to check it is available or not 
            // 2.if it is up and running then process the message else put message into dead-letter 
            // and after some interval when customer ai is available process dead-letter messages

            log.LogInformation($"C# ServiceBus topic trigger function processed message: {mySbMsg}");
        }
    }

我可以使用 HTTPClient 调用客户 api 并处理消息。

but how to put message into dead-letter and How to execute dead-letter after some interval when customer api is available ?

建议流程为

in azure function app which will trigger if new message is there into.

topic start step - Check api is available or down if api is available

process the current message if api is down then do not process

message here we have two choices

1a.put current message into dead letter  

1b.put current message back into main but if we do this then again function app will trigger as its new message trigger based and start step will continue.

最佳答案

与其将其放入死信队列,更好的方法是 defer the message在一定时间内。

If a message cannot be processed because a particular resource for handling that message is temporarily unavailable but message processing should not be summarily suspended, a way to put that message on the side for a few minutes is to remember the SequenceNumber in a scheduled message to be posted in a few minutes, and re-retrieve the deferred message when the scheduled message arrives.

查看此answer有关如何在 Azure Functions v2 中进行延迟的示例。请注意,输入绑定(bind)使用 Message 类型的消息,并且还使用注入(inject)的 MessageReceiver。需要这些才能调用 DeferAsync。服务总线触发器的模板代码将消息类型设置为字符串,因此您必须按照该答案中的描述更改签名。

关于延迟消息:

Deferred messages remain in the main queue along with all other active messages (unlike dead-letter messages that live in a subqueue), but they can no longer be received using the regular Receive/ReceiveAsync functions. To retrieve a deferred message, its owner is responsible for remembering the SequenceNumber as it defers it. Any receiver that knows the sequence number of a deferred message can later receive the message explicitly with Receive(sequenceNumber).

如何使用延迟消息的序列号来调度消息,以便可以稍后处理延迟消息:

You can schedule messages either by setting the ScheduledEnqueueTimeUtc property when sending a message through the regular send path, or explicitly with the ScheduleMessageAsync API

关于c# - 如何将消息放入死信并在azure函数中经过一段时间后处理它,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57492305/

相关文章:

c# - 如何从组中获取第一项并准备数据类对象

azure - 跨项目共享 azure devops 变量

azure - 如何手动将消息发送到Azure Function中的服务总线死信队列?

具有 IoT 中心触发器的 Java Azure 函数未启动

f# - 引用外部程序集失败

c# - ItemsControl 文本框名称在 .cs 文件中不起作用

c# - 集成测试将数据库文件添加到我的测试项目。我该怎么做

c# - 用于 Visual Studio 项目的超简单轻量级源代码控制?

powershell - 如何在arm模板中配置APNS.Certificate

c# - 无法使用 Web API 在 Azure ARM 上创建存储帐户