【问题标题】:How can I close a TcpClient connection using Netty Reactor?如何使用 Netty Reactor 关闭 TcpClient 连接?
【发布时间】:2019-03-15 15:28:49
【问题描述】:

我正在尝试关闭来自 netty reactor.ipc.netty.tcp.TcpClient 的 TCP 连接,但我找不到轻松的方法,没有“断开连接”、“停止”或“关闭”方法。谁能帮我?我正在使用 reactor-netty.0.7.9.RELEASE 库。

我的班级结构如下:

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?
}

感谢您的帮助,非常感谢您。

【问题讨论】:

    标签: java spring-boot reactor-netty


    【解决方案1】:

    是的,抱歉我之前没有发布过。 我所要做的就是将连接从 tcp 客户端传递到字段变量,因此我能够使用 dispose 方法处理连接。 这是代码。

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

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2017-06-23
      • 2019-04-02
      • 1970-01-01
      • 2020-04-13
      • 2020-07-05
      • 2021-03-19
      • 2017-01-17
      • 2010-11-26
      相关资源
      最近更新 更多