简体   繁体   English

处理消费者异常 Rabbit MQ Micronaut 2.2.1

[英]Handling Consumer Exceptions Rabbit MQ Micronaut 2.2.1

Trying to implement the Handling Consumer Exceptions in Micronaut 2.2.1 https://micronaut-projects.github.io/micronaut-rabbitmq/latest/guide/#consumerExceptions尝试在 Micronaut 2.2.1 https://micronaut-projects.github.io/micronaut-rabbitmq/latest/guide/#consumerExceptions中实现处理消费者异常

As per the documentation根据文档

If the consumer bean implements RabbitListenerExceptionHandler, then exceptions will be sent to the method implementation.如果消费者 bean 实现了 RabbitListenerExceptionHandler,那么异常将被发送到方法实现。

If the consumer bean does not implement RabbitListenerExceptionHandler, then the exceptions will be routed to the primary exception handler bean.如果消费者 bean 没有实现 RabbitListenerExceptionHandler,那么异常将被路由到主异常处理程序 bean。 To override the default exception handler, replace the DefaultRabbitListenerExceptionHandler with your own implementation that is designated as @Primary.要覆盖默认异常处理程序,请将 DefaultRabbitListenerExceptionHandler 替换为您自己的指定为 @Primary 的实现。

@Singleton
@Primary
public class RabbitListenerExceptionHandler implements io.micronaut.rabbitmq.exception.RabbitListenerExceptionHandler {
    private static final Logger LOG = LoggerFactory.getLogger(RabbitListenerExceptionHandler.class);
    @Override
    public void handle(RabbitListenerException exception) {
        if (LOG.isErrorEnabled()) {
            Optional<RabbitConsumerState> messageState = exception.getMessageState();
            if (messageState.isPresent()) {
                LOG.error("Error processing a message for RabbitMQ consumer [" + exception.getListener() + "]", exception);
            } else {
                LOG.error("RabbitMQ consumer [" + exception.getListener() + "] produced an error", exception);
            }
        }
    }
}

Exception at run time运行时异常

Caused by: io.micronaut.context.exceptions.NonUniqueBeanException: Multiple possible bean candidates found: [io.micronaut.rabbitmq.exception.DefaultRabbitListenerExceptionHandler, fete.bird.common.extension.RabbitListenerExceptionHandler]

How can I override the RabbitListenerExceptionHandler??如何覆盖 RabbitListenerExceptionHandler?

You have to replace the default implementation with @Replaces(DefaultRabbitListenerExceptionHandler.class)您必须用@Replaces(DefaultRabbitListenerExceptionHandler.class)替换默认实现

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM