【发布时间】:2021-08-31 06:25:31
【问题描述】:
互联网上关于 reactor-netty 的网页很少。而且我不明白下面一些代码的含义。下面的这些代码只是 reactor-netty 的 ABC。但我真的无法在互联网上找到更多信息。所以我不得不寻求帮助。
import reactor.netty.DisposableServer;
import reactor.netty.tcp.TcpServer;
public class Application {
public static void main(String[] args) {
DisposableServer server =
TcpServer.create()
.host("localhost")
.port(8080)
.bindNow();
server.onDispose() // <1> what's the meaning?
.block(); // <2> what's the meaning when combined with server.onDispose()?
}
}
import reactor.netty.Connection;
import reactor.netty.tcp.TcpClient;
public class Application {
public static void main(String[] args) {
Connection connection =
TcpClient.create()
.host("example.com")
.port(80)
.connectNow();
connection.onDispose() // <3> what's the meaning?
.block(); // <4> what's the meaning when combined with connection.onDispose()? It has connectNow invoked already. Does it wait for connectNow function to return and stop?
}
}
import io.netty.handler.ssl.util.SelfSignedCertificate;
import reactor.netty.tcp.TcpServer;
import reactor.netty.tcp.TcpSslContextSpec;
/**
* A TCP server that sends back the received content.
*
* @author Violeta Georgieva
*/
public final class EchoServer {
static final boolean SECURE = System.getProperty("secure") != null;
static final int PORT = Integer.parseInt(System.getProperty("port", SECURE ? "8443" : "8080"));
static final boolean WIRETAP = System.getProperty("wiretap") != null;
public static void main(String[] args) throws Exception {
TcpServer server =
TcpServer.create()
.port(PORT)
.wiretap(WIRETAP)
.handle((in, out) -> out.send(in.receive().retain())); // <5> When is it executed? And how?
if (SECURE) {
SelfSignedCertificate ssc = new SelfSignedCertificate();
server = server.secure(
spec -> spec.sslContext(TcpSslContextSpec.forServer(ssc.certificate(), ssc.privateKey())));
}
server.bindNow()
.onDispose()
.block();
}
}
请帮我解决上面代码中的五个地方。谢谢。
【问题讨论】:
-
阻塞调用者线程直到服务器关闭。
标签: java tcp netty reactor reactor-netty