我正在使用vert.x,并试图列出我的DynamoDB表。以下是我如何构建DynamoDB客户端。
private static DynamoDbAsyncClient buildDynamoDBAsyncClient(final Vertx vertx) {
return VertxSdkClient.withVertx(DynamoDbAsyncClient.builder(), vertx.getOrCreateContext())
.build();
}
这是我提出的请求
CompletableFuture<ListTablesResponse> response = client.listTables(ListTablesRequest.builder()
.build());
// Map the response to another CompletableFuture containing just the table names
CompletableFuture<List<String>> tableNames = response.thenApply(ListTablesResponse::tableNames);
// When future is complete (either successfully or in error) handle the response
tableNames.whenComplete((tables, err) -> {
if (tables != null) {
tables.forEach(System.out::println);
} else {
// Handle error
err.printStackTrace();
}
client.close();
});
tableNames.join();
我收到线程被阻塞的警告,然后请求超时。我做错了什么?提前谢谢。
正如Tim在评论中所说,tablesName.join()
阻塞了线程,您应该让CompletableFuture
处理异步。
试试这个:
private Future<ListTablesResponse> listTables() {
Promise<ListTablesResponse> promise = Promise.promise();
CompletableFuture<ListTablesResponse> response = client.listTables();
response.handleAsync((result, error) -> {
if (error == null) {
promise.complete(result);
} else {
promise.fail(error);
}
return null;
});
return promise.future();
}
然后在您的vertx请求处理程序中:
this.listTables()
.onSuccess(listTablesResponse -> {...})
.onFailure(throwable -> {...});