public class IdleStateHandler
extends io.netty.channel.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| Constructor and Description |
|---|
IdleStateHandler(boolean observeOutput,
long readerIdleTime,
long writerIdleTime,
long allIdleTime,
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,
TimeUnit unit) |
| Modifier and Type | Method and Description |
|---|---|
void |
channelActive(io.netty.channel.ChannelHandlerContext ctx) |
protected void |
channelIdle(io.netty.channel.ChannelHandlerContext ctx,
IdleStateEvent evt)
Is called when an
IdleStateEvent should be fired. |
void |
channelInactive(io.netty.channel.ChannelHandlerContext ctx) |
void |
channelRead(io.netty.channel.ChannelHandlerContext ctx,
Object msg) |
void |
channelReadComplete(io.netty.channel.ChannelHandlerContext ctx) |
void |
channelRegistered(io.netty.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.netty.channel.ChannelHandlerContext ctx) |
void |
handlerRemoved(io.netty.channel.ChannelHandlerContext ctx) |
protected IdleStateEvent |
newIdleStateEvent(IdleState state,
boolean first)
Returns a
IdleStateEvent. |
void |
write(io.netty.channel.ChannelHandlerContext ctx,
Object msg,
io.netty.channel.ChannelPromise promise) |
bind, close, connect, deregister, disconnect, flush, readchannelUnregistered, channelWritabilityChanged, exceptionCaught, userEventTriggeredpublic 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,
TimeUnit unit)
public IdleStateHandler(boolean observeOutput,
long readerIdleTime,
long writerIdleTime,
long allIdleTime,
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(io.netty.channel.ChannelHandlerContext ctx)
throws Exception
handlerAdded in interface io.netty.channel.ChannelHandlerhandlerAdded in class io.netty.channel.ChannelHandlerAdapterExceptionpublic void handlerRemoved(io.netty.channel.ChannelHandlerContext ctx)
throws Exception
handlerRemoved in interface io.netty.channel.ChannelHandlerhandlerRemoved in class io.netty.channel.ChannelHandlerAdapterExceptionpublic void channelRegistered(io.netty.channel.ChannelHandlerContext ctx)
throws Exception
channelRegistered in interface io.netty.channel.ChannelInboundHandlerchannelRegistered in class io.netty.channel.ChannelInboundHandlerAdapterExceptionpublic void channelActive(io.netty.channel.ChannelHandlerContext ctx)
throws Exception
channelActive in interface io.netty.channel.ChannelInboundHandlerchannelActive in class io.netty.channel.ChannelInboundHandlerAdapterExceptionpublic void channelInactive(io.netty.channel.ChannelHandlerContext ctx)
throws Exception
channelInactive in interface io.netty.channel.ChannelInboundHandlerchannelInactive in class io.netty.channel.ChannelInboundHandlerAdapterExceptionpublic void channelRead(io.netty.channel.ChannelHandlerContext ctx,
Object msg)
throws Exception
channelRead in interface io.netty.channel.ChannelInboundHandlerchannelRead in class io.netty.channel.ChannelInboundHandlerAdapterExceptionpublic void channelReadComplete(io.netty.channel.ChannelHandlerContext ctx)
throws Exception
channelReadComplete in interface io.netty.channel.ChannelInboundHandlerchannelReadComplete in class io.netty.channel.ChannelInboundHandlerAdapterExceptionpublic void write(io.netty.channel.ChannelHandlerContext ctx,
Object msg,
io.netty.channel.ChannelPromise promise)
throws Exception
write in interface io.netty.channel.ChannelOutboundHandlerwrite in class io.netty.channel.ChannelDuplexHandlerExceptionprotected void channelIdle(io.netty.channel.ChannelHandlerContext ctx,
IdleStateEvent evt)
throws Exception
IdleStateEvent should be fired. This implementation calls
ChannelHandlerContext.fireUserEventTriggered(Object).Exceptionprotected IdleStateEvent newIdleStateEvent(IdleState state, boolean first)
IdleStateEvent.Copyright © 2008–2019 The Netty Project. All rights reserved.