简体   繁体   中英

How can I close a TcpClient connection using Netty Reactor?

I am trying to close a TCP connection from netty reactor.ipc.netty.tcp.TcpClient, but i can't find a way to do it easily, there is no "disconnect", "stop" or "close" method. Can anyone help me? I am using reactor-netty.0.7.9.RELEASE library.

My class is structured as follows:

private TcpClient client;
private NettyOutbound out;

public Mono<? extends NettyContext> connect() {
    client = TcpClient.create(host, port);
    return client.newHandler(this::handleConnection)
            .log("newHandler");
}

private Publisher<Void> handleConnection(NettyInbound in, NettyOutbound out) {
    this.out = out;
    return out
            .neverComplete()    //keep connection alive
            .log("Never close");
}

public void disconnect() {
    client = TcpClient. //What can i put here to close the connection?
}

I appreciate your help, thank you very much in advance.

Yes, sorry I didn't post it before. All I had to do was to pass the connection from tcp client to a field variable, so I was able to dispose the connection using the dispose method. Here is the code.

private NettyOutbound out;
private Connection connection;

public ServerResponse connect() {
    return TcpClient.create()
        .host(tcpConfig.getHost())
        .port(tcpConfig.getPort())
        .handle(this::handleConnection)
        .connect()
        .flatMap(connection -> {
            this.connection = connection;
            log.info("Sending response to http client.");
            return ServerResponse.ok().build();
        });
}

private Publisher<Void> handleConnection(NettyInbound in, NettyOutbound out) {
    this.out = out;
    in.receive().asString(Charsets.ISO_8859_1)
        .log("In received")
        .subscribe(frameStr -> log.info(frameStr));
    return out
        .neverComplete()    //keep connection alive
        .log("Never close");
}

public void disconnect() {
    this.connection.dispose();
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM