netty的Pipeline流处理顺序——实例

 thomescai http://blog.csdn.net/thomescai(转载请保留)

概要:Pipeline流处理的一个实例,证明了Pipeline流的执行顺序。

ChannelPipeline原理图:



    Upstream接收请求。Downstream发送请求。

代码如下:

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 {

	@Override
	public 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 {
	@Override
	public void messageReceived(ChannelHandlerContext ctx, MessageEvent e)
			throws Exception {
		System.out
				.println("UpstreamHandlerA.messageReceived:" + e.getMessage());
		super.messageReceived(ctx, e);
	}

	@Override
	public void exceptionCaught(ChannelHandlerContext ctx, ExceptionEvent e) {
		System.out.println("UpstreamHandlerA.exceptionCaught:" + e.toString());
		e.getChannel().close();
	}
}

public class UpstreamHandlerB extends SimpleChannelUpstreamHandler {
	@Override
	public void messageReceived(ChannelHandlerContext ctx, MessageEvent e)
			throws Exception {
		System.out
				.println("UpstreamHandlerB.messageReceived:" + e.getMessage());
		super.messageReceived(ctx, e);
	}

	@Override
	public void exceptionCaught(ChannelHandlerContext ctx, ExceptionEvent e) {
		System.out.println("UpstreamHandlerB.exceptionCaught:" + e.toString());
		e.getChannel().close();
	}
}

public class UpstreamHandlerX extends SimpleChannelUpstreamHandler {
	@Override
	public void messageReceived(ChannelHandlerContext ctx, MessageEvent e)
			throws Exception {
		System.out.println("UpstreamHandlerX.messageReceived");
		e.getChannel().write(e.getMessage());// (2)
	}

	@Override
	public 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);
	}
}

运行结果:

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 顺序处理
Downstream: 4 ->3  逆序处理




参考资料:

http://www.slideshare.net/oleone/netty-lt《深入浅出Netty》

你可能感兴趣的:(后台)