如果您没有在vertx事件循环线程上执行此操作,则可以安全地使用
java.util.concurrent.CountDownLatch
以下是您的用例的一个快速示例:
public HealthCheckResponse call() {
Future<RedisConnection> redisConnectionFuture = Redis.createClient(Vertx.vertx(), redisHost).connect();
CountDownLatch countDownLatch = new CountDownLatch(1);
redisConnectionFuture.onComplete(x -> countDownLatch.countDown());
try {
countDownLatch.await();
} catch (InterruptedException e) {
throw new RuntimeException(e);
}
if (redisConnectionFuture.succeeded()) {
return HealthCheckResponse.up("RedisCustomHealthCheck");
} else {
return HealthCheckResponse.down("RedisCustomHealthCheck");
}
}