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》

  • 3
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值