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()); ...
ChannelHandler.Sharable| 构造器和说明 |
|---|
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) |
bind, close, connect, deregister, disconnect, flush, readchannelUnregistered, channelWritabilityChanged, exceptionCaught, userEventTriggeredensureNotSharable, isSharableclone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, waitexceptionCaughtpublic 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 在接口中 ChannelHandlerhandlerAdded 在类中 ChannelHandlerAdapterjava.lang.Exceptionpublic void handlerRemoved(ChannelHandlerContext ctx) throws java.lang.Exception
ChannelHandlerAdapterhandlerRemoved 在接口中 ChannelHandlerhandlerRemoved 在类中 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 在接口中 ChannelInboundHandlerchannelRegistered 在类中 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 在接口中 ChannelInboundHandlerchannelActive 在类中 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 在接口中 ChannelInboundHandlerchannelInactive 在类中 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 在接口中 ChannelInboundHandlerchannelRead 在类中 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 在接口中 ChannelInboundHandlerchannelReadComplete 在类中 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 在接口中 ChannelOutboundHandlerwrite 在类中 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 occurspublic void resetReadTimeout()
public void resetWriteTimeout()
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.