Fixed ChannelHandlerContext example and added more explanation

This commit is contained in:
Trustin Lee 2009-04-28 14:35:23 +00:00
parent e8aba5ae5a
commit 48912c03ac

View File

@ -45,18 +45,35 @@ package org.jboss.netty.channel;
* times it is added to pipelines, regardless if it is added to the same * times it is added to pipelines, regardless if it is added to the same
* pipeline multiple times or added to different pipelines multiple times: * pipeline multiple times or added to different pipelines multiple times:
* <pre> * <pre>
* public class FibonacciHandler extends SimpleUpstreamChannelHandler { * public class FactorialHandler extends SimpleUpstreamChannelHandler {
*
* // This handler will receive a sequence of increasing integers starting
* // from 1.
* public void messageReceived(ChannelHandlerContext ctx, MessageEvent evt) { * public void messageReceived(ChannelHandlerContext ctx, MessageEvent evt) {
* Integer a = (Integer) ctx.getAttachment(); * Integer a = (Integer) ctx.getAttachment();
* Integer b = (Integer) evt.getMessage(); * Integer b = (Integer) evt.getMessage();
* *
* if (a == null) { * if (a == null) {
* a = 0; * a = 1;
* } * }
* *
* ctx.setAttachment(Integer.valueOf(a + b)); * ctx.setAttachment(Integer.valueOf(a * b));
* } * }
* } * }
*
* // Different context objects are given to "f1", "f2", "f3", and "f4" even if
* //.they refer to the same handler instance. Because the FactorialHandler
* // stores its state in a context object (as an attachment), the factorial is
* // calculated correctly 4 times.
* FactorialHandler fh = new FactorialHandler();
*
* ChannelPipeline p1 = Channels.pipeline();
* p1.addLast("f1", fh);
* p1.addLast("f2", fh);
*
* ChannelPipeline p2 = Channels.pipeline();
* p2.addLast("f3", fh);
* p2.addLast("f4", fh);
* </pre> * </pre>
* *
* @author The Netty Project (netty-dev@lists.jboss.org) * @author The Netty Project (netty-dev@lists.jboss.org)