消息延迟Message deferral

当队列或订阅客户端收到一条它愿意处理,但由于应用程序中出现特殊状况而无法处理的消息时,该客户端可将该消息的检索“延迟”到将来的某个时间点。When a queue or subscription client receives a message that it is willing to process, but for which processing is not currently possible due to special circumstances inside of the application, it has the option of "deferring" retrieval of the message to a later point. 该消息将保留在队列或订阅中,但会搁置处理。The message remains in the queue or subscription, but it is set aside.

延迟是专门针对工作流处理方案设计的功能。Deferral is a feature specifically created for workflow processing scenarios. 工作流框架可能需要以特定的顺序处理某些操作,并且可能需要延迟某些所收到的消息的处理,直到已完成其他消息所通知的、以前预定的工作。Workflow frameworks may require certain operations to be processed in a particular order, and may have to postpone processing of some received messages until prescribed prior work that is informed by other messages has been completed.

订单处理序列就是一个简单的演示示例:来自外部付款提供程序的付款通知先显示在系统中,然后将存储前端中的匹配采购订单传播到履行系统。A simple illustrative example is an order processing sequence in which a payment notification from an external payment provider appears in a system before the matching purchase order has been propagated from the store front to the fulfillment system. 在这种情况下,履行系统可能会延迟付款通知的处理,直到出现可与该通知关联的订单。In that case, the fulfillment system might defer processing the payment notification until there is an order with which to associate it. 在会合方案中,来自不同源的消息可推动工作流,实时执行顺序可能确实正确,但反映结果的消息可能不按顺序到达。In rendezvous scenarios, where messages from different sources drive a workflow forward, the real-time execution order may indeed be correct, but the messages reflecting the outcomes may arrive out of order.

最终,延迟有助于将消息的到达顺序重新排列为处理顺序,同时将需要延迟处理的消息安全保留在消息存储中。Ultimately, deferral aids in reordering messages from the arrival order into an order in which they can be processed, while leaving those messages safely in the message store for which processing needs to be postponed.

备注

延迟消息过期后不会自动移动到死信队列。Deferred messages will not be automatically moved to the dead-letter queue after they expire. 此行为是设计使然。This behaviour is by design.

消息延迟 APIMessage deferral APIs

.NET Framework 客户端中的 API 为 BrokeredMessage.DeferBrokeredMessage.DeferAsync,.NET 标准客户端中的 API 为 MessageReceiver.DeferAsync,Java 客户端中的 API 为 IMessageReceiver.deferIMessageReceiver.deferAsyncThe API is BrokeredMessage.Defer or BrokeredMessage.DeferAsync in the .NET Framework client, MessageReceiver.DeferAsync in the .NET Standard client, and IMessageReceiver.defer or IMessageReceiver.deferAsync in the Java client.

延迟的消息连同其他所有活动消息保留在主队列中(与保留在子队列中的死信消息不同),但不再可以使用正则 Receive/ReceiveAsync 函数接收。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. 如果应用程序不再能跟踪延迟的消息,可以通过消息浏览来发现这些消息。Deferred messages can be discovered via message browsing if an application loses track of them.

若要检索延迟的消息,其所有者在延迟它时需负责记住 SequenceNumberTo retrieve a deferred message, its owner is responsible for remembering the SequenceNumber as it defers it. 知道已延迟消息的序列号的任何接收器以后可使用 Receive(sequenceNumber) 显式接收该消息。Any receiver that knows the sequence number of a deferred message can later receive the message explicitly with Receive(sequenceNumber).

如果由于处理消息的特定资源暂时不可用,因而无法处理该消息,但同时又不能立即暂停消息处理,则将该消息搁置几分钟的方法是在要延后几分钟的计划消息中记住 SequenceNumber,并在计划的消息到达时重新检索已延迟的消息。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. 如果消息处理程序依赖于使用某个数据库来执行所有操作,而该数据库暂时不可用,则不应使用延迟,而应全面暂停接收消息,直到数据库再次可用。If a message handler depends on a database for all operations and that database is temporarily unavailable, it should not use deferral, but rather suspend receiving messages altogether until the database is available again.

后续步骤Next steps

若要了解有关服务总线消息传送的详细信息,请参阅以下主题:To learn more about Service Bus messaging, see the following topics: