【问题标题】:what's the meaning of server.onDispose().block()?server.onDispose().block() 是什么意思?
【发布时间】: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


【解决方案1】:

block:它只是阻塞应用程序线程,直到服务器关闭。如果没有阻塞,则应用程序将简单地终止而服务器不侦听。它的 javadoc 声明

Subscribe to this Mono and block indefinitely 

onDispose : 当底层 netty 通道关闭时,future 将收到其成功或失败的通知。它的 Mono 类型只是意味着没有 onNext,因为没有发射,它要么成功,要么失败。

/**
 * Returns an observing {@link Mono} terminating with success when shutdown
 * successfully or error.
 *
 * @return a {@link Mono} terminating with success if shutdown successfully or error
 */
default Mono<Void> onDispose() {
    return FutureMono.from(channel().closeFuture());
}

从客户端连接/通道的角度来看,3 和 4 是相同的行为。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2016-12-20
    • 2010-09-12
    • 1970-01-01
    • 2023-03-25
    • 1970-01-01
    • 2011-08-12
    • 2017-06-11
    相关资源
    最近更新 更多