public class IdleStateHandler extends ChannelDuplexHandler
IdleStateEvent when a Channel has not performed
read, write, or both operation for a while.
| Property | Meaning |
|---|---|
readerIdleTime |
an IdleStateEvent whose state is IdleState.READER_IDLE
will be triggered when no read was performed for the specified period of
time. Specify 0 to disable. |
writerIdleTime |
an IdleStateEvent whose state is IdleState.WRITER_IDLE
will be triggered when no write was performed for the specified period of
time. Specify 0 to disable. |
allIdleTime |
an IdleStateEvent whose state is IdleState.ALL_IDLE
will be triggered when neither read nor write was performed for the
specified period of time. Specify 0 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 extendsChannelInitializer<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 extendsChannelDuplexHandler{@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()); ...
ReadTimeoutHandler,
WriteTimeoutHandler| Modifier and Type | Class and Description |
|---|---|
private static class |
IdleStateHandler.AbstractIdleTask |
private class |
IdleStateHandler.AllIdleTimeoutTask |
private class |
IdleStateHandler.ReaderIdleTimeoutTask |
private class |
IdleStateHandler.WriterIdleTimeoutTask |
ChannelHandler.Sharable| Modifier and Type | Field and Description |
|---|---|
private long |
allIdleTimeNanos |
private java.util.concurrent.ScheduledFuture<?> |
allIdleTimeout |
private boolean |
firstAllIdleEvent |
private boolean |
firstReaderIdleEvent |
private boolean |
firstWriterIdleEvent |
private long |
lastChangeCheckTimeStamp |
private int |
lastMessageHashCode |
private long |
lastPendingWriteBytes |
private long |
lastReadTime |
private long |
lastWriteTime |
private static long |
MIN_TIMEOUT_NANOS |
private boolean |
observeOutput |
private long |
readerIdleTimeNanos |
private java.util.concurrent.ScheduledFuture<?> |
readerIdleTimeout |
private boolean |
reading |
private byte |
state |
private ChannelFutureListener |
writeListener |
private long |
writerIdleTimeNanos |
private java.util.concurrent.ScheduledFuture<?> |
writerIdleTimeout |
| Constructor and Description |
|---|
IdleStateHandler(boolean observeOutput,
long readerIdleTime,
long writerIdleTime,
long allIdleTime,
java.util.concurrent.TimeUnit unit)
Creates a new instance firing
IdleStateEvents. |
IdleStateHandler(int readerIdleTimeSeconds,
int writerIdleTimeSeconds,
int allIdleTimeSeconds)
Creates a new instance firing
IdleStateEvents. |
IdleStateHandler(long readerIdleTime,
long writerIdleTime,
long allIdleTime,
java.util.concurrent.TimeUnit unit) |
| Modifier and Type | Method and Description |
|---|---|
void |
channelActive(ChannelHandlerContext ctx)
Calls
ChannelHandlerContext.fireChannelActive() to forward
to the next ChannelInboundHandler in the ChannelPipeline. |
protected void |
channelIdle(ChannelHandlerContext ctx,
IdleStateEvent evt)
Is called when an
IdleStateEvent should be fired. |
void |
channelInactive(ChannelHandlerContext ctx)
Calls
ChannelHandlerContext.fireChannelInactive() to forward
to the next ChannelInboundHandler in the ChannelPipeline. |
void |
channelRead(ChannelHandlerContext ctx,
java.lang.Object msg)
Calls
ChannelHandlerContext.fireChannelRead(Object) to forward
to the next ChannelInboundHandler in the ChannelPipeline. |
void |
channelReadComplete(ChannelHandlerContext ctx)
Calls
ChannelHandlerContext.fireChannelReadComplete() to forward
to the next ChannelInboundHandler in the ChannelPipeline. |
void |
channelRegistered(ChannelHandlerContext ctx)
Calls
ChannelHandlerContext.fireChannelRegistered() to forward
to the next ChannelInboundHandler in the ChannelPipeline. |
private void |
destroy() |
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)
Do nothing by default, sub-classes may override this method.
|
void |
handlerRemoved(ChannelHandlerContext ctx)
Do nothing by default, sub-classes may override this method.
|
private boolean |
hasOutputChanged(ChannelHandlerContext ctx,
boolean first)
Returns
true if and only if the IdleStateHandler was constructed
with observeOutput enabled and there has been an observed change in the
ChannelOutboundBuffer between two consecutive calls of this method. |
private void |
initialize(ChannelHandlerContext ctx) |
private void |
initOutputChanged(ChannelHandlerContext ctx) |
protected IdleStateEvent |
newIdleStateEvent(IdleState state,
boolean first)
Returns a
IdleStateEvent. |
(package private) java.util.concurrent.ScheduledFuture<?> |
schedule(ChannelHandlerContext ctx,
java.lang.Runnable task,
long delay,
java.util.concurrent.TimeUnit unit)
This method is visible for testing!
|
(package private) long |
ticksInNanos()
This method is visible for testing!
|
void |
write(ChannelHandlerContext ctx,
java.lang.Object msg,
ChannelPromise promise)
Calls
ChannelOutboundInvoker.write(Object, ChannelPromise) to forward
to the next ChannelOutboundHandler in the ChannelPipeline. |
bind, close, connect, deregister, disconnect, flush, readchannelUnregistered, channelWritabilityChanged, exceptionCaught, userEventTriggeredensureNotSharable, isSharableclone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, waitexceptionCaughtprivate static final long MIN_TIMEOUT_NANOS
private final ChannelFutureListener writeListener
private final boolean observeOutput
private final long readerIdleTimeNanos
private final long writerIdleTimeNanos
private final long allIdleTimeNanos
private java.util.concurrent.ScheduledFuture<?> readerIdleTimeout
private long lastReadTime
private boolean firstReaderIdleEvent
private java.util.concurrent.ScheduledFuture<?> writerIdleTimeout
private long lastWriteTime
private boolean firstWriterIdleEvent
private java.util.concurrent.ScheduledFuture<?> allIdleTimeout
private boolean firstAllIdleEvent
private byte state
private boolean reading
private long lastChangeCheckTimeStamp
private int lastMessageHashCode
private long lastPendingWriteBytes
public IdleStateHandler(int readerIdleTimeSeconds,
int writerIdleTimeSeconds,
int allIdleTimeSeconds)
IdleStateEvents.readerIdleTimeSeconds - an IdleStateEvent whose state is IdleState.READER_IDLE
will be triggered when no read was performed for the specified
period of time. Specify 0 to disable.writerIdleTimeSeconds - an IdleStateEvent whose state is IdleState.WRITER_IDLE
will be triggered when no write was performed for the specified
period of time. Specify 0 to disable.allIdleTimeSeconds - an IdleStateEvent whose state is IdleState.ALL_IDLE
will be triggered when neither read nor write was performed for
the specified period of time. Specify 0 to disable.public IdleStateHandler(long readerIdleTime,
long writerIdleTime,
long allIdleTime,
java.util.concurrent.TimeUnit unit)
public IdleStateHandler(boolean observeOutput,
long readerIdleTime,
long writerIdleTime,
long allIdleTime,
java.util.concurrent.TimeUnit unit)
IdleStateEvents.observeOutput - whether or not the consumption of bytes should be taken into
consideration when assessing write idleness. The default is false.readerIdleTime - an IdleStateEvent whose state is IdleState.READER_IDLE
will be triggered when no read was performed for the specified
period of time. Specify 0 to disable.writerIdleTime - an IdleStateEvent whose state is IdleState.WRITER_IDLE
will be triggered when no write was performed for the specified
period of time. Specify 0 to disable.allIdleTime - an IdleStateEvent whose state is IdleState.ALL_IDLE
will be triggered when neither read nor write was performed for
the specified period of time. Specify 0 to disable.unit - the TimeUnit of readerIdleTime,
writeIdleTime, and allIdleTimepublic long getReaderIdleTimeInMillis()
public long getWriterIdleTimeInMillis()
public long getAllIdleTimeInMillis()
public void handlerAdded(ChannelHandlerContext ctx) throws java.lang.Exception
ChannelHandlerAdapterhandlerAdded in interface ChannelHandlerhandlerAdded in class ChannelHandlerAdapterjava.lang.Exceptionpublic void handlerRemoved(ChannelHandlerContext ctx) throws java.lang.Exception
ChannelHandlerAdapterhandlerRemoved in interface ChannelHandlerhandlerRemoved in class ChannelHandlerAdapterjava.lang.Exceptionpublic void channelRegistered(ChannelHandlerContext ctx) throws java.lang.Exception
ChannelInboundHandlerAdapterChannelHandlerContext.fireChannelRegistered() to forward
to the next ChannelInboundHandler in the ChannelPipeline.
Sub-classes may override this method to change behavior.channelRegistered in interface ChannelInboundHandlerchannelRegistered in class ChannelInboundHandlerAdapterjava.lang.Exceptionpublic void channelActive(ChannelHandlerContext ctx) throws java.lang.Exception
ChannelInboundHandlerAdapterChannelHandlerContext.fireChannelActive() to forward
to the next ChannelInboundHandler in the ChannelPipeline.
Sub-classes may override this method to change behavior.channelActive in interface ChannelInboundHandlerchannelActive in class ChannelInboundHandlerAdapterjava.lang.Exceptionpublic void channelInactive(ChannelHandlerContext ctx) throws java.lang.Exception
ChannelInboundHandlerAdapterChannelHandlerContext.fireChannelInactive() to forward
to the next ChannelInboundHandler in the ChannelPipeline.
Sub-classes may override this method to change behavior.channelInactive in interface ChannelInboundHandlerchannelInactive in class ChannelInboundHandlerAdapterjava.lang.Exceptionpublic void channelRead(ChannelHandlerContext ctx, java.lang.Object msg) throws java.lang.Exception
ChannelInboundHandlerAdapterChannelHandlerContext.fireChannelRead(Object) to forward
to the next ChannelInboundHandler in the ChannelPipeline.
Sub-classes may override this method to change behavior.channelRead in interface ChannelInboundHandlerchannelRead in class ChannelInboundHandlerAdapterjava.lang.Exceptionpublic void channelReadComplete(ChannelHandlerContext ctx) throws java.lang.Exception
ChannelInboundHandlerAdapterChannelHandlerContext.fireChannelReadComplete() to forward
to the next ChannelInboundHandler in the ChannelPipeline.
Sub-classes may override this method to change behavior.channelReadComplete in interface ChannelInboundHandlerchannelReadComplete in class ChannelInboundHandlerAdapterjava.lang.Exceptionpublic void write(ChannelHandlerContext ctx, java.lang.Object msg, ChannelPromise promise) throws java.lang.Exception
ChannelDuplexHandlerChannelOutboundInvoker.write(Object, ChannelPromise) to forward
to the next ChannelOutboundHandler in the ChannelPipeline.
Sub-classes may override this method to change behavior.write in interface ChannelOutboundHandlerwrite in class ChannelDuplexHandlerctx - the ChannelHandlerContext for which the write operation is mademsg - the message to writepromise - the ChannelPromise to notify once the operation completesjava.lang.Exception - thrown if an error occursprivate void initialize(ChannelHandlerContext ctx)
long ticksInNanos()
java.util.concurrent.ScheduledFuture<?> schedule(ChannelHandlerContext ctx, java.lang.Runnable task, long delay, java.util.concurrent.TimeUnit unit)
private void destroy()
protected void channelIdle(ChannelHandlerContext ctx, IdleStateEvent evt) throws java.lang.Exception
IdleStateEvent should be fired. This implementation calls
ChannelHandlerContext.fireUserEventTriggered(Object).java.lang.Exceptionprotected IdleStateEvent newIdleStateEvent(IdleState state, boolean first)
IdleStateEvent.private void initOutputChanged(ChannelHandlerContext ctx)
private boolean hasOutputChanged(ChannelHandlerContext ctx, boolean first)
true if and only if the IdleStateHandler was constructed
with observeOutput enabled and there has been an observed change in the
ChannelOutboundBuffer between two consecutive calls of this method.
https://github.com/netty/netty/issues/6150