Pipeline stream processing sequence of netty-instance

Source: Internet
Author: User

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《

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.