【发布时间】:2017-11-19 01:40:17
【问题描述】:
我对使用 netty 的经验并不多,对ChannelPipeline 的文档有疑问。这就是它的确切含义:
static final EventExecutorGroup group = new DefaultEventExecutorGroup(16);
...
ChannelPipeline pipeline = ch.pipeline();
pipeline.addLast("decoder", new MyProtocolDecoder());
pipeline.addLast("encoder", new MyProtocolEncoder());
// Tell the pipeline to run MyBusinessLogicHandler's event handler methods
// in a different thread than an I/O thread so that the I/O thread is not blocked by
// a time-consuming task.
// If your business logic is fully asynchronous or finished very quickly, you don't
// need to specify a group.
pipeline.addLast(group, "handler", new MyBusinessLogicHandler());
我不太了解MyBusinessLogicHandler 类。它应该实现DuplexChannelHandler 还是不同的东西?例如,在我的特殊情况下,我有以下课程:
public class Packet{}
我想将一些字节序列解码为Packet,给MyBusinessLogicHandler,它会产生一些Packet。然后再次将其编码为字节流以发送回客户端。我目前是这样看的:
public class MyBusinessLogicHandler extends SimpleChannelInboundHandler<MyModel>{
public void channelRead0(ChannelHandlerContext ctx, Packet msg){
Packet rslt = null;
//Do some complicated business logic
ctx.write(rslt);
}
}
我不确定这是否是在 netty 中做事的常用方法。你能澄清一下吗?
【问题讨论】: