不兼容的类型:Single<HttpServer> 无法转换为 Completable

incompatible types: Single<HttpServer> cannot be converted to Completable

我正在尝试在 Vert.x 中使用 RxJava2 垂直:

import io.reactivex.Completable;
import io.vertx.core.Promise;

public class MainVerticle extends io.vertx.reactivex.core.AbstractVerticle {


  @Override
  public Completable rxStart() {
    return vertx.createHttpServer().requestHandler(req -> {
      req.response()
        .putHeader("content-type", "text/plain")
        .end("Hello from Vert.x!");
    })
      .rxListen(8080);

  }
}

编译器抱怨:

 error: incompatible types: Single<HttpServer> cannot be converted to Completable
      .rxListen(8080);
               ^
1 error

FAILURE: Build failed with an exception.

* What went wrong:
Execution failed for task ':compileJava'.
> Compilation failed; see the compiler error output for details.

我不知道,我应该调用哪个方法。

Single<HttpServer> rxListen(int port,String host)

returns 问题中的 Single not Completable 实例不清楚您要做什么,但如果您想在端口上收听,则需要做这样的事情

public class MyVerticle extends AbstractVerticle {

 private HttpServer server;

 public void start(Future<Void> startFuture) {
   server = vertx.createHttpServer().requestHandler(req -> {
     req.response()
       .putHeader("content-type", "text/plain")
       .end("Hello from Vert.x!");
     });

   // Now bind the server:
   server.listen(8080, res -> {
     if (res.succeeded()) {
       startFuture.complete();
     } else {
       startFuture.fail(res.cause());
     }
   });
 }
}

如果您想使用 Completable,您需要订阅服务器并调用方法 rxClose

 Completable single = server.rxClose();

    // Subscribe to bind the server
    single.
      subscribe(
        () -> {
          // Server is closed
        },
        failure -> {
          // Server closed but encoutered issue
        }
      );