java - 处理消费者异常 Rabbit MQ Micronaut 2.2.1
问题描述
尝试在 Micronaut 2.2.1 中实现处理消费者异常https://micronaut-projects.github.io/micronaut-rabbitmq/latest/guide/#consumerExceptions
根据文档
如果消费者 bean 实现了 RabbitListenerExceptionHandler,那么异常将被发送到方法实现。
如果消费者 bean 没有实现 RabbitListenerExceptionHandler,那么异常将被路由到主异常处理程序 bean。要覆盖默认异常处理程序,请将 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);
}
}
}
}
运行时异常
Caused by: io.micronaut.context.exceptions.NonUniqueBeanException: Multiple possible bean candidates found: [io.micronaut.rabbitmq.exception.DefaultRabbitListenerExceptionHandler, fete.bird.common.extension.RabbitListenerExceptionHandler]
如何覆盖 RabbitListenerExceptionHandler ?
解决方案
您必须将默认实现替换为@Replaces(DefaultRabbitListenerExceptionHandler.class)
推荐阅读
- python - 逐行打印 SSH 输出,而不是一次打印所有内容
- java - 物体运动模糊
- javascript - 使用 dataURL 制作可下载的文件
- jquery - Django ajax 搜索以 404 状态响应
- java - 到达 onmessage websocket 事件 HtmlUnit
- angular - 有没有办法修复 Angular CLI 生成的文件以停止在 WebStorm 中出现红色
- python - scrapy 不会跟随页面中的所有 URL
- r - R如何组合函数的参数
- android - 为firebase函数获取java http请求并快速放入
- openrefine - 如何从一系列页面下载数据?