public class IdleStateHandler
extends org.jboss.netty.channel.SimpleChannelUpstreamHandler
implements org.jboss.netty.channel.LifeCycleAwareChannelHandler, org.jboss.netty.util.ExternalResourceReleasable
IdleStateEvent when a Channel has not performed
read, write, or both operation for a while.
| Property | Meaning |
|---|---|
readerIdleTime |
an IdleStateEvent whose state is IdleState.READER_IDLE
will be triggered when no read was performed for the specified period of
time. Specify 0 to disable. |
writerIdleTime |
an IdleStateEvent whose state is IdleState.WRITER_IDLE
will be triggered when no write was performed for the specified period of
time. Specify 0 to disable. |
allIdleTime |
an IdleStateEvent whose state is IdleState.ALL_IDLE will
be triggered when neither read nor write was performed for the specified
period of time. Specify 0 to disable. |
// An example 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 implementsTheChannelPipelineFactory{ private finalTimertimer; public MyPipelineFactory(Timertimer) { this.timer = timer; } publicChannelPipelinegetPipeline() { returnChannels.pipeline( newIdleStateHandler(timer, 60, 30, 0), // timer must be shared. new MyHandler()); } } // Handler should handle theIdleStateEventtriggered byIdleStateHandler. public class MyHandler extendsIdleStateAwareChannelHandler{@Overridepublic void channelIdle(ChannelHandlerContextctx,IdleStateEvente) { if (e.getState() ==IdleState.READER_IDLE) { e.getChannel().close(); } else if (e.getState() ==IdleState.WRITER_IDLE) { e.getChannel().write(new PingMessage()); } } }ServerBootstrapbootstrap = ...;Timertimer = newHashedWheelTimer(); ... bootstrap.setPipelineFactory(new MyPipelineFactory(timer)); ...
Timer which was specified when the ReadTimeoutHandler is
created should be stopped manually by calling
releaseExternalResources() or Timer.stop() when your
application shuts down.ReadTimeoutHandler,
WriteTimeoutHandler| Constructor and Description |
|---|
IdleStateHandler(org.jboss.netty.util.Timer timer,
int allIdleTimeSeconds)
Creates a new instance.
|
IdleStateHandler(org.jboss.netty.util.Timer timer,
long allIdleTime,
TimeUnit unit)
Creates a new instance.
|
| Modifier and Type | Method and Description |
|---|---|
void |
afterAdd(org.jboss.netty.channel.ChannelHandlerContext ctx) |
void |
afterRemove(org.jboss.netty.channel.ChannelHandlerContext ctx) |
void |
beforeAdd(org.jboss.netty.channel.ChannelHandlerContext ctx) |
void |
beforeRemove(org.jboss.netty.channel.ChannelHandlerContext ctx) |
void |
channelClosed(org.jboss.netty.channel.ChannelHandlerContext ctx,
org.jboss.netty.channel.ChannelStateEvent e) |
protected void |
channelIdle(org.jboss.netty.channel.ChannelHandlerContext ctx,
org.jboss.netty.handler.timeout.IdleState state,
long lastActivityTimeMillis) |
void |
channelOpen(org.jboss.netty.channel.ChannelHandlerContext ctx,
org.jboss.netty.channel.ChannelStateEvent e) |
void |
messageReceived(org.jboss.netty.channel.ChannelHandlerContext ctx,
org.jboss.netty.channel.MessageEvent e) |
void |
releaseExternalResources()
Stops the
Timer which was specified in the constructor of this
handler. |
void |
reset() |
void |
writeComplete(org.jboss.netty.channel.ChannelHandlerContext ctx,
org.jboss.netty.channel.WriteCompletionEvent e) |
public IdleStateHandler(org.jboss.netty.util.Timer timer,
int allIdleTimeSeconds)
timer - the Timer that is used to trigger the scheduled
event. The recommended Timer implementation is
HashedWheelTimer.readerIdleTimeSeconds - an IdleStateEvent whose state is
IdleState.READER_IDLE will be triggered when no read was
performed for the specified period of time. Specify 0 to
disable.writerIdleTimeSeconds - an IdleStateEvent whose state is
IdleState.WRITER_IDLE will be triggered when no write was
performed for the specified period of time. Specify 0 to
disable.allIdleTimeSeconds - an IdleStateEvent whose state is
IdleState.ALL_IDLE will be triggered when neither read nor
write was performed for the specified period of time. Specify
0 to disable.public IdleStateHandler(org.jboss.netty.util.Timer timer,
long allIdleTime,
TimeUnit unit)
timer - the Timer that is used to trigger the scheduled
event. The recommended Timer implementation is
HashedWheelTimer.readerIdleTime - an IdleStateEvent whose state is
IdleState.READER_IDLE will be triggered when no read was
performed for the specified period of time. Specify 0 to
disable.writerIdleTime - an IdleStateEvent whose state is
IdleState.WRITER_IDLE will be triggered when no write was
performed for the specified period of time. Specify 0 to
disable.allIdleTime - an IdleStateEvent whose state is
IdleState.ALL_IDLE will be triggered when neither read nor
write was performed for the specified period of time. Specify
0 to disable.unit - the TimeUnit of readerIdleTime, writeIdleTime, and allIdleTimepublic void releaseExternalResources()
Timer which was specified in the constructor of this
handler. You should not call this method if the Timer is in use
by other objects.releaseExternalResources in interface org.jboss.netty.util.ExternalResourceReleasablepublic void beforeAdd(org.jboss.netty.channel.ChannelHandlerContext ctx)
throws Exception
beforeAdd in interface org.jboss.netty.channel.LifeCycleAwareChannelHandlerExceptionpublic void afterAdd(org.jboss.netty.channel.ChannelHandlerContext ctx)
throws Exception
afterAdd in interface org.jboss.netty.channel.LifeCycleAwareChannelHandlerExceptionpublic void beforeRemove(org.jboss.netty.channel.ChannelHandlerContext ctx)
throws Exception
beforeRemove in interface org.jboss.netty.channel.LifeCycleAwareChannelHandlerExceptionpublic void afterRemove(org.jboss.netty.channel.ChannelHandlerContext ctx)
throws Exception
afterRemove in interface org.jboss.netty.channel.LifeCycleAwareChannelHandlerExceptionpublic void channelOpen(org.jboss.netty.channel.ChannelHandlerContext ctx,
org.jboss.netty.channel.ChannelStateEvent e)
throws Exception
channelOpen in class org.jboss.netty.channel.SimpleChannelUpstreamHandlerExceptionpublic void channelClosed(org.jboss.netty.channel.ChannelHandlerContext ctx,
org.jboss.netty.channel.ChannelStateEvent e)
throws Exception
channelClosed in class org.jboss.netty.channel.SimpleChannelUpstreamHandlerExceptionpublic void messageReceived(org.jboss.netty.channel.ChannelHandlerContext ctx,
org.jboss.netty.channel.MessageEvent e)
throws Exception
messageReceived in class org.jboss.netty.channel.SimpleChannelUpstreamHandlerExceptionpublic void writeComplete(org.jboss.netty.channel.ChannelHandlerContext ctx,
org.jboss.netty.channel.WriteCompletionEvent e)
throws Exception
writeComplete in class org.jboss.netty.channel.SimpleChannelUpstreamHandlerExceptionprotected void channelIdle(org.jboss.netty.channel.ChannelHandlerContext ctx,
org.jboss.netty.handler.timeout.IdleState state,
long lastActivityTimeMillis)
throws Exception
Exceptionpublic void reset()
Copyright © 2011. All Rights Reserved.