Asynchronous @RabbitListener Return Types

@RabbitListener(和 @RabbitHandler)方法可以用异步返回类型 CompletableFuture<?>Mono<?> 指定,允许异步发送回复。ListenableFuture<?> 不再受支持;它已被 Spring Framework 弃用。

@RabbitListener (and @RabbitHandler) methods can be specified with asynchronous return types CompletableFuture<?> and Mono<?>, letting the reply be sent asynchronously. ListenableFuture<?> is no longer supported; it has been deprecated by Spring Framework.

侦听器容器工厂必须配置为 AcknowledgeMode.MANUAL,以便消费者线程不应答消息;相反,当异步操作完成时,异步完成将应答或不应答消息。当异步结果完成并出现错误时,消息是否重新加入队列取决于抛出的异常类型、容器配置以及容器错误处理程序。默认情况下,消息将重新加入队列,除非将容器的 defaultRequeueRejected 属性设置为 false(其默认值为 true)。如果异步结果完成并出现 AmqpRejectAndDontRequeueException,则消息不会重新加入队列。如果容器的 defaultRequeueRejected 属性为 false,则你可以通过将 future 的异常设置为 ImmediateRequeueException 来覆盖它,且消息将重新加入队列。如果侦听器方法中发生某些异常从而导致无法创建异步结果对象,则你必须捕获该异常并返回适当的返回对象,使其导致应答或重新加入消息。

The listener container factory must be configured with AcknowledgeMode.MANUAL so that the consumer thread will not ack the message; instead, the asynchronous completion will ack or nack the message when the async operation completes. When the async result is completed with an error, whether the message is requeued or not depends on the exception type thrown, the container configuration, and the container error handler. By default, the message will be requeued, unless the container’s defaultRequeueRejected property is set to false (it is true by default). If the async result is completed with an AmqpRejectAndDontRequeueException, the message will not be requeued. If the container’s defaultRequeueRejected property is false, you can override that by setting the future’s exception to a ImmediateRequeueException and the message will be requeued. If some exception occurs within the listener method that prevents creation of the async result object, you MUST catch that exception and return an appropriate return object that will cause the message to be acknowledged or requeued.

从版本 2.2.21、2.3.13 和 2.4.1 开始,当检测到异步返回类型时,AcknowledgeMode 将自动设置为 MANUAL。此外,带有致命异常的传入消息将被单独否定确认,以前任何未确认的消息也都已否定确认。

Starting with versions 2.2.21, 2.3.13, 2.4.1, the AcknowledgeMode will be automatically set the MANUAL when async return types are detected. In addition, incoming messages with fatal exceptions will be negatively acknowledged individually, previously any prior unacknowledged message were also negatively acknowledged.

从版本 3.0.5 开始,@RabbitListener(和 @RabbitHandler)方法可以用 Kotlin suspend 标记,并且整个处理过程和回复生成(可选)发生在各自的 Kotlin 协程上。关于 AcknowledgeMode.MANUAL 的所有提及规则仍然适用。org.jetbrains.kotlinx:kotlinx-coroutines-reactor 依赖项必须存在于类路径中,以允许 suspend 函数调用。

Starting with version 3.0.5, the @RabbitListener (and @RabbitHandler) methods can be marked with Kotlin suspend and the whole handling process and reply producing (optional) happens on respective Kotlin coroutine. All the mentioned rules about AcknowledgeMode.MANUAL are still apply. The org.jetbrains.kotlinx:kotlinx-coroutines-reactor dependency must be present in classpath to allow suspend function invocations.

同样地,从 3.0.5 版本开始,如果已在具有异步返回类型的侦听器上配置了 RabbitListenerErrorHandler,则在出现故障后会调用错误处理程序。请参阅 Handling Exceptions以详细了解该错误处理程序及其用途。

Also starting with version 3.0.5, if a RabbitListenerErrorHandler is configured on a listener with an async return type (including Kotlin suspend functions), the error handler is invoked after a failure. See Handling Exceptions for more information about this error handler and its purpose.