public class IdleStateHandler extends ChannelHandlerAdapter
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
> {@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 extendsChannelHandlerAdapter
{@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()); ...
ReadTimeoutHandler
,
WriteTimeoutHandler
ChannelHandler.Sharable, ChannelHandler.Skip
Constructor and Description |
---|
IdleStateHandler(int readerIdleTimeSeconds,
int writerIdleTimeSeconds,
int allIdleTimeSeconds)
Creates a new instance firing
IdleStateEvent s. |
IdleStateHandler(long readerIdleTime,
long writerIdleTime,
long allIdleTime,
TimeUnit unit)
Creates a new instance firing
IdleStateEvent s. |
Modifier and Type | Method and Description |
---|---|
void |
channelActive(ChannelHandlerContext ctx)
Calls
ChannelHandlerContext.fireChannelActive() to forward
to the next ChannelHandler 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 ChannelHandler in the ChannelPipeline . |
void |
channelRead(ChannelHandlerContext ctx,
Object msg)
Calls
ChannelHandlerContext.fireChannelRead(Object) to forward
to the next ChannelHandler in the ChannelPipeline . |
void |
channelRegistered(ChannelHandlerContext ctx)
Calls
ChannelHandlerContext.fireChannelRegistered() to forward
to the next ChannelHandler in the ChannelPipeline . |
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.
|
void |
write(ChannelHandlerContext ctx,
Object msg,
ChannelPromise promise)
Calls
ChannelHandlerContext.write(Object) to forward
to the next ChannelHandler in the ChannelPipeline . |
bind, channelReadComplete, channelUnregistered, channelWritabilityChanged, close, connect, deregister, disconnect, exceptionCaught, flush, isSharable, read, userEventTriggered
public IdleStateHandler(int readerIdleTimeSeconds, int writerIdleTimeSeconds, int allIdleTimeSeconds)
IdleStateEvent
s.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)
IdleStateEvent
s.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 allIdleTime
public long getReaderIdleTimeInMillis()
public long getWriterIdleTimeInMillis()
public long getAllIdleTimeInMillis()
public void handlerAdded(ChannelHandlerContext ctx) throws Exception
ChannelHandlerAdapter
handlerAdded
in interface ChannelHandler
handlerAdded
in class ChannelHandlerAdapter
Exception
public void handlerRemoved(ChannelHandlerContext ctx) throws Exception
ChannelHandlerAdapter
handlerRemoved
in interface ChannelHandler
handlerRemoved
in class ChannelHandlerAdapter
Exception
public void channelRegistered(ChannelHandlerContext ctx) throws Exception
ChannelHandlerAdapter
ChannelHandlerContext.fireChannelRegistered()
to forward
to the next ChannelHandler
in the ChannelPipeline
.
Sub-classes may override this method to change behavior.channelRegistered
in interface ChannelHandler
channelRegistered
in class ChannelHandlerAdapter
Exception
public void channelActive(ChannelHandlerContext ctx) throws Exception
ChannelHandlerAdapter
ChannelHandlerContext.fireChannelActive()
to forward
to the next ChannelHandler
in the ChannelPipeline
.
Sub-classes may override this method to change behavior.channelActive
in interface ChannelHandler
channelActive
in class ChannelHandlerAdapter
Exception
public void channelInactive(ChannelHandlerContext ctx) throws Exception
ChannelHandlerAdapter
ChannelHandlerContext.fireChannelInactive()
to forward
to the next ChannelHandler
in the ChannelPipeline
.
Sub-classes may override this method to change behavior.channelInactive
in interface ChannelHandler
channelInactive
in class ChannelHandlerAdapter
Exception
public void channelRead(ChannelHandlerContext ctx, Object msg) throws Exception
ChannelHandlerAdapter
ChannelHandlerContext.fireChannelRead(Object)
to forward
to the next ChannelHandler
in the ChannelPipeline
.
Sub-classes may override this method to change behavior.channelRead
in interface ChannelHandler
channelRead
in class ChannelHandlerAdapter
Exception
public void write(ChannelHandlerContext ctx, Object msg, ChannelPromise promise) throws Exception
ChannelHandlerAdapter
ChannelHandlerContext.write(Object)
to forward
to the next ChannelHandler
in the ChannelPipeline
.
Sub-classes may override this method to change behavior.write
in interface ChannelHandler
write
in class ChannelHandlerAdapter
ctx
- the ChannelHandlerContext
for which the write operation is mademsg
- the message to writepromise
- the ChannelPromise
to notify once the operation completesException
- thrown if an error accourprotected void channelIdle(ChannelHandlerContext ctx, IdleStateEvent evt) throws Exception
IdleStateEvent
should be fired. This implementation calls
ChannelHandlerContext.fireUserEventTriggered(Object)
.Exception
Copyright © 2008–2015 The Netty Project. All rights reserved.