入站端点确认模式

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

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

由于 Channel 是对“活动”对象的引用,因此它不能序列化,并且如果消息持久化,它会丢失。

以下示例展示了如何使用 MANUAL 确认

@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;
}
© . This site is unofficial and not affiliated with VMware.