java - 不兼容的类型:单无法转换为 Completable
问题描述
我正在尝试RxJava2
在 Vert.x 中使用 Verticle:
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)
从问题中返回 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
}
);
推荐阅读
- symfony - Symfony - 使用 LexikJWTAuthenticationBundle 设置 TokenController
- asp.net-core - Google 授权的重定向 URI 格式
- java - 为什么 Google Cloud API 尝试以最终用户身份连接?
- git - Windows VS Code 能否在连接到 git 存储库的 Ubuntu 主机上通过 ssh 运行 git 命令
- xml - 如何将 .xml 读入内存并写出相同的结果
- c++ - 如何跨平台将二进制资源构建到程序中?
- git - 使用 Shippable 将应用程序推送到 heroku 时身份验证失败
- python - 用 1 替换 numpy 数组中的多个元素
- batch-file - 使用今天的日期和时间创建一个时间戳文件夹并将一些文件夹复制到其中
- c# - 如何检查设备上是否安装了 Microsoft Visual C++ 2015 Redistributable