NettyのPipelineストリームの処理順序-例
thomescai http://blog.csdn.net/thomescai(転載は保留してください)
概要:Pipelineストリーム処理の一例で、Pipelineストリームの実行順序を証明します.
ChannelPipeline原理図:
Upstreamはリクエストを受信します.Downstreamはリクエストを送信します.
コードは次のとおりです.
実行結果:
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』
概要: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』