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 anIdleStateEventwhen aChannelhas not performed read, write, or both operation for a while.Supported idle states
Property Meaning readerIdleTimean IdleStateEventwhose state isIdleState.READER_IDLEwill be triggered when no read was performed for the specified period of time. Specify0to disable.writerIdleTimean IdleStateEventwhose state isIdleState.WRITER_IDLEwill be triggered when no write was performed for the specified period of time. Specify0to disable.allIdleTimean IdleStateEventwhose state isIdleState.ALL_IDLEwill be triggered when neither read nor write was performed for the specified period of time. Specify0to 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> {@Overridepublic void initChannel(Channelchannel) { channel.pipeline().addLast("idleStateHandler", newIdleStateHandler(60, 30, 0)); channel.pipeline().addLast("myHandler", new MyHandler()); } } // Handler should handle theIdleStateEventtriggered byIdleStateHandler. public class MyHandler extendsChannelDuplexHandler{@Overridepublic void userEventTriggered(ChannelHandlerContextctx,Objectevt) throwsException{ if (evt instanceofIdleStateEvent) {IdleStateEvente = (IdleStateEvent) evt; if (e.state() ==IdleState.READER_IDLE) { ctx.close(); } else if (e.state() ==IdleState.WRITER_IDLE) { ctx.writeAndFlush(new PingMessage()); } } } }ServerBootstrapbootstrap = ...; ... bootstrap.childHandler(new MyChannelInitializer()); ...- See Also:
ReadTimeoutHandler,WriteTimeoutHandler
-
-
Nested Class Summary
Nested Classes Modifier and Type Class Description private static classIdleStateHandler.AbstractIdleTaskprivate classIdleStateHandler.AllIdleTimeoutTaskprivate classIdleStateHandler.ReaderIdleTimeoutTaskprivate classIdleStateHandler.WriterIdleTimeoutTask-
Nested classes/interfaces inherited from interface io.netty.channel.ChannelHandler
ChannelHandler.Sharable
-
-
Field Summary
Fields Modifier and Type Field Description private longallIdleTimeNanosprivate java.util.concurrent.ScheduledFuture<?>allIdleTimeoutprivate booleanfirstAllIdleEventprivate booleanfirstReaderIdleEventprivate booleanfirstWriterIdleEventprivate longlastChangeCheckTimeStampprivate longlastFlushProgressprivate intlastMessageHashCodeprivate longlastPendingWriteBytesprivate longlastReadTimeprivate longlastWriteTimeprivate static longMIN_TIMEOUT_NANOSprivate booleanobserveOutputprivate longreaderIdleTimeNanosprivate java.util.concurrent.ScheduledFuture<?>readerIdleTimeoutprivate booleanreadingprivate bytestateprivate ChannelFutureListenerwriteListenerprivate longwriterIdleTimeNanosprivate 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 firingIdleStateEvents.IdleStateHandler(int readerIdleTimeSeconds, int writerIdleTimeSeconds, int allIdleTimeSeconds)Creates a new instance firingIdleStateEvents.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 voidchannelActive(ChannelHandlerContext ctx)CallsChannelHandlerContext.fireChannelActive()to forward to the nextChannelInboundHandlerin theChannelPipeline.protected voidchannelIdle(ChannelHandlerContext ctx, IdleStateEvent evt)Is called when anIdleStateEventshould be fired.voidchannelInactive(ChannelHandlerContext ctx)CallsChannelHandlerContext.fireChannelInactive()to forward to the nextChannelInboundHandlerin theChannelPipeline.voidchannelRead(ChannelHandlerContext ctx, java.lang.Object msg)CallsChannelHandlerContext.fireChannelRead(Object)to forward to the nextChannelInboundHandlerin theChannelPipeline.voidchannelReadComplete(ChannelHandlerContext ctx)CallsChannelHandlerContext.fireChannelReadComplete()to forward to the nextChannelInboundHandlerin theChannelPipeline.voidchannelRegistered(ChannelHandlerContext ctx)CallsChannelHandlerContext.fireChannelRegistered()to forward to the nextChannelInboundHandlerin theChannelPipeline.private voiddestroy()longgetAllIdleTimeInMillis()Return the allIdleTime that was given when instance this class in milliseconds.longgetReaderIdleTimeInMillis()Return the readerIdleTime that was given when instance this class in milliseconds.longgetWriterIdleTimeInMillis()Return the writerIdleTime that was given when instance this class in milliseconds.voidhandlerAdded(ChannelHandlerContext ctx)Do nothing by default, sub-classes may override this method.voidhandlerRemoved(ChannelHandlerContext ctx)Do nothing by default, sub-classes may override this method.private booleanhasOutputChanged(ChannelHandlerContext ctx, boolean first)Returnstrueif and only if theIdleStateHandlerwas constructed withobserveOutputenabled and there has been an observed change in theChannelOutboundBufferbetween two consecutive calls of this method.private voidinitialize(ChannelHandlerContext ctx)private voidinitOutputChanged(ChannelHandlerContext ctx)protected IdleStateEventnewIdleStateEvent(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) longticksInNanos()This method is visible for testing!voidwrite(ChannelHandlerContext ctx, java.lang.Object msg, ChannelPromise promise)CallsChannelOutboundInvoker.write(Object, ChannelPromise)to forward to the nextChannelOutboundHandlerin 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 firingIdleStateEvents.- Parameters:
readerIdleTimeSeconds- anIdleStateEventwhose state isIdleState.READER_IDLEwill be triggered when no read was performed for the specified period of time. Specify0to disable.writerIdleTimeSeconds- anIdleStateEventwhose state isIdleState.WRITER_IDLEwill be triggered when no write was performed for the specified period of time. Specify0to disable.allIdleTimeSeconds- anIdleStateEventwhose state isIdleState.ALL_IDLEwill be triggered when neither read nor write was performed for the specified period of time. Specify0to 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 firingIdleStateEvents.- Parameters:
observeOutput- whether or not the consumption ofbytesshould be taken into consideration when assessing write idleness. The default isfalse.readerIdleTime- anIdleStateEventwhose state isIdleState.READER_IDLEwill be triggered when no read was performed for the specified period of time. Specify0to disable.writerIdleTime- anIdleStateEventwhose state isIdleState.WRITER_IDLEwill be triggered when no write was performed for the specified period of time. Specify0to disable.allIdleTime- anIdleStateEventwhose state isIdleState.ALL_IDLEwill be triggered when neither read nor write was performed for the specified period of time. Specify0to disable.unit- theTimeUnitofreaderIdleTime,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:ChannelHandlerAdapterDo nothing by default, sub-classes may override this method.- Specified by:
handlerAddedin interfaceChannelHandler- Overrides:
handlerAddedin classChannelHandlerAdapter- Throws:
java.lang.Exception
-
handlerRemoved
public void handlerRemoved(ChannelHandlerContext ctx) throws java.lang.Exception
Description copied from class:ChannelHandlerAdapterDo nothing by default, sub-classes may override this method.- Specified by:
handlerRemovedin interfaceChannelHandler- Overrides:
handlerRemovedin classChannelHandlerAdapter- Throws:
java.lang.Exception
-
channelRegistered
public void channelRegistered(ChannelHandlerContext ctx) throws java.lang.Exception
Description copied from class:ChannelInboundHandlerAdapterCallsChannelHandlerContext.fireChannelRegistered()to forward to the nextChannelInboundHandlerin theChannelPipeline. Sub-classes may override this method to change behavior.- Specified by:
channelRegisteredin interfaceChannelInboundHandler- Overrides:
channelRegisteredin classChannelInboundHandlerAdapter- Throws:
java.lang.Exception
-
channelActive
public void channelActive(ChannelHandlerContext ctx) throws java.lang.Exception
Description copied from class:ChannelInboundHandlerAdapterCallsChannelHandlerContext.fireChannelActive()to forward to the nextChannelInboundHandlerin theChannelPipeline. Sub-classes may override this method to change behavior.- Specified by:
channelActivein interfaceChannelInboundHandler- Overrides:
channelActivein classChannelInboundHandlerAdapter- Throws:
java.lang.Exception
-
channelInactive
public void channelInactive(ChannelHandlerContext ctx) throws java.lang.Exception
Description copied from class:ChannelInboundHandlerAdapterCallsChannelHandlerContext.fireChannelInactive()to forward to the nextChannelInboundHandlerin theChannelPipeline. Sub-classes may override this method to change behavior.- Specified by:
channelInactivein interfaceChannelInboundHandler- Overrides:
channelInactivein classChannelInboundHandlerAdapter- Throws:
java.lang.Exception
-
channelRead
public void channelRead(ChannelHandlerContext ctx, java.lang.Object msg) throws java.lang.Exception
Description copied from class:ChannelInboundHandlerAdapterCallsChannelHandlerContext.fireChannelRead(Object)to forward to the nextChannelInboundHandlerin theChannelPipeline. Sub-classes may override this method to change behavior.- Specified by:
channelReadin interfaceChannelInboundHandler- Overrides:
channelReadin classChannelInboundHandlerAdapter- Throws:
java.lang.Exception
-
channelReadComplete
public void channelReadComplete(ChannelHandlerContext ctx) throws java.lang.Exception
Description copied from class:ChannelInboundHandlerAdapterCallsChannelHandlerContext.fireChannelReadComplete()to forward to the nextChannelInboundHandlerin theChannelPipeline. Sub-classes may override this method to change behavior.- Specified by:
channelReadCompletein interfaceChannelInboundHandler- Overrides:
channelReadCompletein 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:ChannelDuplexHandlerCallsChannelOutboundInvoker.write(Object, ChannelPromise)to forward to the nextChannelOutboundHandlerin theChannelPipeline. Sub-classes may override this method to change behavior.- Specified by:
writein interfaceChannelOutboundHandler- Overrides:
writein classChannelDuplexHandler- Parameters:
ctx- theChannelHandlerContextfor which the write operation is mademsg- the message to writepromise- theChannelPromiseto 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 anIdleStateEventshould 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)
Returnstrueif and only if theIdleStateHandlerwas constructed withobserveOutputenabled and there has been an observed change in theChannelOutboundBufferbetween two consecutive calls of this method. https://github.com/netty/netty/issues/6150
-
-