带有CompletableFuture的Vertx HTTPClient阻止回调线程

时间:2018-10-05 17:00:15

标签: java java-8 httpclient vert.x completable-future

我面临一个非常奇怪的问题。

我正在使用Vert.x,并且从处理程序中,我使用Vert.x的HttpClientRequest调用REST API。现在,我在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(),则执行回调并给出结果。

为什么会这样?解决方法是什么?

注意:我知道不建议使用Completable.get()方法,但是在我的用例中,我必须使用它。

以下是示例代码,给了我问题:

package io.vertx.starter;

import io.vertx.core.AbstractVerticle;
import io.vertx.core.Future;
import io.vertx.core.http.*;
import io.vertx.ext.web.Router;
import io.vertx.ext.web.RoutingContext;

import java.util.concurrent.CompletableFuture;

public class SampleVerticle extends AbstractVerticle {

  public void start ( Future startFuture ) throws Exception {
    Future<Void> future = Future.future ();
    HttpServer server = vertx.createHttpServer ();

    Router router = Router.router (vertx);
    router.get ("/sample").handler (this::sampeHandler);
    router.get ("/testcompletableblocking").handler (this::testCompBlocking);
    router.get ("/testcompletablenonblocking").handler (this::testCompNonBlocking);

    server.requestHandler (router::accept) // <5>
      .listen (8080, ar -> { // <6>
        if (ar.succeeded ()) {
          System.out.println ("Server started");
          future.complete ();
        } else {
          System.out.println ("Server is not started");
          future.fail (ar.cause ());
        }
      });
  }

  private void sampeHandler ( RoutingContext context ) {
    try {
      Thread.sleep (1000);
    } catch (Exception e) {
    }
    String response = "Hello...";
    context.response ().setStatusCode (200).putHeader ("content-type", "text/html").end (response);
  }

  private void testCompBlocking ( RoutingContext context ) {

    System.out.println ("Calling testCompBlocking....");
    HttpClientOptions clientOptions = new HttpClientOptions ().setDefaultHost ("localhost").setDefaultPort (8080).setSsl (false).setKeepAlive (true);
    HttpClient client = vertx.createHttpClient (clientOptions);

    String requestURI = "/sample";
    CompletableFuture<String> comp = new CompletableFuture<> ();
    HttpClientRequest request = client.request (HttpMethod.GET, requestURI, response -> {

      response.bodyHandler (body -> {
        String kmsResponse = new String (body.getBytes ());
        System.out.println ("kmsResponse-" + kmsResponse);
        comp.complete (kmsResponse);
      });
    }).exceptionHandler (e -> {
      e.printStackTrace ();
      comp.completeExceptionally (e);
    });
    request.end ();

    String result = "Not Success";
    try {
      result = comp.get ();
    } catch (Exception e) {
      System.out.println ("Exception in getting from Completable..." + e.getMessage ());
      e.printStackTrace ();
    }
    context.response ().setStatusCode (200);
    context.response ().putHeader ("content-type", "text/html");
    context.response ().end (result);
    System.out.println ("end testCompBlocking....");
  }

  private void testCompNonBlocking ( RoutingContext context ) {

    System.out.println ("Calling testCompNonBlocking....");
    HttpClientOptions clientOptions = new HttpClientOptions ().setDefaultHost ("localhost").setDefaultPort (8080).setKeepAlive (false);
    HttpClient client = vertx.createHttpClient (clientOptions);

    String requestURI = "/sample";
    CompletableFuture<String> comp = new CompletableFuture<> ();
    HttpClientRequest request = client.request (HttpMethod.GET, requestURI, response -> {

      response.bodyHandler (body -> {
        String kmsResponse = new String (body.getBytes ());
        System.out.println ("kmsResponse-" + kmsResponse);
        comp.complete (kmsResponse);
      });
    }).exceptionHandler (e -> {
      e.printStackTrace ();
      comp.completeExceptionally (e);
    });
    request.end ();

    String result = "Not Blocking, please see result at Console";
    try {
      comp.thenAccept (apiResult -> System.out.println ("apiResult from CompletableFuture - " + apiResult));
    } catch (Exception e) {
      System.out.println ("Exception in getting from Completable..." + e.getMessage ());
      e.printStackTrace ();
    }
    context.response ().setStatusCode (200);
    context.response ().putHeader ("content-type", "text/html");
    context.response ().end (result);
    System.out.println ("end testCompNonBlocking....");
  }

}

调用localhost:8080/testcompletableblocking,不发送响应,并且当前线程永远被阻塞。

2 个答案:

答案 0 :(得分:2)

您的实现存在的问题是它违反了The Golden Rule - Don’t Block the Event Loop。您不应在事件循环上调用CompletableFuture.get()之类的阻止操作。同样,sampleHandler()也不应在事件循环上调用Thread.sleep(),但这是一个较小的问题。

结果是您的事件循环现在被阻止了……因此您的/sample请求无法再处理了。而且由于未处理该请求,因此您CompletableFuture仍未完成……陷入僵局。

有两个可能的解决方案:

  1. 按设计使用CompletableFuture,而不是get(),而是依靠链式调用,尽管它不强制Vert.x的线程模型。例如:

    comp.whenComplete((result, e) -> {
        System.out.println("Got sample response");
        if (e != null) {
            context.response().setStatusCode(500)
                    .end(e.getMessage());
        } else {
            context.response().setStatusCode(200)
                    .putHeader("content-type", "text/html")
                    .end(result);
        }
        System.out.println("end testCompBlocking....");
    });
    
  2. Use Vert.x facilities用于运行阻止代码。 CompletableFuture不需要这样做,但是其他API可能需要它。例如:

    context.vertx().<String>executeBlocking(future -> {
                String result = "Not Success";
                try {
                    result = comp.get();
                } catch (Exception e) {
                    System.out.println("Exception in getting from Completable..." + e.getMessage());
                    e.printStackTrace();
                }
                future.complete(result);
            },
            false,
            result -> {
                context.response().setStatusCode(200);
                context.response().putHeader("content-type", "text/html");
                context.response().end(result.result());
                System.out.println("end testCompBlocking....");
            });
    

答案 1 :(得分:0)

get()阻塞主线程,直到将来完成为止,但是HttpClientRequest在主线程上执行,因此情况导致死锁。

相反,thenAccept()是非阻塞的,仅创建在将来完成时执行的回调。


根据您提供的代码,您的用例尚不清楚;您是否有理由分别使用HttpClientCompletableFuture而不是WebClientFuture

如果需要使用CompletableFuture,则应查看this项目,以获取与Vert.x更兼容的实现。