消息序列化和时间戳Message sequencing and timestamps

序列化和时间戳是所有服务总线实体上始终启用的两项功能,通过收到或检索到的消息的 SequenceNumberEnqueuedTimeUtc 属性体现。Sequencing and timestamping are two features that are always enabled on all Service Bus entities and surface through the SequenceNumber and EnqueuedTimeUtc properties of received or browsed messages.

如果消息的绝对顺序至关重要,和/或使用者需要消息的可信唯一标识符,中转站会向消息分发相对于队列或主题的无间隔递增序列号。For those cases in which absolute order of messages is significant and/or in which a consumer needs a trustworthy unique identifier for messages, the broker stamps messages with a gap-free, increasing sequence number relative to the queue or topic. 对于已分区实体,序列号是相对于分区进行分发。For partitioned entities, the sequence number is issued relative to the partition.

SequenceNumber 值是在中转站接受并存储消息时分配给消息的唯一 64 位整数,可用作内部标识符。The SequenceNumber value is a unique 64-bit integer assigned to a message as it is accepted and stored by the broker and functions as its internal identifier. 对于已分区实体,最前面的 16 位数反映的是分区标识符。For partitioned entities, the topmost 16 bits reflect the partition identifier. 在 48/64 位范围用尽后,序列号会回滚到 0。Sequence numbers roll over to zero when the 48/64-bit range is exhausted.

序列号可作为唯一标识符受到信任,因为它是由中央主管中立机构(而不是客户端)分配。The sequence number can be trusted as a unique identifier since it is assigned by a central and neutral authority and not by clients. 它还表示真正的到达顺序,比作为顺序条件的时间戳更为精确,因为时间戳在消息传递速度极快时的解析度可能不够高,并且可能会在中转站所有权跨节点转让时受到时钟偏差的影响(不过影响很小)。It also represents the true order of arrival, and is more precise than a time stamp as an order criterion, because time stamps may not have a high enough resolution at extreme message rates and may be subject to (however minimal) clock skew in situations where the broker ownership transitions between nodes.

绝对到达顺序非常重要。例如,在供货量有限的业务方案中,在供应期内货物供应遵循“先到先得”的原则;音乐会门票销售就是一个例子。The absolute arrival order matters, for example, in business scenarios in which a limited number of offered goods are served on a first-come-first-served basis while supplies last; concert ticket sales are an example.

时间戳功能起到中立可信机构的作用,可准确捕获消息的到达时间 (UTC),通过 EnqueuedTimeUtc 属性体现。The time-stamping capability acts as a neutral and trustworthy authority that accurately captures the UTC time of arrival of a message, reflected in the EnqueuedTimeUtc property. 此值非常适用于依赖截止时间、但处理进度远远落后于队列积压工作 (backlog) 的业务方案,如需要确定工作项是否在特定日期的午夜前提交。The value is useful if a business scenario depends on deadlines, such as whether a work item was submitted on a certain date before midnight, but the processing is far behind the queue backlog.

计划的消息Scheduled messages

可以将消息提交到队列或主题以供延迟处理;例如,将作业安排为在特定时间可供系统处理。You can submit messages to a queue or topic for delayed processing; for example, to schedule a job to become available for processing by a system at a certain time. 此功能实现了可靠的分布式时间计划程序。This capability realizes a reliable distributed time-based scheduler.

在定义的排队时间前,计划的消息不会在队列中具体化。Scheduled messages do not materialize in the queue until the defined enqueue time. 在此之前,可以取消计划的消息。Before that time, scheduled messages can be canceled. 取消操作会将消息删除。Cancellation deletes the message.

可以在通过常规发送路径发送消息时设置 ScheduledEnqueueTimeUtc 属性,也可以明确使用 ScheduleMessageAsync API 来安排消息。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. 后一种方法立即返回计划的消息的 SequenceNumber,稍后可用于根据需要取消计划的消息。The latter immediately returns the scheduled message's SequenceNumber, which you can later use to cancel the scheduled message if needed. 也可以使用消息浏览,发现计划的消息及其序列号。Scheduled messages and their sequence numbers can also be discovered using message browsing.

只有当计划的消息处于此状态时,消息的 SequenceNumber 才有效。The SequenceNumber for a scheduled message is only valid while the message is in this state. 当消息转换为有效状态时,消息就会被追加到队列中,就像瞬时排入队列一样,包括分配新的 SequenceNumber。As the message transitions to the active state, the message is appended to the queue as if had been enqueued at the current instant, which includes assigning a new SequenceNumber.

因为此功能与各个消息绑定在一起,并且消息只能排入队列一次,所以服务总线不支持定期安排消息。Because the feature is anchored on individual messages and messages can only be enqueued once, Service Bus does not support recurring schedules for messages.

后续步骤Next steps

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