vert-x3 / vertx-mongo-client

Mongo Client for Eclipse Vert.x
http://vertx.io
Apache License 2.0
58 stars 98 forks source link

The synchronization result cannot be obtained through the get method in CompletableFuture #303

Closed maoyutofu closed 1 year ago

maoyutofu commented 1 year ago

Questions

The synchronization result cannot be obtained through the get method in CompletableFuture

Version

4.2.5

Context

final CompletableFuture<List<JsonObject>> queryFuture = new CompletableFuture<>();
client.findWithOptions("table_name", new JsonObject(), new FindOptions().setLimit(10).setSkip(0)).onComplete(ar -> {
    if (ar.succeeded()) {
        queryFuture.complete(ar.result());
    } else {
        queryFuture.complete(null);
    }
});

final CompletableFuture<List<JsonObject>> resultFuture = queryFuture.thenApply(dataList -> dataList);
try {
    System.out.println(resultFuture.get());
} catch (InterruptedException | ExecutionException e) {
    e.printStackTrace();
}
tsegismont commented 1 year ago

I assume the code is invoked on the event loop, that gets blocked by resultFuture.get().

You need to rework this part of the code to avoid blocking.