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
public class IdleStateHandler extends ChannelDuplexHandler
Triggers anIdleStateEvent
when aChannel
has not performed read, write, or both operation for a while.Supported idle states
Property Meaning readerIdleTime
an IdleStateEvent
whose state isIdleState.READER_IDLE
will be triggered when no read was performed for the specified period of time. Specify0
to disable.writerIdleTime
an IdleStateEvent
whose state isIdleState.WRITER_IDLE
will be triggered when no write was performed for the specified period of time. Specify0
to disable.allIdleTime
an IdleStateEvent
whose state isIdleState.ALL_IDLE
will be triggered when neither read nor write was performed for the specified period of time. Specify0
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", 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:
ReadTimeoutHandler
,WriteTimeoutHandler
-
-
Nested Class Summary
Nested Classes Modifier and Type Class Description private static class
IdleStateHandler.AbstractIdleTask
private class
IdleStateHandler.AllIdleTimeoutTask
private class
IdleStateHandler.ReaderIdleTimeoutTask
private class
IdleStateHandler.WriterIdleTimeoutTask
-
Nested classes/interfaces inherited from interface io.netty.channel.ChannelHandler
ChannelHandler.Sharable
-
-
Field Summary
Fields Modifier and Type Field Description private long
allIdleTimeNanos
private java.util.concurrent.ScheduledFuture<?>
allIdleTimeout
private boolean
firstAllIdleEvent
private boolean
firstReaderIdleEvent
private boolean
firstWriterIdleEvent
private long
lastChangeCheckTimeStamp
private long
lastFlushProgress
private int
lastMessageHashCode
private long
lastPendingWriteBytes
private long
lastReadTime
private long
lastWriteTime
private static long
MIN_TIMEOUT_NANOS
private boolean
observeOutput
private long
readerIdleTimeNanos
private java.util.concurrent.ScheduledFuture<?>
readerIdleTimeout
private boolean
reading
private byte
state
private ChannelFutureListener
writeListener
private long
writerIdleTimeNanos
private java.util.concurrent.ScheduledFuture<?>
writerIdleTimeout
-
Constructor Summary
Constructors Constructor Description IdleStateHandler(boolean observeOutput, long readerIdleTime, long writerIdleTime, long allIdleTime, java.util.concurrent.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, java.util.concurrent.TimeUnit unit)
-
Method Summary
All Methods Instance Methods Concrete Methods Modifier and Type Method Description void
channelActive(ChannelHandlerContext ctx)
CallsChannelHandlerContext.fireChannelActive()
to forward to the nextChannelInboundHandler
in theChannelPipeline
.protected void
channelIdle(ChannelHandlerContext ctx, IdleStateEvent evt)
Is called when anIdleStateEvent
should be fired.void
channelInactive(ChannelHandlerContext ctx)
CallsChannelHandlerContext.fireChannelInactive()
to forward to the nextChannelInboundHandler
in theChannelPipeline
.void
channelRead(ChannelHandlerContext ctx, java.lang.Object msg)
CallsChannelHandlerContext.fireChannelRead(Object)
to forward to the nextChannelInboundHandler
in theChannelPipeline
.void
channelReadComplete(ChannelHandlerContext ctx)
CallsChannelHandlerContext.fireChannelReadComplete()
to forward to the nextChannelInboundHandler
in theChannelPipeline
.void
channelRegistered(ChannelHandlerContext ctx)
CallsChannelHandlerContext.fireChannelRegistered()
to forward to the nextChannelInboundHandler
in theChannelPipeline
.private void
destroy()
long
getAllIdleTimeInMillis()
Return the allIdleTime that was given when instance this class in milliseconds.long
getReaderIdleTimeInMillis()
Return the readerIdleTime that was given when instance this class in milliseconds.long
getWriterIdleTimeInMillis()
Return the writerIdleTime that was given when instance this class in milliseconds.void
handlerAdded(ChannelHandlerContext ctx)
Do nothing by default, sub-classes may override this method.void
handlerRemoved(ChannelHandlerContext ctx)
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
initialize(ChannelHandlerContext ctx)
private void
initOutputChanged(ChannelHandlerContext ctx)
protected IdleStateEvent
newIdleStateEvent(IdleState state, boolean first)
Returns aIdleStateEvent
.(package private) java.util.concurrent.ScheduledFuture<?>
schedule(ChannelHandlerContext ctx, java.lang.Runnable task, long delay, java.util.concurrent.TimeUnit unit)
This method is visible for testing!(package private) long
ticksInNanos()
This method is visible for testing!void
write(ChannelHandlerContext ctx, java.lang.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 Detail
-
MIN_TIMEOUT_NANOS
private static final long MIN_TIMEOUT_NANOS
-
writeListener
private final ChannelFutureListener writeListener
-
observeOutput
private final boolean observeOutput
-
readerIdleTimeNanos
private final long readerIdleTimeNanos
-
writerIdleTimeNanos
private final long writerIdleTimeNanos
-
allIdleTimeNanos
private final long allIdleTimeNanos
-
readerIdleTimeout
private java.util.concurrent.ScheduledFuture<?> readerIdleTimeout
-
lastReadTime
private long lastReadTime
-
firstReaderIdleEvent
private boolean firstReaderIdleEvent
-
writerIdleTimeout
private java.util.concurrent.ScheduledFuture<?> writerIdleTimeout
-
lastWriteTime
private long lastWriteTime
-
firstWriterIdleEvent
private boolean firstWriterIdleEvent
-
allIdleTimeout
private java.util.concurrent.ScheduledFuture<?> allIdleTimeout
-
firstAllIdleEvent
private boolean firstAllIdleEvent
-
state
private byte state
-
reading
private boolean reading
-
lastChangeCheckTimeStamp
private long lastChangeCheckTimeStamp
-
lastMessageHashCode
private int lastMessageHashCode
-
lastPendingWriteBytes
private long lastPendingWriteBytes
-
lastFlushProgress
private long lastFlushProgress
-
-
Constructor Detail
-
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
public IdleStateHandler(long readerIdleTime, long writerIdleTime, long allIdleTime, java.util.concurrent.TimeUnit unit)
-
IdleStateHandler
public IdleStateHandler(boolean observeOutput, long readerIdleTime, long writerIdleTime, long allIdleTime, java.util.concurrent.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 Detail
-
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
public void handlerAdded(ChannelHandlerContext ctx) throws java.lang.Exception
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:
java.lang.Exception
-
handlerRemoved
public void handlerRemoved(ChannelHandlerContext ctx) throws java.lang.Exception
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:
java.lang.Exception
-
channelRegistered
public void channelRegistered(ChannelHandlerContext ctx) throws java.lang.Exception
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:
java.lang.Exception
-
channelActive
public void channelActive(ChannelHandlerContext ctx) throws java.lang.Exception
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:
java.lang.Exception
-
channelInactive
public void channelInactive(ChannelHandlerContext ctx) throws java.lang.Exception
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:
java.lang.Exception
-
channelRead
public void channelRead(ChannelHandlerContext ctx, java.lang.Object msg) throws java.lang.Exception
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:
java.lang.Exception
-
channelReadComplete
public void channelReadComplete(ChannelHandlerContext ctx) throws java.lang.Exception
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:
java.lang.Exception
-
write
public void write(ChannelHandlerContext ctx, java.lang.Object msg, ChannelPromise promise) throws java.lang.Exception
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:
java.lang.Exception
- thrown if an error occurs
-
initialize
private void initialize(ChannelHandlerContext ctx)
-
ticksInNanos
long ticksInNanos()
This method is visible for testing!
-
schedule
java.util.concurrent.ScheduledFuture<?> schedule(ChannelHandlerContext ctx, java.lang.Runnable task, long delay, java.util.concurrent.TimeUnit unit)
This method is visible for testing!
-
destroy
private void destroy()
-
channelIdle
protected void channelIdle(ChannelHandlerContext ctx, IdleStateEvent evt) throws java.lang.Exception
Is called when anIdleStateEvent
should be fired. This implementation callsChannelHandlerContext.fireUserEventTriggered(Object)
.- Throws:
java.lang.Exception
-
newIdleStateEvent
protected IdleStateEvent newIdleStateEvent(IdleState state, boolean first)
Returns aIdleStateEvent
.
-
initOutputChanged
private void initOutputChanged(ChannelHandlerContext ctx)
-
hasOutputChanged
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. https://github.com/netty/netty/issues/6150
-
-