Package io.netty.handler.timeout
Class IdleStateHandler
java.lang.Object
io.netty.channel.ChannelHandlerAdapter
io.netty.channel.ChannelInboundHandlerAdapter
io.netty.channel.ChannelDuplexHandler
io.netty.handler.timeout.IdleStateHandler
- All Implemented Interfaces:
ChannelHandler
,ChannelInboundHandler
,ChannelOutboundHandler
- Direct Known Subclasses:
ReadTimeoutHandler
Triggers an
IdleStateEvent
when a Channel
has not performed
read, write, or both operation for a while.
Supported idle states
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 MyChannelInitializer extendsChannelInitializer
<Channel
> {@Override
public void initChannel(Channel
channel) { channel.pipeline().addLast("idleStateHandler", newIdleStateHandler
(60, 30, 0)); channel.pipeline().addLast("myHandler", new MyHandler()); } } // Handler should handle theIdleStateEvent
triggered byIdleStateHandler
. public class MyHandler extendsChannelDuplexHandler
{@Override
public void userEventTriggered(ChannelHandlerContext
ctx,Object
evt) throwsException
{ if (evt instanceofIdleStateEvent
) {IdleStateEvent
e = (IdleStateEvent
) evt; if (e.state() ==IdleState
.READER_IDLE) { ctx.close(); } else if (e.state() ==IdleState
.WRITER_IDLE) { ctx.writeAndFlush(new PingMessage()); } } } }ServerBootstrap
bootstrap = ...; ... bootstrap.childHandler(new MyChannelInitializer()); ...
- See Also:
-
Nested Class Summary
Nested ClassesModifier and TypeClassDescriptionprivate static class
private final class
private final class
private final class
Nested classes/interfaces inherited from interface io.netty.channel.ChannelHandler
ChannelHandler.Sharable
-
Field Summary
FieldsModifier and TypeFieldDescriptionprivate final long
private Future
<?> private boolean
private boolean
private boolean
private long
private long
private int
private long
private long
private long
private static final long
private final boolean
private final long
private Future
<?> private boolean
private static final byte
private static final byte
private byte
private final ChannelFutureListener
private final long
private Future
<?> -
Constructor Summary
ConstructorsConstructorDescriptionIdleStateHandler
(boolean observeOutput, long readerIdleTime, long writerIdleTime, long allIdleTime, TimeUnit unit) Creates a new instance firingIdleStateEvent
s.IdleStateHandler
(int readerIdleTimeSeconds, int writerIdleTimeSeconds, int allIdleTimeSeconds) Creates a new instance firingIdleStateEvent
s.IdleStateHandler
(long readerIdleTime, long writerIdleTime, long allIdleTime, TimeUnit unit) -
Method Summary
Modifier and TypeMethodDescriptionvoid
CallsChannelHandlerContext.fireChannelActive()
to forward to the nextChannelInboundHandler
in theChannelPipeline
.protected void
channelIdle
(ChannelHandlerContext ctx, IdleStateEvent evt) Is called when anIdleStateEvent
should be fired.void
CallsChannelHandlerContext.fireChannelInactive()
to forward to the nextChannelInboundHandler
in theChannelPipeline
.void
channelRead
(ChannelHandlerContext ctx, Object msg) CallsChannelHandlerContext.fireChannelRead(Object)
to forward to the nextChannelInboundHandler
in theChannelPipeline
.void
CallsChannelHandlerContext.fireChannelReadComplete()
to forward to the nextChannelInboundHandler
in theChannelPipeline
.void
CallsChannelHandlerContext.fireChannelRegistered()
to forward to the nextChannelInboundHandler
in theChannelPipeline
.private void
destroy()
long
Return the allIdleTime that was given when instance this class in milliseconds.long
Return the readerIdleTime that was given when instance this class in milliseconds.long
Return the writerIdleTime that was given when instance this class in milliseconds.void
Do nothing by default, sub-classes may override this method.void
Do nothing by default, sub-classes may override this method.private boolean
hasOutputChanged
(ChannelHandlerContext ctx, boolean first) Returnstrue
if and only if theIdleStateHandler
was constructed withobserveOutput
enabled and there has been an observed change in theChannelOutboundBuffer
between two consecutive calls of this method.private void
private void
protected IdleStateEvent
newIdleStateEvent
(IdleState state, boolean first) Returns aIdleStateEvent
.void
Reset the read timeout.void
Reset the write timeout.(package private) Future
<?> schedule
(ChannelHandlerContext ctx, Runnable task, long delay, TimeUnit unit) This method is visible for testing!(package private) long
This method is visible for testing!void
write
(ChannelHandlerContext ctx, Object msg, ChannelPromise promise) CallsChannelOutboundInvoker.write(Object, ChannelPromise)
to forward to the nextChannelOutboundHandler
in theChannelPipeline
.Methods inherited from class io.netty.channel.ChannelDuplexHandler
bind, close, connect, deregister, disconnect, flush, read
Methods inherited from class io.netty.channel.ChannelInboundHandlerAdapter
channelUnregistered, channelWritabilityChanged, exceptionCaught, userEventTriggered
Methods inherited from class io.netty.channel.ChannelHandlerAdapter
ensureNotSharable, isSharable
-
Field Details
-
MIN_TIMEOUT_NANOS
private static final long MIN_TIMEOUT_NANOS -
writeListener
-
observeOutput
private final boolean observeOutput -
readerIdleTimeNanos
private final long readerIdleTimeNanos -
writerIdleTimeNanos
private final long writerIdleTimeNanos -
allIdleTimeNanos
private final long allIdleTimeNanos -
readerIdleTimeout
-
lastReadTime
private long lastReadTime -
firstReaderIdleEvent
private boolean firstReaderIdleEvent -
writerIdleTimeout
-
lastWriteTime
private long lastWriteTime -
firstWriterIdleEvent
private boolean firstWriterIdleEvent -
allIdleTimeout
-
firstAllIdleEvent
private boolean firstAllIdleEvent -
state
private byte state -
ST_INITIALIZED
private static final byte ST_INITIALIZED- See Also:
-
ST_DESTROYED
private static final byte ST_DESTROYED- See Also:
-
reading
private boolean reading -
lastChangeCheckTimeStamp
private long lastChangeCheckTimeStamp -
lastMessageHashCode
private int lastMessageHashCode -
lastPendingWriteBytes
private long lastPendingWriteBytes -
lastFlushProgress
private long lastFlushProgress
-
-
Constructor Details
-
IdleStateHandler
public IdleStateHandler(int readerIdleTimeSeconds, int writerIdleTimeSeconds, int allIdleTimeSeconds) Creates a new instance firingIdleStateEvent
s.- Parameters:
readerIdleTimeSeconds
- anIdleStateEvent
whose state isIdleState.READER_IDLE
will be triggered when no read was performed for the specified period of time. Specify0
to disable.writerIdleTimeSeconds
- anIdleStateEvent
whose state isIdleState.WRITER_IDLE
will be triggered when no write was performed for the specified period of time. Specify0
to disable.allIdleTimeSeconds
- anIdleStateEvent
whose state isIdleState.ALL_IDLE
will be triggered when neither read nor write was performed for the specified period of time. Specify0
to disable.
-
IdleStateHandler
- See Also:
-
IdleStateHandler
public IdleStateHandler(boolean observeOutput, long readerIdleTime, long writerIdleTime, long allIdleTime, TimeUnit unit) Creates a new instance firingIdleStateEvent
s.- Parameters:
observeOutput
- whether or not the consumption ofbytes
should be taken into consideration when assessing write idleness. The default isfalse
.readerIdleTime
- anIdleStateEvent
whose state isIdleState.READER_IDLE
will be triggered when no read was performed for the specified period of time. Specify0
to disable.writerIdleTime
- anIdleStateEvent
whose state isIdleState.WRITER_IDLE
will be triggered when no write was performed for the specified period of time. Specify0
to disable.allIdleTime
- anIdleStateEvent
whose state isIdleState.ALL_IDLE
will be triggered when neither read nor write was performed for the specified period of time. Specify0
to disable.unit
- theTimeUnit
ofreaderIdleTime
,writeIdleTime
, andallIdleTime
-
-
Method Details
-
getReaderIdleTimeInMillis
public long getReaderIdleTimeInMillis()Return the readerIdleTime that was given when instance this class in milliseconds. -
getWriterIdleTimeInMillis
public long getWriterIdleTimeInMillis()Return the writerIdleTime that was given when instance this class in milliseconds. -
getAllIdleTimeInMillis
public long getAllIdleTimeInMillis()Return the allIdleTime that was given when instance this class in milliseconds. -
handlerAdded
Description copied from class:ChannelHandlerAdapter
Do nothing by default, sub-classes may override this method.- Specified by:
handlerAdded
in interfaceChannelHandler
- Overrides:
handlerAdded
in classChannelHandlerAdapter
- Throws:
Exception
-
handlerRemoved
Description copied from class:ChannelHandlerAdapter
Do nothing by default, sub-classes may override this method.- Specified by:
handlerRemoved
in interfaceChannelHandler
- Overrides:
handlerRemoved
in classChannelHandlerAdapter
- Throws:
Exception
-
channelRegistered
Description copied from class:ChannelInboundHandlerAdapter
CallsChannelHandlerContext.fireChannelRegistered()
to forward to the nextChannelInboundHandler
in theChannelPipeline
. Sub-classes may override this method to change behavior.- Specified by:
channelRegistered
in interfaceChannelInboundHandler
- Overrides:
channelRegistered
in classChannelInboundHandlerAdapter
- Throws:
Exception
-
channelActive
Description copied from class:ChannelInboundHandlerAdapter
CallsChannelHandlerContext.fireChannelActive()
to forward to the nextChannelInboundHandler
in theChannelPipeline
. Sub-classes may override this method to change behavior.- Specified by:
channelActive
in interfaceChannelInboundHandler
- Overrides:
channelActive
in classChannelInboundHandlerAdapter
- Throws:
Exception
-
channelInactive
Description copied from class:ChannelInboundHandlerAdapter
CallsChannelHandlerContext.fireChannelInactive()
to forward to the nextChannelInboundHandler
in theChannelPipeline
. Sub-classes may override this method to change behavior.- Specified by:
channelInactive
in interfaceChannelInboundHandler
- Overrides:
channelInactive
in classChannelInboundHandlerAdapter
- Throws:
Exception
-
channelRead
Description copied from class:ChannelInboundHandlerAdapter
CallsChannelHandlerContext.fireChannelRead(Object)
to forward to the nextChannelInboundHandler
in theChannelPipeline
. Sub-classes may override this method to change behavior.- Specified by:
channelRead
in interfaceChannelInboundHandler
- Overrides:
channelRead
in classChannelInboundHandlerAdapter
- Throws:
Exception
-
channelReadComplete
Description copied from class:ChannelInboundHandlerAdapter
CallsChannelHandlerContext.fireChannelReadComplete()
to forward to the nextChannelInboundHandler
in theChannelPipeline
. Sub-classes may override this method to change behavior.- Specified by:
channelReadComplete
in interfaceChannelInboundHandler
- Overrides:
channelReadComplete
in classChannelInboundHandlerAdapter
- Throws:
Exception
-
write
Description copied from class:ChannelDuplexHandler
CallsChannelOutboundInvoker.write(Object, ChannelPromise)
to forward to the nextChannelOutboundHandler
in theChannelPipeline
. Sub-classes may override this method to change behavior.- Specified by:
write
in interfaceChannelOutboundHandler
- Overrides:
write
in classChannelDuplexHandler
- Parameters:
ctx
- theChannelHandlerContext
for which the write operation is mademsg
- the message to writepromise
- theChannelPromise
to notify once the operation completes- Throws:
Exception
- thrown if an error occurs
-
resetReadTimeout
public void resetReadTimeout()Reset the read timeout. As this handler is not thread-safe, this method must be called on the event loop. -
resetWriteTimeout
public void resetWriteTimeout()Reset the write timeout. As this handler is not thread-safe, this method must be called on the event loop. -
initialize
-
ticksInNanos
long ticksInNanos()This method is visible for testing! -
schedule
This method is visible for testing! -
destroy
private void destroy() -
channelIdle
Is called when anIdleStateEvent
should be fired. This implementation callsChannelHandlerContext.fireUserEventTriggered(Object)
.- Throws:
Exception
-
newIdleStateEvent
Returns aIdleStateEvent
. -
initOutputChanged
- See Also:
-
hasOutputChanged
Returnstrue
if and only if theIdleStateHandler
was constructed withobserveOutput
enabled and there has been an observed change in theChannelOutboundBuffer
between two consecutive calls of this method. https://github.com/netty/netty/issues/6150
-