Thomescai http://blog.csdn.net/thomescai (reprinted please keep)
Summary: an instance of pipeline stream processing proves the execution sequence of the pipeline stream.
Channelpipeline schematic:
Upstream receives requests. Downstream sends a request.
The Code is as follows:
public class ServerTest {public static void main(String args[]) {ServerBootstrap bootsrap = new ServerBootstrap(new NioServerSocketChannelFactory(Executors.newCachedThreadPool(), Executors.newCachedThreadPool()));bootsrap.setPipelineFactory(new PipelineFactoryTest());bootsrap.bind(new InetSocketAddress(8888));}}
public class PipelineFactoryTest implements ChannelPipelineFactory {@Overridepublic ChannelPipeline getPipeline() throws Exception {ChannelPipeline pipeline = Channels.pipeline();pipeline.addLast("1", new UpstreamHandlerA());pipeline.addLast("2", new UpstreamHandlerB());pipeline.addLast("3", new DownstreamHandlerA());pipeline.addLast("4", new DownstreamHandlerB());pipeline.addLast("5", new UpstreamHandlerX());return pipeline;}}
@ChannelPipelineCoverage("all")public class UpstreamHandlerA extends SimpleChannelUpstreamHandler {@Overridepublic void messageReceived(ChannelHandlerContext ctx, MessageEvent e)throws Exception {System.out.println("UpstreamHandlerA.messageReceived:" + e.getMessage());super.messageReceived(ctx, e);}@Overridepublic void exceptionCaught(ChannelHandlerContext ctx, ExceptionEvent e) {System.out.println("UpstreamHandlerA.exceptionCaught:" + e.toString());e.getChannel().close();}}
public class UpstreamHandlerB extends SimpleChannelUpstreamHandler {@Overridepublic void messageReceived(ChannelHandlerContext ctx, MessageEvent e)throws Exception {System.out.println("UpstreamHandlerB.messageReceived:" + e.getMessage());super.messageReceived(ctx, e);}@Overridepublic void exceptionCaught(ChannelHandlerContext ctx, ExceptionEvent e) {System.out.println("UpstreamHandlerB.exceptionCaught:" + e.toString());e.getChannel().close();}}
public class UpstreamHandlerX extends SimpleChannelUpstreamHandler {@Overridepublic void messageReceived(ChannelHandlerContext ctx, MessageEvent e)throws Exception {System.out.println("UpstreamHandlerX.messageReceived");e.getChannel().write(e.getMessage());// (2)}@Overridepublic void exceptionCaught(ChannelHandlerContext ctx, ExceptionEvent e) {System.out.println("UpstreamHandlerX.exceptionCaught");e.getChannel().close();}}
public class DownstreamHandlerA extends SimpleChannelDownstreamHandler {public void handleDownstream(ChannelHandlerContext ctx, ChannelEvent e)throws Exception {System.out.println("DownstreamHandlerA.handleDownstream");super.handleDownstream(ctx, e);}}
public class DownstreamHandlerB extends SimpleChannelDownstreamHandler {public void handleDownstream(ChannelHandlerContext ctx, ChannelEvent e)throws Exception {System.out.println("DownstreamHandlerB.handleDownstream");super.handleDownstream(ctx, e);}}
Running result:
Upstreamhandlera. messagereceived: bigendianheapchannelbuffer (ridx = 0, widx = 386, Cap = 386)
Upstreamhandlerb. messagereceived: bigendianheapchannelbuffer (ridx = 0, widx = 386, Cap = 386)
Upstreamhandlerx. messagereceived
Downstreamhandlerb. handledownstream
Downstreamhandlera. handledownstream
Upstream: 1-> 2-> 5 sequential processing
Downstream: 4-> 3 reverse Processing
References:
Http://www.slideapps.net/oleone/netty-lt《