Inbound Endpoint Acknowledge Mode

默认情况下,入站端点使用 AUTO 确认模式,这意味着容器在下游集成流完成后(或使用 QueueChannelExecutorChannel 将消息传递给另一线程)时自动确认消息。将模式设置为 NONE 会配置使用者,使其根本不使用确认(代理会在发送消息后立即自动确认消息)。将模式设置为 MANUAL 会让用户代码在处理期间的某个其他点确认消息。为了支持此模式,端点在 amqp_channelamqp_deliveryTag 头中分别提供了 ChanneldeliveryTag

By default, the inbound endpoints use the AUTO acknowledge mode, which means the container automatically acknowledges the message when the downstream integration flow completes (or a message is handed off to another thread by using a QueueChannel or ExecutorChannel). Setting the mode to NONE configures the consumer such that acknowledgments are not used at all (the broker automatically acknowledges the message as soon as it is sent). Setting the mode to MANUAL lets user code acknowledge the message at some other point during processing. To support this, with this mode, the endpoints provide the Channel and deliveryTag in the amqp_channel and amqp_deliveryTag headers, respectively.

你可以在 Channel 上执行任何有效的 Rabbit 命令,但通常情况下只使用 basicAckbasicNack(或 basicReject)。为了不干扰容器的操作,你不应保留对通道的引用,并且仅在当前消息的上下文中使用它。

You can perform any valid Rabbit command on the Channel but, generally, only basicAck and basicNack (or basicReject) are used. In order to not interfere with the operation of the container, you should not retain a reference to the channel and use it only in the context of the current message.

由于 Channel 是对 “live” 对象的引用,因此它无法序列化并将在持久化消息时丢失。

Since the Channel is a reference to a “live” object, it cannot be serialized and is lost if a message is persisted.

以下示例显示了你如何使用 MANUAL 确认:

The following example shows how you might use MANUAL acknowledgement:

@ServiceActivator(inputChannel = "foo", outputChannel = "bar")
public Object handle(@Payload String payload, @Header(AmqpHeaders.CHANNEL) Channel channel,
        @Header(AmqpHeaders.DELIVERY_TAG) Long deliveryTag) throws Exception {

    // Do some processing

    if (allOK) {
        channel.basicAck(deliveryTag, false);

        // perhaps do some more processing

    }
    else {
        channel.basicNack(deliveryTag, false, true);
    }
    return someResultForDownStreamProcessing;
}