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> {@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:
-
Nested Class Summary
Nested classes/interfaces inherited from interface ChannelHandler
ChannelHandler.Sharable -
Constructor Summary
ConstructorsConstructorDescriptionIdleStateHandler(boolean observeOutput, long readerIdleTime, long writerIdleTime, long allIdleTime, 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, TimeUnit unit) -
Method Summary
Modifier and TypeMethodDescriptionvoidCallsChannelHandlerContext.fireChannelActive()to forward to the nextChannelInboundHandlerin theChannelPipeline.protected voidchannelIdle(ChannelHandlerContext ctx, IdleStateEvent evt) Is called when anIdleStateEventshould be fired.voidCallsChannelHandlerContext.fireChannelInactive()to forward to the nextChannelInboundHandlerin theChannelPipeline.voidchannelRead(ChannelHandlerContext ctx, Object msg) CallsChannelHandlerContext.fireChannelRead(Object)to forward to the nextChannelInboundHandlerin theChannelPipeline.voidCallsChannelHandlerContext.fireChannelReadComplete()to forward to the nextChannelInboundHandlerin theChannelPipeline.voidCallsChannelHandlerContext.fireChannelRegistered()to forward to the nextChannelInboundHandlerin theChannelPipeline.longReturn the allIdleTime that was given when instance this class in milliseconds.longReturn the readerIdleTime that was given when instance this class in milliseconds.longReturn the writerIdleTime that was given when instance this class in milliseconds.voidDo nothing by default, sub-classes may override this method.voidDo nothing by default, sub-classes may override this method.protected IdleStateEventnewIdleStateEvent(IdleState state, boolean first) Returns aIdleStateEvent.voidReset the read timeout.voidReset the write timeout.voidwrite(ChannelHandlerContext ctx, Object msg, ChannelPromise promise) CallsChannelOutboundInvoker.write(Object, ChannelPromise)to forward to the nextChannelOutboundHandlerin theChannelPipeline.Methods inherited from class ChannelDuplexHandler
bind, close, connect, deregister, disconnect, flush, readMethods inherited from class ChannelInboundHandlerAdapter
channelUnregistered, channelWritabilityChanged, exceptionCaught, userEventTriggeredMethods inherited from class ChannelHandlerAdapter
ensureNotSharable, isSharable
-
Constructor Details
-
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
- See Also:
-
IdleStateHandler
public IdleStateHandler(boolean observeOutput, long readerIdleTime, long writerIdleTime, long allIdleTime, 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 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:ChannelHandlerAdapterDo nothing by default, sub-classes may override this method.- Specified by:
handlerAddedin interfaceChannelHandler- Overrides:
handlerAddedin classChannelHandlerAdapter- Throws:
Exception
-
handlerRemoved
Description copied from class:ChannelHandlerAdapterDo nothing by default, sub-classes may override this method.- Specified by:
handlerRemovedin interfaceChannelHandler- Overrides:
handlerRemovedin classChannelHandlerAdapter- Throws:
Exception
-
channelRegistered
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:
Exception
-
channelActive
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:
Exception
-
channelInactive
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:
Exception
-
channelRead
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:
Exception
-
channelReadComplete
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:
Exception
-
write
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:
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. -
channelIdle
Is called when anIdleStateEventshould be fired. This implementation callsChannelHandlerContext.fireUserEventTriggered(Object).- Throws:
Exception
-
newIdleStateEvent
Returns aIdleStateEvent.
-