我正面临一个非常奇怪的问题。
我正在使用 Vert.x 和处理程序,我正在使用 Vert.x 调用 REST API HttpClientRequest。现在我有一个CompletableFuture我正在HttpClientRequest. 后来,我使用CompletableFuture.get(). 但是无论何时get()调用方法,主线程都会被阻塞(正如预期的那样),但它会永远被阻塞。我没有看到回调发生在我的响应处理程序上并且它永远被卡住了。
这是代码:
import io.vertx.core.http.HttpClientRequest;
import io.vertx.core.http.HttpMethod;
import io.vertx.core.json.Json;
import io.vertx.core.json.JsonObject;
import java.util.concurrent.CompletableFuture;
import io.vertx.core.http.HttpClient;
CompletableFuture<JsonObject> comp = new CompletableFuture<JsonObject>();
HttpClient httpClient = new HttpClient(); //This object initialized and set the endpoit, port and domain name.
HttpClientRequest request = httpClient.request(HttpMethod.POST, requestURI, response -> {
response.bodyHandler(body -> {
//do some process
comp.complete(new JsonObject(body);
});
}).exceptionHandler(e -> {
//log the error
comp.completeExceptionally(e);
});
request.end();
//after some process
comp.get(); // here main thread is stuck forever.
我的 API 给出了 200 个响应,我在 Wireshark 中看到了,如果我执行comp.thenAccept()回调,它会给出我的结果。
为什么会发生这种情况,解决方案是什么?
湖上湖
拉丁的传说
相关分类