【发布时间】:2019-06-21 07:14:27
【问题描述】:
当我在数据库关闭时调用heathIndicator.health() 方法时,它会抛出NullPointerException。堆栈跟踪显示 AbstractHealthIndicator 中的 logger 对象为空。我也通过调试验证了它。当数据库启动时,代码可以正常工作,因为没有调用记录器。我不太明白为什么会发生这种情况。
这是Spring的AbstractHealthIndicator
中health方法的代码
@Override
public final Health health() {
Health.Builder builder = new Health.Builder();
try {
doHealthCheck(builder);
}
catch (Exception ex) {
// null pointer exception happens here, logger is null.
if (this.logger.isWarnEnabled()) {
String message = this.healthCheckFailedMessage.apply(ex);
this.logger.warn(StringUtils.hasText(message) ? message : DEFAULT_MESSAGE,
ex);
}
builder.down(ex);
}
return builder.build();
}
【问题讨论】:
-
你在扩展
AbstractHealthIndicator吗?你在用spring-boot-2吗? -
你是如何初始化记录器变量的?
-
这不是我的代码,我只是在使用它。是
AbstractHealthIndicator的health方法的源代码。 -
哦,好吧,明白了,为什么要担心spring的内部代码?,如果需要实现健康检查,请扩展它并覆盖方法。
-
我的意思是它正在做我想要的,除了那部分。这甚至不是逻辑问题, logger 出于某种原因只是 null 。顺便说一句,我知道我可以自己实现,但我只是好奇为什么会发生这种情况。
标签: java spring spring-boot spring-boot-actuator health-monitoring