Package io.netty.handler.timeout
Class ReadTimeoutHandler
java.lang.Object
io.netty.channel.ChannelHandlerAdapter
io.netty.channel.ChannelInboundHandlerAdapter
io.netty.channel.ChannelDuplexHandler
io.netty.handler.timeout.IdleStateHandler
io.netty.handler.timeout.ReadTimeoutHandler
- All Implemented Interfaces:
ChannelHandler
,ChannelInboundHandler
,ChannelOutboundHandler
Raises a
ReadTimeoutException
when no data was read within a certain
period of time.
// The connection is closed when there is no inbound traffic // for 30 seconds. public class MyChannelInitializer extendsChannelInitializer
<Channel
> { public void initChannel(Channel
channel) { channel.pipeline().addLast("readTimeoutHandler", newReadTimeoutHandler
(30)); channel.pipeline().addLast("myHandler", new MyHandler()); } } // Handler should handle theReadTimeoutException
. public class MyHandler extendsChannelDuplexHandler
{@Override
public void exceptionCaught(ChannelHandlerContext
ctx,Throwable
cause) throwsException
{ if (cause instanceofReadTimeoutException
) { // do something } else { super.exceptionCaught(ctx, cause); } } }ServerBootstrap
bootstrap = ...; ... bootstrap.childHandler(new MyChannelInitializer()); ...
- See Also:
-
Nested Class Summary
Nested classes/interfaces inherited from interface io.netty.channel.ChannelHandler
ChannelHandler.Sharable
-
Field Summary
Fields -
Constructor Summary
ConstructorsConstructorDescriptionReadTimeoutHandler
(int timeoutSeconds) Creates a new instance.ReadTimeoutHandler
(long timeout, TimeUnit unit) Creates a new instance. -
Method Summary
Modifier and TypeMethodDescriptionprotected final void
channelIdle
(ChannelHandlerContext ctx, IdleStateEvent evt) Is called when anIdleStateEvent
should be fired.protected void
Is called when a read timeout was detected.Methods inherited from class io.netty.handler.timeout.IdleStateHandler
channelActive, channelInactive, channelRead, channelReadComplete, channelRegistered, getAllIdleTimeInMillis, getReaderIdleTimeInMillis, getWriterIdleTimeInMillis, handlerAdded, handlerRemoved, newIdleStateEvent, resetReadTimeout, resetWriteTimeout, schedule, ticksInNanos, write
Methods inherited from class io.netty.channel.ChannelDuplexHandler
bind, close, connect, deregister, disconnect, flush, read
Methods inherited from class io.netty.channel.ChannelInboundHandlerAdapter
channelUnregistered, channelWritabilityChanged, exceptionCaught, userEventTriggered
Methods inherited from class io.netty.channel.ChannelHandlerAdapter
ensureNotSharable, isSharable
-
Field Details
-
closed
private boolean closed
-
-
Constructor Details
-
ReadTimeoutHandler
public ReadTimeoutHandler(int timeoutSeconds) Creates a new instance.- Parameters:
timeoutSeconds
- read timeout in seconds
-
ReadTimeoutHandler
Creates a new instance.- Parameters:
timeout
- read timeoutunit
- theTimeUnit
oftimeout
-
-
Method Details
-
channelIdle
Description copied from class:IdleStateHandler
Is called when anIdleStateEvent
should be fired. This implementation callsChannelHandlerContext.fireUserEventTriggered(Object)
.- Overrides:
channelIdle
in classIdleStateHandler
- Throws:
Exception
-
readTimedOut
Is called when a read timeout was detected.- Throws:
Exception
-