package examples;
import io.vertx.core.Future;
import io.vertx.core.Vertx;
import java.util.UUID;
import java.util.concurrent.CompletionStage;
public class CompletionStageInteropExamples {
public void toCS(Vertx vertx) {
Future<String> future = vertx.createDnsClient().lookup("vertx.io");
future.toCompletionStage().whenComplete((ip, err) -> {
if (err != null) {
System.err.println("Could not resolve vertx.io");
err.printStackTrace();
} else {
System.out.println("vertx.io => " + ip);
}
});
}
private Future<String> storeInDb(String key, String value) {
return Future.succeededFuture("Yo");
}
public void fromCS(Vertx vertx, CompletionStage<String> completionStage) {
Future.fromCompletionStage(completionStage, vertx.getOrCreateContext())
.flatMap(str -> {
String key = UUID.randomUUID().toString();
return storeInDb(key, str);
})
.onSuccess(str -> {
System.out.println("We have a result: " + str);
})
.onFailure(err -> {
System.err.println("We have a problem");
err.printStackTrace();
});
}
}