- java.lang.Object
-
- io.netty5.handler.timeout.IdleStateHandler
-
- All Implemented Interfaces:
ChannelHandler
- Direct Known Subclasses:
ReadTimeoutHandler
public class IdleStateHandler extends Object implements ChannelHandler
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 implementsChannelHandler{@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
-
-
Constructor Summary
Constructors Constructor Description IdleStateHandler(int readerIdleTimeSeconds, int writerIdleTimeSeconds, int allIdleTimeSeconds)Creates a new instance firingIdleStateEvents.IdleStateHandler(long readerIdleTime, long writerIdleTime, long allIdleTime, TimeUnit unit)Creates a new instance firingIdleStateEvents.
-
Method Summary
All Methods Instance Methods Concrete Methods Modifier and Type Method Description voidchannelActive(ChannelHandlerContext ctx)TheChannelof theChannelHandlerContextis now activeprotected voidchannelIdle(ChannelHandlerContext ctx, IdleStateEvent evt)Is called when anIdleStateEventshould be fired.voidchannelInactive(ChannelHandlerContext ctx)TheChannelof theChannelHandlerContextwas registered is now inactive and reached its end of lifetime.voidchannelRead(ChannelHandlerContext ctx, Object msg)Invoked when the currentChannelhas read a message from the peer.voidchannelReadComplete(ChannelHandlerContext ctx)Invoked when the last message read by the current read operation has been consumed byChannelHandler.channelRead(ChannelHandlerContext, Object).voidchannelRegistered(ChannelHandlerContext ctx)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)Gets called after theChannelHandlerwas added to the actual context and it's ready to handle events.voidhandlerRemoved(ChannelHandlerContext ctx)Gets called after theChannelHandlerwas removed from the actual context and it doesn't handle events anymore.protected IdleStateEventnewIdleStateEvent(IdleState state, boolean first)Returns aIdleStateEvent.Future<Void>write(ChannelHandlerContext ctx, Object msg)Called once a write operation is made.-
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
-
Methods inherited from interface io.netty5.channel.ChannelHandler
bind, channelExceptionCaught, channelInboundEvent, channelShutdown, channelUnregistered, channelWritabilityChanged, close, connect, deregister, disconnect, flush, isSharable, pendingOutboundBytes, read, register, sendOutboundEvent, shutdown
-
-
-
-
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, TimeUnit unit)Creates a new instance firingIdleStateEvents.- Parameters:
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 Exception
Description copied from interface:ChannelHandlerGets called after theChannelHandlerwas added to the actual context and it's ready to handle events.- Specified by:
handlerAddedin interfaceChannelHandler- Throws:
Exception
-
handlerRemoved
public void handlerRemoved(ChannelHandlerContext ctx) throws Exception
Description copied from interface:ChannelHandlerGets called after theChannelHandlerwas removed from the actual context and it doesn't handle events anymore.- Specified by:
handlerRemovedin interfaceChannelHandler- Throws:
Exception
-
channelRegistered
public void channelRegistered(ChannelHandlerContext ctx) throws Exception
Description copied from interface:ChannelHandler- Specified by:
channelRegisteredin interfaceChannelHandler- Throws:
Exception
-
channelActive
public void channelActive(ChannelHandlerContext ctx) throws Exception
Description copied from interface:ChannelHandlerTheChannelof theChannelHandlerContextis now active- Specified by:
channelActivein interfaceChannelHandler- Throws:
Exception
-
channelInactive
public void channelInactive(ChannelHandlerContext ctx) throws Exception
Description copied from interface:ChannelHandlerTheChannelof theChannelHandlerContextwas registered is now inactive and reached its end of lifetime.- Specified by:
channelInactivein interfaceChannelHandler- Throws:
Exception
-
channelRead
public void channelRead(ChannelHandlerContext ctx, Object msg) throws Exception
Description copied from interface:ChannelHandlerInvoked when the currentChannelhas read a message from the peer.- Specified by:
channelReadin interfaceChannelHandler- Throws:
Exception
-
channelReadComplete
public void channelReadComplete(ChannelHandlerContext ctx) throws Exception
Description copied from interface:ChannelHandlerInvoked when the last message read by the current read operation has been consumed byChannelHandler.channelRead(ChannelHandlerContext, Object). IfChannelOption.AUTO_READis off, no further attempt to read an inbound data from the currentChannelwill be made untilChannelHandlerContext.read()is called.- Specified by:
channelReadCompletein interfaceChannelHandler- Throws:
Exception
-
write
public Future<Void> write(ChannelHandlerContext ctx, Object msg)
Description copied from interface:ChannelHandlerCalled once a write operation is made. The write operation will write the messages through theChannelPipeline. Those are then ready to be flushed to the actualChannelonceChannel.flush()is called.- Specified by:
writein interfaceChannelHandler- Parameters:
ctx- theChannelHandlerContextfor which the write operation is mademsg- the message to write- Returns:
- the
Futurewhich will be notified once the operation completes.
-
channelIdle
protected void channelIdle(ChannelHandlerContext ctx, IdleStateEvent evt) throws Exception
Is called when anIdleStateEventshould be fired. This implementation callsChannelHandlerContext.fireChannelInboundEvent(Object).- Throws:
Exception
-
newIdleStateEvent
protected IdleStateEvent newIdleStateEvent(IdleState state, boolean first)
Returns aIdleStateEvent.
-
-