- 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 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 implementsChannelHandler
{@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
-
-
Constructor Summary
Constructors Constructor Description IdleStateHandler(int readerIdleTimeSeconds, int writerIdleTimeSeconds, int allIdleTimeSeconds)
Creates a new instance firingIdleStateEvent
s.IdleStateHandler(long readerIdleTime, long writerIdleTime, long allIdleTime, TimeUnit unit)
Creates a new instance firingIdleStateEvent
s.
-
Method Summary
All Methods Instance Methods Concrete Methods Modifier and Type Method Description void
channelActive(io.netty5.channel.ChannelHandlerContext ctx)
protected void
channelIdle(io.netty5.channel.ChannelHandlerContext ctx, IdleStateEvent evt)
Is called when anIdleStateEvent
should be fired.void
channelInactive(io.netty5.channel.ChannelHandlerContext ctx)
void
channelRead(io.netty5.channel.ChannelHandlerContext ctx, Object msg)
void
channelReadComplete(io.netty5.channel.ChannelHandlerContext ctx)
void
channelRegistered(io.netty5.channel.ChannelHandlerContext ctx)
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(io.netty5.channel.ChannelHandlerContext ctx)
void
handlerRemoved(io.netty5.channel.ChannelHandlerContext ctx)
protected IdleStateEvent
newIdleStateEvent(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 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, TimeUnit unit)
Creates a new instance firingIdleStateEvent
s.- Parameters:
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(io.netty5.channel.ChannelHandlerContext ctx) throws Exception
- Specified by:
handlerAdded
in interfaceio.netty5.channel.ChannelHandler
- Throws:
Exception
-
handlerRemoved
public void handlerRemoved(io.netty5.channel.ChannelHandlerContext ctx) throws Exception
- Specified by:
handlerRemoved
in interfaceio.netty5.channel.ChannelHandler
- Throws:
Exception
-
channelRegistered
public void channelRegistered(io.netty5.channel.ChannelHandlerContext ctx) throws Exception
- Specified by:
channelRegistered
in interfaceio.netty5.channel.ChannelHandler
- Throws:
Exception
-
channelActive
public void channelActive(io.netty5.channel.ChannelHandlerContext ctx) throws Exception
- Specified by:
channelActive
in interfaceio.netty5.channel.ChannelHandler
- Throws:
Exception
-
channelInactive
public void channelInactive(io.netty5.channel.ChannelHandlerContext ctx) throws Exception
- Specified by:
channelInactive
in interfaceio.netty5.channel.ChannelHandler
- Throws:
Exception
-
channelRead
public void channelRead(io.netty5.channel.ChannelHandlerContext ctx, Object msg) throws Exception
- Specified by:
channelRead
in interfaceio.netty5.channel.ChannelHandler
- Throws:
Exception
-
channelReadComplete
public void channelReadComplete(io.netty5.channel.ChannelHandlerContext ctx) throws Exception
- Specified by:
channelReadComplete
in interfaceio.netty5.channel.ChannelHandler
- Throws:
Exception
-
write
public io.netty5.util.concurrent.Future<Void> write(io.netty5.channel.ChannelHandlerContext ctx, Object msg)
- Specified by:
write
in interfaceio.netty5.channel.ChannelHandler
-
channelIdle
protected void channelIdle(io.netty5.channel.ChannelHandlerContext ctx, IdleStateEvent evt) throws Exception
Is called when anIdleStateEvent
should be fired. This implementation callsChannelHandlerContext.fireChannelInboundEvent(Object)
.- Throws:
Exception
-
newIdleStateEvent
protected IdleStateEvent newIdleStateEvent(IdleState state, boolean first)
Returns aIdleStateEvent
.
-
-