问题:我写了MyDecoder
which extends ByteToMessageDecoder,单线程没问题,但是多线程时,报'the handler should be sharable'。查看ByteToMessageDecoder,发现其不能被@sharable。
解决:
在ChannelInitializer中,要new XxxHandler()。
pipeline.addLast(new TelnetServerHandler());
而不能写成员变量,如:
private static final TelnetServerHandler SERVER_HANDLER = new TelnetServerHandler();
因为,ByteToMessageDecoder是有状态的(成员变量),如下:
public abstract class ByteToMessageDecoder extends ChannelInboundHandlerAdapter { ByteBuf cumulation; private boolean singleDecode; private boolean decodeWasNull; private boolean first;