xample that sends a ping message when there is no outbound traffic // for 30 seconds. The connection is closed when there is no inbound traffic // for 60 seconds. public class MyPipelineFactory implements {@link ChannelPipelineFactory} {private final {@link Timer} timer;public MyPipelineFactory( {@link Timer} timer) {this.timer = timer; } public {@link ChannelPipeline} getPipeline() {return {@link Channels}.pipeline(
new {@link IdleStateHandler}(timer, 60, 30, 0), // timer must be shared. new MyHandler()); } } // Handler should handle the {@link IdleStateEvent} triggered by {@link IdleStateHandler}. public class MyHandler extends {@link IdleStateAwareChannelHandler} {{@code @Override}public void channelIdle( {@link ChannelHandlerContext} ctx, {@link IdleStateEvent} e) {if (e.getState() == {@link IdleState}.READER_IDLE) { e.getChannel().close(); } else if (e.getState() == {@link IdleState}.WRITER_IDLE) { e.getChannel().write(new PingMessage()); } } } {@link ServerBootstrap} bootstrap = ...;{@link Timer} timer = new {@link HashedWheelTimer}(); ... bootstrap.setPipelineFactory(new MyPipelineFactory(timer)); ... The {@link Timer} which was specified when the {@link ReadTimeoutHandler} iscreated should be stopped manually by calling {@link #releaseExternalResources()}or {@link Timer#stop()} when your application shuts down.
@author
The Netty Project
@author
Trustin Lee
@version $Rev: 2224 $, $Date: 2010-03-30 17:02:32 +0900 (Tue, 30 Mar 2010) $
@see ReadTimeoutHandler
@see WriteTimeoutHandler
@apiviz.landmark
@apiviz.uses org.jboss.netty.util.HashedWheelTimer
@apiviz.has org.jboss.netty.handler.timeout.IdleStateEvent oneway - - triggers