TimerServer:
ch.pipeline().addLast(new TimeEncoder());
ch.pipeline().addLast(new TimeServerHandler());
TimeServerHandler
public class TimeServerHandler extends ChannelInboundHandlerAdapter {
@Override
public void channelActive(ChannelHandlerContext ctx) throws Exception {//(1)
ChannelFuture f = ctx.writeAndFlush(new UnixTime());
f.addListener(ChannelFutureListener.CLOSE);//因为是write,传递给的不是ByteToMessageDecoder
}
@Override
public void exceptionCaught(ChannelHandlerContext ctx, Throwable cause) throws Exception {
cause.printStackTrace();
ctx.close();
}
}
TimeEncoder
public class TimeEncoder extends ChannelOutboundHandlerAdapter {
@Override
public void write(ChannelHandlerContext ctx, Object msg, ChannelPromise promise) throws Exception {
UnixTime m = (UnixTime)msg;
ByteBuf encoded = ctx.alloc().buffer(4);
encoded.writeInt((int)m.value());
ctx.write(encoded,promise);
}
}
注意:
在TimeServerHandler中,ctx.writeAndFlush(new UnixTime()),已经writeAndFlush了,所以执行过程:TimeServerHandler(write) ---->TimeEncoder(write)--->ChannelOutboundHandlerAdapter(flush)。
如果在TimeServerHandler中只写write不写flush,那么不会自动执行ChannelOutboundHandlerAdapter中的flush方法,从而无法发送到网络。
如果在TimeServerHandler中只写write不写flush,但是在TimeEncoder中执行flush方法,不执行ChannelOutboundHandlerAdapter中的flush方法,但能发送到网络。
总结:在ChannelPipeline中传输的handler,只要在任意一个handler钟写flush,都会发送到网络。