Dropwizard的健康检查超时?

时间:2015-02-25 16:06:47

标签: java dropwizard jdbi

我有两个sql数据库连接,dropwizard会自动添加运行状况检查。但是当应用程序失去与其中一个连接的连接时,/ healthcheck端点会无限期地响应,我希望它在几秒后超时。

我已经设置了maxWaitForConnection设置,并且我还尝试了各种checkConnectionOn..设置,但没有任何帮助。

更新:如果服务器主动拒绝连接,则运行状况检查会正确失败,但如果不是这种情况,则会无限期挂起,例如网络问题。

无论出现什么问题,是否可以在指定的时间值执行sql运行状况检查超时?

1 个答案:

答案 0 :(得分:2)

如果JDBC超时设置不起作用,您可以随时将数据库检查包装在Future中并限制其运行时间:

protected Result check() throws Exception {
  ExecutorService executor = Executors.newSingleThreadExecutor();
  Future<Void> future = executor.submit(new DbConnectionChecker());

  try {
    future.get(SECONDS_THRESHOLD, TimeUnit.SECONDS);
  } catch (TimeoutException e) {
    return Result.unhealthy("DB timed out");
  }
  executor.shutdownNow();
  return Result.healthy();
}

DbConnectionChecker类似于:

static class DbConnectionChecker implements Callable<Void> {
  public Void call() throws Exception {
    // Check your DB connection as you normally would
  }
}

很高兴弄清楚为什么JDBC连接设置没有按预期工作。

相关问题