出站消息的 netty 管道顺序
netty pipeline order for outbound message
我正在使用 Netty v4.1.9final 并尝试从客户端向服务器发送消息。我尝试在客户端使用处理程序设置通道客户端初始化程序,如下所示:
final Bootstrap bootstrap = BootstrapGenerator.generate();
bootstrap.handler(new XmlClientInitializer());
XMLClientInitializer
public class XmlClientInitializer extends ChannelInitializer<SocketChannel> {
@Override
public void initChannel(SocketChannel ch) throws Exception {
final ChannelPipeline pipeline = ch.pipeline();
pipeline.addLast("fileEncoder", new FileEncoder());
pipeline.addLast("handler", new XmlSenderHandler());
}
}
文件编码器
public class FileEncoder extends MessageToByteEncoder<String> {
XmlSenderHandler
public class XmlSenderHandler extends ChannelOutboundHandlerAdapter {
private static final Logger log = LogManager.getLogger(XmlSenderHandler.class.getName());
private static ChannelHandlerContext ctx;
@Override
public void write(ChannelHandlerContext ctx, Object msg, ChannelPromise promise) {
log.info("attempting to write messages to server {}", msg.toString());
ctx.write(msg, promise);
}
@Override
@SuppressWarnings("FutureReturnValueIgnored")
public void exceptionCaught(ChannelHandlerContext ctx, Throwable cause) throws Exception {
super.exceptionCaught(ctx, cause);
}
}
即使 FileEncoder 在管道中首先定义,它也会在 xml 处理程序之后被调用(这不是我想要的)。这是因为 FileEncoder 正在扩展 MessageToByteEncoder 还是我配置的通道不正确?
否,管道配置正确。重点是入站事件是从第一个处理程序到最后一个处理程序,而出站事件是从最后一个处理程序到第一个处理程序。
ChannelPipeline doc
我正在使用 Netty v4.1.9final 并尝试从客户端向服务器发送消息。我尝试在客户端使用处理程序设置通道客户端初始化程序,如下所示:
final Bootstrap bootstrap = BootstrapGenerator.generate();
bootstrap.handler(new XmlClientInitializer());
XMLClientInitializer
public class XmlClientInitializer extends ChannelInitializer<SocketChannel> {
@Override
public void initChannel(SocketChannel ch) throws Exception {
final ChannelPipeline pipeline = ch.pipeline();
pipeline.addLast("fileEncoder", new FileEncoder());
pipeline.addLast("handler", new XmlSenderHandler());
}
}
文件编码器
public class FileEncoder extends MessageToByteEncoder<String> {
XmlSenderHandler
public class XmlSenderHandler extends ChannelOutboundHandlerAdapter {
private static final Logger log = LogManager.getLogger(XmlSenderHandler.class.getName());
private static ChannelHandlerContext ctx;
@Override
public void write(ChannelHandlerContext ctx, Object msg, ChannelPromise promise) {
log.info("attempting to write messages to server {}", msg.toString());
ctx.write(msg, promise);
}
@Override
@SuppressWarnings("FutureReturnValueIgnored")
public void exceptionCaught(ChannelHandlerContext ctx, Throwable cause) throws Exception {
super.exceptionCaught(ctx, cause);
}
}
即使 FileEncoder 在管道中首先定义,它也会在 xml 处理程序之后被调用(这不是我想要的)。这是因为 FileEncoder 正在扩展 MessageToByteEncoder 还是我配置的通道不正确?
否,管道配置正确。重点是入站事件是从第一个处理程序到最后一个处理程序,而出站事件是从最后一个处理程序到第一个处理程序。 ChannelPipeline doc