gpt4 book ai didi

java - 不兼容的类型 : Single cannot be converted to Completable

转载 作者:行者123 更新时间:2023-12-02 09:10:07 25 4
gpt4 key购买 nike

我正在尝试在 Vert.x 中的 verticle 中使用 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)

从问题中返回 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
}
);

关于java - 不兼容的类型 : Single<HttpServer> cannot be converted to Completable,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/59489616/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com