gpt4 book ai didi

java - Dropwizard 的健康检查超时?

转载 作者:行者123 更新时间:2023-11-30 08:56:16 25 4
gpt4 key购买 nike

我有两个 sql 数据库连接,dropwizard 会自动为其添加健康检查。但是当应用程序失去与其中之一的连接时,/healthcheck 端点需要无限长的时间来响应,我希望它在几秒钟后超时。

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

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

是否可以让 sql 健康检查在指定的时间值超时,无论问题是什么?

最佳答案

如果 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 连接设置没有按预期工作。

关于java - Dropwizard 的健康检查超时?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28723806/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com