Skip to content
Advertisement

what’s the meaning of server.onDispose().block()?

There are few web pages about reactor-netty on the internet. And I don’t understand the meaning of some code below. These code below is just ABC of reactor-netty. But I really can not find more information on the internet. So I have to ask for help.

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();
    }
}

Please help me with the five places in the code above. Thank you.

Advertisement

Answer

<2>block: Its simply to block the application thread until the server is shutdown. If there was no block the application would simply terminate without the server listening. The javadoc for it states

Subscribe to this Mono and block indefinitely 

<1>onDispose : When the underlying netty channel shutsdown, the future will get notified of its success or failure. Its Mono type simply means there is no onNext as there are no emissions, its either successful or a failure.

/**
 * 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 and 4 is the same behavior from a client connection/channel perspective.

User contributions licensed under: CC BY-SA
7 People found this is helpful
Advertisement