0
有沒有更加健全的方式來編程下面的超級簡單設置/獲取/關閉測試程序?請注意,我必須複製Redis關閉代碼並將其包含在設置錯誤路徑和獲取完整路徑中。vert.x鏈接異步邏輯?
import io.vertx.core.Vertx;
import io.vertx.redis.RedisClient;
import io.vertx.redis.RedisOptions;
public class RedisTest {
public static void main(String[] args) throws Exception {
Vertx vertx = Vertx.vertx();
String host = "localhost";
final RedisClient client = RedisClient.create(vertx,
new RedisOptions().setHost(host));
client.set("key", "value", r -> {
if (r.succeeded()) {
System.out.println("key stored");
client.get("key", s -> {
if (s.succeeded()) {
System.out.println("Retrieved value: " + s.result());
} else {
System.out.println("Connection or Operation Failed " + s.cause());
}
System.out.println("Closing Redis connection");
client.close(t -> {
if (s.succeeded()) {
System.out.println("Redis closed successfully.");
} else {
System.out.println("Connection or Operation Failed " + t.cause());
}
System.out.println("Closing vertx");
vertx.close();
});
});
} else {
System.out.println("Connection or Operation Failed " + r.cause());
System.out.println("Closing Redis connection");
client.close(s -> {
if (s.succeeded()) {
System.out.println("Redis closed successfully.");
} else {
System.out.println("Connection or Operation Failed " + s.cause());
}
System.out.println("Closing vertx");
vertx.close();
});
}
});
System.out.println("Exiting main");
}
}