Top Description Inners Fields Constructors Methods
io.netty.handler.timeout

public Class IdleStateHandler

extends ChannelDuplexHandler
Class Inheritance
Known Direct Subclasses
io.netty.handler.timeout.ReadTimeoutHandler
Imports
io.netty.bootstrap.ServerBootstrap, io.netty.channel.Channel, .Channel.Unsafe, .ChannelDuplexHandler, .ChannelFuture, .ChannelFutureListener, .ChannelHandlerContext, .ChannelInitializer, .ChannelOutboundBuffer, .ChannelPromise, io.netty.util.concurrent.Future, io.netty.util.internal.ObjectUtil, java.util.concurrent.TimeUnit

Triggers an IdleStateEvent when a Channel has not performed read, write, or both operation for a while.

Supported idle states

PropertyMeaning
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 extends ChannelInitializer<Channel> {
    @Override
    public void initChannel(Channel channel) {
        channel.pipeline().addLast("idleStateHandler", new IdleStateHandler(60, 30, 0));
        channel.pipeline().addLast("myHandler", new MyHandler());
    }
}

// Handler should handle the IdleStateEvent triggered by IdleStateHandler.
public class MyHandler extends ChannelDuplexHandler {
    @Override
    public void userEventTriggered(ChannelHandlerContext ctx, Object evt) throws Exception {
        if (evt instanceof IdleStateEvent) {
            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
ReadTimeoutHandler, WriteTimeoutHandler

Nested and Inner Type Summary

Modifier and TypeClass and Description
private abstract static class
private class
private class
private class

Field Summary

Modifier and TypeField and Description
private 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

AccessConstructor and Description
public
IdleStateHandler(int
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.
readerIdleTimeSeconds
,
int
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.
writerIdleTimeSeconds
,
int
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.
allIdleTimeSeconds
)

Creates a new instance firing IdleStateEvents.

public
IdleStateHandler(long readerIdleTime, long writerIdleTime, long allIdleTime, TimeUnit unit)

public
IdleStateHandler(boolean
whether or not the consumption of bytes should be taken into consideration when assessing write idleness. The default is false.
observeOutput
,
long
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.
readerIdleTime
,
long
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.
writerIdleTime
,
long
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.
allIdleTime
,
TimeUnit
the TimeUnit of readerIdleTime, writeIdleTime, and allIdleTime
unit
)

Creates a new instance firing IdleStateEvents.

Method Summary

Modifier and TypeMethod and Description
public void
protected void
channelIdle(ChannelHandlerContext ctx, IdleStateEvent evt)

Is called when an IdleStateEvent should be fired.

public void
public void
public void
public void
private void
public long
getAllIdleTimeInMillis()

Return the allIdleTime that was given when instance this class in milliseconds.

public long
getReaderIdleTimeInMillis()

Return the readerIdleTime that was given when instance this class in milliseconds.

public long
getWriterIdleTimeInMillis()

Return the writerIdleTime that was given when instance this class in milliseconds.

public void
handlerAdded(ChannelHandlerContext ctx)

Overrides io.netty.channel.ChannelHandlerAdapter.handlerAdded.

Implements io.netty.channel.ChannelHandler.handlerAdded.

Gets called after the ChannelHandler was added to the actual context and it's ready to handle events.
public void
handlerRemoved(ChannelHandlerContext ctx)

Overrides io.netty.channel.ChannelHandlerAdapter.handlerRemoved.

Implements io.netty.channel.ChannelHandler.handlerRemoved.

Gets called after the ChannelHandler was removed from the actual context and it doesn't handle events anymore.
private boolean
hasOutputChanged(ChannelHandlerContext ctx, boolean first)

Returns true if and only if the IdleStateHandler was constructed with observeOutput enabled and there has been an observed change in the ChannelOutboundBuffer between two consecutive calls of this method.

private void
private void
protected IdleStateEvent
newIdleStateEvent(IdleState state, boolean first)

Returns a IdleStateEvent.

public void
resetReadTimeout()

Reset the read timeout.

public void
resetWriteTimeout()

Reset the write timeout.

pack-priv Future<?>
schedule(ChannelHandlerContext ctx, Runnable task, long delay, TimeUnit unit)

This method is visible for testing!

pack-priv long
ticksInNanos()

This method is visible for testing!

public void
write(ChannelHandlerContext
the ChannelHandlerContext for which the write operation is made
ctx
,
Object
the message to write
msg
,
ChannelPromise
the ChannelPromise to notify once the operation completes
promise
)

Overrides io.netty.channel.ChannelDuplexHandler.write.

Implements io.netty.channel.ChannelOutboundHandler.write.

Calls ChannelHandlerContext#write(Object, ChannelPromise) to forward to the next ChannelOutboundHandler in the ChannelPipeline.
Inherited from io.netty.channel.ChannelDuplexHandler:
bindcloseconnectderegisterdisconnectflushread

Field Detail

allIdleTimeNanosback to summary
private final long allIdleTimeNanos
allIdleTimeoutback to summary
private Future<?> allIdleTimeout
firstAllIdleEventback to summary
private boolean firstAllIdleEvent
firstReaderIdleEventback to summary
private boolean firstReaderIdleEvent
firstWriterIdleEventback to summary
private boolean firstWriterIdleEvent
lastChangeCheckTimeStampback to summary
private long lastChangeCheckTimeStamp
lastFlushProgressback to summary
private long lastFlushProgress
lastMessageHashCodeback to summary
private int lastMessageHashCode
lastPendingWriteBytesback to summary
private long lastPendingWriteBytes
lastReadTimeback to summary
private long lastReadTime
lastWriteTimeback to summary
private long lastWriteTime
MIN_TIMEOUT_NANOSback to summary
private static final long MIN_TIMEOUT_NANOS
observeOutputback to summary
private final boolean observeOutput
readerIdleTimeNanosback to summary
private final long readerIdleTimeNanos
readerIdleTimeoutback to summary
private Future<?> readerIdleTimeout
readingback to summary
private boolean reading
ST_DESTROYEDback to summary
private static final byte ST_DESTROYED
ST_INITIALIZEDback to summary
private static final byte ST_INITIALIZED
stateback to summary
private byte state
writeListenerback to summary
private final ChannelFutureListener writeListener
writerIdleTimeNanosback to summary
private final long writerIdleTimeNanos
writerIdleTimeoutback to summary
private Future<?> writerIdleTimeout

Constructor Detail

IdleStateHandlerback to summary
public IdleStateHandler(int readerIdleTimeSeconds, int writerIdleTimeSeconds, int allIdleTimeSeconds)

Creates a new instance firing IdleStateEvents.

Parameters
readerIdleTimeSeconds:int

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:int

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:int

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.

IdleStateHandlerback to summary
public IdleStateHandler(long readerIdleTime, long writerIdleTime, long allIdleTime, TimeUnit unit)
See Also
IdleStateHandler(boolean, long, long, long, TimeUnit)
IdleStateHandlerback to summary
public IdleStateHandler(boolean observeOutput, long readerIdleTime, long writerIdleTime, long allIdleTime, TimeUnit unit)

Creates a new instance firing IdleStateEvents.

Parameters
observeOutput:boolean

whether or not the consumption of bytes should be taken into consideration when assessing write idleness. The default is false.

readerIdleTime:long

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:long

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:long

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:TimeUnit

the TimeUnit of readerIdleTime, writeIdleTime, and allIdleTime

Method Detail

channelActiveback to summary
public void channelActive(ChannelHandlerContext ctx) throws Exception

Overrides io.netty.channel.ChannelInboundHandlerAdapter.channelActive.

Implements io.netty.channel.ChannelInboundHandler.channelActive.

Doc from io.netty.channel.ChannelInboundHandlerAdapter.channelActive.

Calls ChannelHandlerContext#fireChannelActive() to forward to the next ChannelInboundHandler in the ChannelPipeline. Sub-classes may override this method to change behavior.

Annotations
@Override
channelIdleback to summary
protected void channelIdle(ChannelHandlerContext ctx, IdleStateEvent evt) throws Exception

Is called when an IdleStateEvent should be fired. This implementation calls ChannelHandlerContext#fireUserEventTriggered(Object).

channelInactiveback to summary
public void channelInactive(ChannelHandlerContext ctx) throws Exception

Overrides io.netty.channel.ChannelInboundHandlerAdapter.channelInactive.

Implements io.netty.channel.ChannelInboundHandler.channelInactive.

Doc from io.netty.channel.ChannelInboundHandlerAdapter.channelInactive.

Calls ChannelHandlerContext#fireChannelInactive() to forward to the next ChannelInboundHandler in the ChannelPipeline. Sub-classes may override this method to change behavior.

Annotations
@Override
channelReadback to summary
public void channelRead(ChannelHandlerContext ctx, Object msg) throws Exception

Overrides io.netty.channel.ChannelInboundHandlerAdapter.channelRead.

Implements io.netty.channel.ChannelInboundHandler.channelRead.

Doc from io.netty.channel.ChannelInboundHandlerAdapter.channelRead.

Calls ChannelHandlerContext#fireChannelRead(Object) to forward to the next ChannelInboundHandler in the ChannelPipeline. Sub-classes may override this method to change behavior.

Annotations
@Override
channelReadCompleteback to summary
public void channelReadComplete(ChannelHandlerContext ctx) throws Exception

Overrides io.netty.channel.ChannelInboundHandlerAdapter.channelReadComplete.

Implements io.netty.channel.ChannelInboundHandler.channelReadComplete.

Doc from io.netty.channel.ChannelInboundHandlerAdapter.channelReadComplete.

Calls ChannelHandlerContext#fireChannelReadComplete() to forward to the next ChannelInboundHandler in the ChannelPipeline. Sub-classes may override this method to change behavior.

Annotations
@Override
channelRegisteredback to summary
public void channelRegistered(ChannelHandlerContext ctx) throws Exception

Overrides io.netty.channel.ChannelInboundHandlerAdapter.channelRegistered.

Implements io.netty.channel.ChannelInboundHandler.channelRegistered.

Doc from io.netty.channel.ChannelInboundHandlerAdapter.channelRegistered.

Calls ChannelHandlerContext#fireChannelRegistered() to forward to the next ChannelInboundHandler in the ChannelPipeline. Sub-classes may override this method to change behavior.

Annotations
@Override
destroyback to summary
private void destroy()
getAllIdleTimeInMillisback to summary
public long getAllIdleTimeInMillis()

Return the allIdleTime that was given when instance this class in milliseconds.

getReaderIdleTimeInMillisback to summary
public long getReaderIdleTimeInMillis()

Return the readerIdleTime that was given when instance this class in milliseconds.

getWriterIdleTimeInMillisback to summary
public long getWriterIdleTimeInMillis()

Return the writerIdleTime that was given when instance this class in milliseconds.

handlerAddedback to summary
public void handlerAdded(ChannelHandlerContext ctx) throws Exception

Overrides io.netty.channel.ChannelHandlerAdapter.handlerAdded.

Implements io.netty.channel.ChannelHandler.handlerAdded.

Doc from io.netty.channel.ChannelHandler.handlerAdded.

Gets called after the ChannelHandler was added to the actual context and it's ready to handle events.

Annotations
@Override
handlerRemovedback to summary
public void handlerRemoved(ChannelHandlerContext ctx) throws Exception

Overrides io.netty.channel.ChannelHandlerAdapter.handlerRemoved.

Implements io.netty.channel.ChannelHandler.handlerRemoved.

Doc from io.netty.channel.ChannelHandler.handlerRemoved.

Gets called after the ChannelHandler was removed from the actual context and it doesn't handle events anymore.

Annotations
@Override
hasOutputChangedback to summary
private boolean hasOutputChanged(ChannelHandlerContext ctx, boolean first)

Returns true if and only if the IdleStateHandler was constructed with observeOutput enabled and there has been an observed change in the ChannelOutboundBuffer between two consecutive calls of this method. https://github.com/netty/netty/issues/6150

initializeback to summary
private void initialize(ChannelHandlerContext ctx)
initOutputChangedback to summary
private void initOutputChanged(ChannelHandlerContext ctx)
See Also
hasOutputChanged(ChannelHandlerContext, boolean)
newIdleStateEventback to summary
protected IdleStateEvent newIdleStateEvent(IdleState state, boolean first)

Returns a IdleStateEvent.

resetReadTimeoutback to summary
public void resetReadTimeout()

Reset the read timeout. As this handler is not thread-safe, this method must be called on the event loop.

resetWriteTimeoutback to summary
public void resetWriteTimeout()

Reset the write timeout. As this handler is not thread-safe, this method must be called on the event loop.

scheduleback to summary
pack-priv Future<?> schedule(ChannelHandlerContext ctx, Runnable task, long delay, TimeUnit unit)

This method is visible for testing!

ticksInNanosback to summary
pack-priv long ticksInNanos()

This method is visible for testing!

writeback to summary
public void write(ChannelHandlerContext ctx, Object msg, ChannelPromise promise) throws Exception

Overrides io.netty.channel.ChannelDuplexHandler.write.

Implements io.netty.channel.ChannelOutboundHandler.write.

Doc from io.netty.channel.ChannelDuplexHandler.write.

Calls ChannelHandlerContext#write(Object, ChannelPromise) to forward to the next ChannelOutboundHandler in the ChannelPipeline. Sub-classes may override this method to change behavior.

Parameters
ctx:ChannelHandlerContext

Doc from io.netty.channel.ChannelOutboundHandler.write.

the ChannelHandlerContext for which the write operation is made

msg:Object

Doc from io.netty.channel.ChannelOutboundHandler.write.

the message to write

promise:ChannelPromise

Doc from io.netty.channel.ChannelOutboundHandler.write.

the ChannelPromise to notify once the operation completes

Annotations
@Override
Exceptions
Exception:

Doc from io.netty.channel.ChannelOutboundHandler.write.

thrown if an error occurs

io.netty.handler.timeout back to summary

private abstract Class IdleStateHandler.AbstractIdleTask

extends Object
implements Runnable
Class Inheritance
All Implemented Interfaces
java.lang.Runnable
Known Direct Subclasses
io.netty.handler.timeout.IdleStateHandler.ReaderIdleTimeoutTask, io.netty.handler.timeout.IdleStateHandler.WriterIdleTimeoutTask, io.netty.handler.timeout.IdleStateHandler.AllIdleTimeoutTask

Field Summary

Modifier and TypeField and Description
private final ChannelHandlerContext

Constructor Summary

AccessConstructor and Description
pack-priv

Method Summary

Modifier and TypeMethod and Description
public void
run()

Implements java.lang.Runnable.run.

Runs this operation.
protected abstract void
Inherited from java.lang.Object:
cloneequalsfinalizegetClasshashCodenotifynotifyAlltoStringwaitwaitwait

Field Detail

ctxback to summary
private final ChannelHandlerContext ctx

Constructor Detail

AbstractIdleTaskback to summary
pack-priv AbstractIdleTask(ChannelHandlerContext ctx)

Method Detail

runback to summary
public void run()

Implements java.lang.Runnable.run.

Doc from java.lang.Runnable.run.

Runs this operation.

Annotations
@Override
runback to summary
protected abstract void run(ChannelHandlerContext ctx)
io.netty.handler.timeout back to summary

private final Class IdleStateHandler.AllIdleTimeoutTask

extends AbstractIdleTask
Class Inheritance

Constructor Summary

AccessConstructor and Description
pack-priv

Method Summary

Modifier and TypeMethod and Description
protected void
Inherited from io.netty.handler.timeout.IdleStateHandler.AbstractIdleTask:
run

Constructor Detail

AllIdleTimeoutTaskback to summary
pack-priv AllIdleTimeoutTask(ChannelHandlerContext ctx)

Method Detail

runback to summary
protected void run(ChannelHandlerContext ctx)

Implements abstract io.netty.handler.timeout.IdleStateHandler.AbstractIdleTask.run.

Annotations
@Override
io.netty.handler.timeout back to summary

private final Class IdleStateHandler.ReaderIdleTimeoutTask

extends AbstractIdleTask
Class Inheritance

Constructor Summary

AccessConstructor and Description
pack-priv

Method Summary

Modifier and TypeMethod and Description
protected void
Inherited from io.netty.handler.timeout.IdleStateHandler.AbstractIdleTask:
run

Constructor Detail

ReaderIdleTimeoutTaskback to summary
pack-priv ReaderIdleTimeoutTask(ChannelHandlerContext ctx)

Method Detail

runback to summary
protected void run(ChannelHandlerContext ctx)

Implements abstract io.netty.handler.timeout.IdleStateHandler.AbstractIdleTask.run.

Annotations
@Override
io.netty.handler.timeout back to summary

private final Class IdleStateHandler.WriterIdleTimeoutTask

extends AbstractIdleTask
Class Inheritance

Constructor Summary

AccessConstructor and Description
pack-priv

Method Summary

Modifier and TypeMethod and Description
protected void
Inherited from io.netty.handler.timeout.IdleStateHandler.AbstractIdleTask:
run

Constructor Detail

WriterIdleTimeoutTaskback to summary
pack-priv WriterIdleTimeoutTask(ChannelHandlerContext ctx)

Method Detail

runback to summary
protected void run(ChannelHandlerContext ctx)

Implements abstract io.netty.handler.timeout.IdleStateHandler.AbstractIdleTask.run.

Annotations
@Override