ChannelOutboundHandler本应该只关注outbound事件,但是它却声明了一个read方法:
/**
* Intercepts {@link ChannelHandlerContext#read()}.
*/
void read(ChannelHandlerContext ctx) throws Exception;
有人在stackoverflow问了这个问题,trustin给出了回答:
Inbound handlers are supposed to handle inbound events. Events are triggered by external stimuli such as data received from a socket. Outbound handlers are supposed to intercept the operations issued by your application.
所以ChannelOutboundHandler上的read方法,如其注释所述,是为了拦截ChannelHandlerContext.read()操作。也就是说,ChannelOutboundHandler可以通过read()方法在必要的时候阻止向inbound读取更多数据的操作。这个设计在处理协议的握手时非常有用。