4

Netty 4 でカスタム アウトバウンド メッセージ ハンドラーを試していますが、うまく動作しないようです。ハンドラーはステートメントをログに記録するだけで、チャネル パイプラインの下部に向かって追加されます。私の理解では、書き込み操作が発行されると、これらのハンドラーは下から順番に呼び出されます。カスタム ハンドラーのアイデアは、他の送信メッセージ ハンドラーより前に実行されるというものでした。

残念ながら、このログ ハンドラーをパイプラインに追加すると、ログ ステートメントが表示されますが、Netty はすぐに接続を閉じているようです。これが私のチャネル初期化子とアウトバウンド ハンドラー コードです。

HttpOutboundHandler.java

public class HttpOutboundHandler extends ChannelOutboundMessageHandlerAdapter<DefaultFullHttpResponse> {
    private static final Logger logger = LoggerFactory.getLogger(HttpOutboundHandler.class);

    @Override
    public void flush(ChannelHandlerContext context, DefaultFullHttpResponse response)
            throws Exception {
        logger.debug("Executing outbound handler.");
    }
}

HttpChannelInitializer.java

@Override
protected void initChannel(SocketChannel socketChannel) throws Exception {
    pipeline.addLast("encoder", new HttpResponseEncoder());
    pipeline.addLast("decoder", new HttpRequestDecoder());
    pipeline.addLast("aggregator", new HttpObjectAggregator(1048576);
    pipeline.addLast("compressor", new HttpContentCompressor(gzipLevel));
    pipeline.addLast("outboundHandler", outboundHandler);
    pipeline.addLast(eventExecutorGroup, "inboundHandler", inboundHandler);
}

最後に、これがロガー出力です。

[DEBUG] (Slf4JLogger:71) - [id: 0xbddf00cf, /0:0:0:0:0:0:0:1:57402 => /0:0:0:0:0:0:0:1:8080] ACTIVE
[DEBUG] (HttpOutboundHandler:19) - Executing outbound handler.
[DEBUG] (Slf4JLogger:71) - [id: 0x942993c1, /0:0:0:0:0:0:0:1:57403 :> /0:0:0:0:0:0:0:1:8080] INACTIVE
4

1 に答える 1

3

他の誰かがこれを見つけた場合に備えて、自分の質問に答えます。

メッセージを次の送信メッセージ バッファーに追加する必要があることが判明しました (これにより、メッセージがチェーン内の次のハンドラーに渡されると考えられます)。また、メッセージを保持する必要がありました。更新されたコードは次のようになります...

public class HttpOutboundHandler extends ChannelOutboundMessageHandlerAdapter<DefaultFullHttpResponse> {
    private static final Logger logger = LoggerFactory.getLogger(HttpOutboundHandler.class);

    @Override
    public void flush(ChannelHandlerContext context, DefaultFullHttpResponse response)
            throws Exception {
        logger.debug("Executing outbound handler.");
        ChannelHandlerUtil.addToNextOutboundBuffer(context, response.retain());
    }
}
于 2013-05-10T16:26:24.640 に答える