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

时间:2019-12-26 14:13:37

标签: java rx-java2 vert.x

我正在尝试在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.

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

1 个答案:

答案 0 :(得分:1)

$ Attempting to call a function in a renderer window that has been closed or released.
Function provided here: Object.<anonymous> (C:\Users\LaLaLa\AppData\Local\atom\app-1.42.0\resources\app.asar\node_modules\github\lib\worker.js:79:22
Remote event names: destroyed, crashed

从问题中返回Single Single Completable的实例并不清楚您要做什么,但是如果您想在端口上侦听,则需要执行以下操作

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

如果要使用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()); } }); } }

rxClose