- java.lang.Object
-
- io.netty5.handler.timeout.IdleStateHandler
-
- All Implemented Interfaces:
io.netty5.channel.ChannelHandler
- Direct Known Subclasses:
ReadTimeoutHandler
public class IdleStateHandler extends Object implements io.netty5.channel.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(io.netty5.channel.ChannelHandlerContext ctx)protected voidchannelIdle(io.netty5.channel.ChannelHandlerContext ctx, IdleStateEvent evt)Is called when anIdleStateEventshould be fired.voidchannelInactive(io.netty5.channel.ChannelHandlerContext ctx)voidchannelRead(io.netty5.channel.ChannelHandlerContext ctx, Object msg)voidchannelReadComplete(io.netty5.channel.ChannelHandlerContext ctx)voidchannelRegistered(io.netty5.channel.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(io.netty5.channel.ChannelHandlerContext ctx)voidhandlerRemoved(io.netty5.channel.ChannelHandlerContext ctx)protected IdleStateEventnewIdleStateEvent(IdleState state, boolean first)Returns aIdleStateEvent.io.netty5.util.concurrent.Future<Void>write(io.netty5.channel.ChannelHandlerContext ctx, Object msg)-
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(io.netty5.channel.ChannelHandlerContext ctx) throws Exception- Specified by:
handlerAddedin interfaceio.netty5.channel.ChannelHandler- Throws:
Exception
-
handlerRemoved
public void handlerRemoved(io.netty5.channel.ChannelHandlerContext ctx) throws Exception- Specified by:
handlerRemovedin interfaceio.netty5.channel.ChannelHandler- Throws:
Exception
-
channelRegistered
public void channelRegistered(io.netty5.channel.ChannelHandlerContext ctx) throws Exception- Specified by:
channelRegisteredin interfaceio.netty5.channel.ChannelHandler- Throws:
Exception
-
channelActive
public void channelActive(io.netty5.channel.ChannelHandlerContext ctx) throws Exception- Specified by:
channelActivein interfaceio.netty5.channel.ChannelHandler- Throws:
Exception
-
channelInactive
public void channelInactive(io.netty5.channel.ChannelHandlerContext ctx) throws Exception- Specified by:
channelInactivein interfaceio.netty5.channel.ChannelHandler- Throws:
Exception
-
channelRead
public void channelRead(io.netty5.channel.ChannelHandlerContext ctx, Object msg) throws Exception- Specified by:
channelReadin interfaceio.netty5.channel.ChannelHandler- Throws:
Exception
-
channelReadComplete
public void channelReadComplete(io.netty5.channel.ChannelHandlerContext ctx) throws Exception- Specified by:
channelReadCompletein interfaceio.netty5.channel.ChannelHandler- Throws:
Exception
-
write
public io.netty5.util.concurrent.Future<Void> write(io.netty5.channel.ChannelHandlerContext ctx, Object msg)
- Specified by:
writein interfaceio.netty5.channel.ChannelHandler
-
channelIdle
protected void channelIdle(io.netty5.channel.ChannelHandlerContext ctx, IdleStateEvent evt) throws ExceptionIs called when anIdleStateEventshould be fired. This implementation callsChannelHandlerContext.fireChannelInboundEvent(Object).- Throws:
Exception
-
newIdleStateEvent
protected IdleStateEvent newIdleStateEvent(IdleState state, boolean first)
Returns aIdleStateEvent.
-
-