首页 > 解决方案 > 获取最后一个 ChannelInboundHandler 的结果

问题描述

我正在编写基于 Netty 的客户端应用程序。我想提取管道中最后一个处理程序的结果。

管道是用

protected void initChannel(SocketChannel ch) throws Exception {
    ChannelPipeline p = ch.pipeline();
    if (sslCtx != null)
        p.addLast("ssl", sslCtx.newHandler(ch.alloc()));

    p.addLast("client", new HttpClientCodec());
    p.addLast("decompressor", new HttpContentDecompressor());
    p.addLast("aggregator", new HttpObjectAggregator(HTTP_OBJECT_MAX_SIZE));
    p.addLast("my-handler", new MyApiResponseChannelHandler());
}

最终实现在哪里

public class MyApiResponseChannelHandler extends MessageToMessageDecoder<FullHttpResponse> {

    @Override
    protected void decode(ChannelHandlerContext ctx, FullHttpResponse msg, List<Object> out) throws Exception {
        out.add(msg.status().code() + " - " + msg.status().reasonPhrase());
    }
}

我想了解添加到out列表中的字符串。

发送请求时,我添加了如下代码

ch.writeAndFlush(request).addListener( f -> logger.info("Got value {}", f.get()));

并且

ch.closeFuture().addListener( f -> logger.info("Got value {}", f.get()));

他们都给了我一个null价值。

我怎样才能得到最后的结果ChannelInboundHandler?我会对未来感到满意,但我似乎无法找到合适的未来。

标签: javanetty

解决方案


推荐阅读