- java.lang.Object
-
- io.netty5.handler.timeout.IdleStateHandler
-
- All Implemented Interfaces:
ChannelHandler
- Direct Known Subclasses:
ReadTimeoutHandler
public class IdleStateHandler extends Object implements 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(ChannelHandlerContext ctx)
TheChannel
of theChannelHandlerContext
is now activeprotected void
channelIdle(ChannelHandlerContext ctx, IdleStateEvent evt)
Is called when anIdleStateEvent
should be fired.void
channelInactive(ChannelHandlerContext ctx)
TheChannel
of theChannelHandlerContext
was registered is now inactive and reached its end of lifetime.void
channelRead(ChannelHandlerContext ctx, Object msg)
Invoked when the currentChannel
has read a message from the peer.void
channelReadComplete(ChannelHandlerContext ctx)
Invoked when the last message read by the current read operation has been consumed byChannelHandler.channelRead(ChannelHandlerContext, Object)
.void
channelRegistered(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(ChannelHandlerContext ctx)
Gets called after theChannelHandler
was added to the actual context and it's ready to handle events.void
handlerRemoved(ChannelHandlerContext ctx)
Gets called after theChannelHandler
was removed from the actual context and it doesn't handle events anymore.protected IdleStateEvent
newIdleStateEvent(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 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(ChannelHandlerContext ctx) throws Exception
Description copied from interface:ChannelHandler
Gets called after theChannelHandler
was added to the actual context and it's ready to handle events.- Specified by:
handlerAdded
in interfaceChannelHandler
- Throws:
Exception
-
handlerRemoved
public void handlerRemoved(ChannelHandlerContext ctx) throws Exception
Description copied from interface:ChannelHandler
Gets called after theChannelHandler
was removed from the actual context and it doesn't handle events anymore.- Specified by:
handlerRemoved
in interfaceChannelHandler
- Throws:
Exception
-
channelRegistered
public void channelRegistered(ChannelHandlerContext ctx) throws Exception
Description copied from interface:ChannelHandler
- Specified by:
channelRegistered
in interfaceChannelHandler
- Throws:
Exception
-
channelActive
public void channelActive(ChannelHandlerContext ctx) throws Exception
Description copied from interface:ChannelHandler
TheChannel
of theChannelHandlerContext
is now active- Specified by:
channelActive
in interfaceChannelHandler
- Throws:
Exception
-
channelInactive
public void channelInactive(ChannelHandlerContext ctx) throws Exception
Description copied from interface:ChannelHandler
TheChannel
of theChannelHandlerContext
was registered is now inactive and reached its end of lifetime.- Specified by:
channelInactive
in interfaceChannelHandler
- Throws:
Exception
-
channelRead
public void channelRead(ChannelHandlerContext ctx, Object msg) throws Exception
Description copied from interface:ChannelHandler
Invoked when the currentChannel
has read a message from the peer.- Specified by:
channelRead
in interfaceChannelHandler
- Throws:
Exception
-
channelReadComplete
public void channelReadComplete(ChannelHandlerContext ctx) throws Exception
Description copied from interface:ChannelHandler
Invoked when the last message read by the current read operation has been consumed byChannelHandler.channelRead(ChannelHandlerContext, Object)
. IfChannelOption.AUTO_READ
is off, no further attempt to read an inbound data from the currentChannel
will be made untilChannelHandlerContext.read()
is called.- Specified by:
channelReadComplete
in interfaceChannelHandler
- Throws:
Exception
-
write
public Future<Void> write(ChannelHandlerContext ctx, Object msg)
Description copied from interface:ChannelHandler
Called once a write operation is made. The write operation will write the messages through theChannelPipeline
. Those are then ready to be flushed to the actualChannel
onceChannel.flush()
is called.- Specified by:
write
in interfaceChannelHandler
- Parameters:
ctx
- theChannelHandlerContext
for which the write operation is mademsg
- the message to write- Returns:
- the
Future
which will be notified once the operation completes.
-
channelIdle
protected void channelIdle(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
.
-
-