当独占消费者失败时要处理哪个 AmqpEvent 或 AmqpException

Which AmqpEvent or AmqpException to handle when an exclusive consumer fails

我有两个相同应用程序的实例,运行 在不同的虚拟机中。我想授予其中一个消费者对队列的独占访问权限,同时使另一个消费者使用的本地缓存无效。

现在,我发现我需要处理 ListenerContainerConsumerFailedEvent,但我猜测为此事件实施 ApplicationListener 并不能确保我收到此事件,因为独家消费者例外。我可能想检查事件的Throwable,或者进一步检查事件。

AmqpException 的哪个子类或我应该执行哪些进一步检查以确保接收到由于独占消费者访问而导致的异常?

监听器容器实现的逻辑是这样的:

if (e.getCause() instanceof ShutdownSignalException
            && e.getCause().getMessage().contains("in exclusive use")) {
        getExclusiveConsumerExceptionLogger().log(logger,
                "Exclusive consumer failure", e.getCause());
        publishConsumerFailedEvent("Consumer raised exception, attempting restart", false, e);
    }

因此,我们确实引发了一个 ListenerContainerConsumerFailedEvent 事件,您可以像我们在框架中那样跟踪原因消息,但另一方面,您可以注入自己的 ConditionalExceptionLogger:

/**
 * Set a {@link ConditionalExceptionLogger} for logging exclusive consumer failures. The
 * default is to log such failures at WARN level.
 * @param exclusiveConsumerExceptionLogger the conditional exception logger.
 * @since 1.5
 */
public void setExclusiveConsumerExceptionLogger(ConditionalExceptionLogger exclusiveConsumerExceptionLogger) {

并在那边捕捉到如此独特的情况。

您也可以考虑在您的代码中使用 RabbitUtils.isExclusiveUseChannelClose(cause)

/**
 * Return true if the {@link ShutdownSignalException} reason is AMQP.Channel.Close
 * and the operation that failed was basicConsumer and the failure text contains
 * "exclusive".
 * @param sig the exception.
 * @return true if the declaration failed because of an exclusive queue.
 */
public static boolean isExclusiveUseChannelClose(ShutdownSignalException sig) {