@ChannelHandler.Sharable public class IdleStateHandler extends SimpleChannelUpstreamHandler implements LifeCycleAwareChannelHandler, ExternalResourceReleasable
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 MyPipelineFactory implementsTheChannelPipelineFactory{ private finalTimertimer; private finalChannelHandleridleStateHandler; public MyPipelineFactory(Timertimer) { this.timer = timer; this.idleStateHandler = newIdleStateHandler(timer, 60, 30, 0), // timer must be shared. } publicChannelPipelinegetPipeline() { returnChannels.pipeline( idleStateHandler, new MyHandler()); } } // Handler should handle theIdleStateEventtriggered byIdleStateHandler. public class MyHandler extendsIdleStateAwareChannelHandler{@Overridepublic void channelIdle(ChannelHandlerContextctx,IdleStateEvente) { if (e.getState() ==IdleState.READER_IDLE) { e.getChannel().close(); } else if (e.getState() ==IdleState.WRITER_IDLE) { e.getChannel().write(new PingMessage()); } } }ServerBootstrapbootstrap = ...;Timertimer = newHashedWheelTimer(); ... bootstrap.setPipelineFactory(new MyPipelineFactory(timer)); ...
Timer which was specified when the IdleStateHandler is
created should be stopped manually by calling releaseExternalResources()
or Timer.stop() when your application shuts down.ReadTimeoutHandler,
WriteTimeoutHandler| Modifier and Type | Class and Description |
|---|---|
private class |
IdleStateHandler.AllIdleTimeoutTask |
private class |
IdleStateHandler.ReaderIdleTimeoutTask |
private static class |
IdleStateHandler.State |
private class |
IdleStateHandler.WriterIdleTimeoutTask |
ChannelHandler.Sharable| Modifier and Type | Field and Description |
|---|---|
(package private) long |
allIdleTimeMillis |
(package private) long |
readerIdleTimeMillis |
(package private) Timer |
timer |
(package private) long |
writerIdleTimeMillis |
| Constructor and Description |
|---|
IdleStateHandler(Timer timer,
int readerIdleTimeSeconds,
int writerIdleTimeSeconds,
int allIdleTimeSeconds)
Creates a new instance.
|
IdleStateHandler(Timer timer,
long readerIdleTime,
long writerIdleTime,
long allIdleTime,
java.util.concurrent.TimeUnit unit)
Creates a new instance.
|
| Modifier and Type | Method and Description |
|---|---|
void |
afterAdd(ChannelHandlerContext ctx) |
void |
afterRemove(ChannelHandlerContext ctx) |
void |
beforeAdd(ChannelHandlerContext ctx) |
void |
beforeRemove(ChannelHandlerContext ctx) |
void |
channelClosed(ChannelHandlerContext ctx,
ChannelStateEvent e)
Invoked when a
Channel was closed and all its related resources
were released. |
protected void |
channelIdle(ChannelHandlerContext ctx,
IdleState state,
long lastActivityTimeMillis) |
void |
channelOpen(ChannelHandlerContext ctx,
ChannelStateEvent e)
Invoked when a
Channel is open, but not bound nor connected. |
private static void |
destroy(ChannelHandlerContext ctx) |
private void |
fireChannelIdle(ChannelHandlerContext ctx,
IdleState state,
long lastActivityTimeMillis) |
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.
|
private void |
initialize(ChannelHandlerContext ctx) |
void |
messageReceived(ChannelHandlerContext ctx,
MessageEvent e)
Invoked when a message object (e.g:
ChannelBuffer) was received
from a remote peer. |
void |
releaseExternalResources()
Stops the
Timer which was specified in the constructor of this
handler. |
private static IdleStateHandler.State |
state(ChannelHandlerContext ctx) |
void |
writeComplete(ChannelHandlerContext ctx,
WriteCompletionEvent e)
Invoked when something was written into a
Channel. |
channelBound, channelConnected, channelDisconnected, channelInterestChanged, channelUnbound, childChannelClosed, childChannelOpen, exceptionCaught, handleUpstreamfinal Timer timer
final long readerIdleTimeMillis
final long writerIdleTimeMillis
final long allIdleTimeMillis
public IdleStateHandler(Timer timer, int readerIdleTimeSeconds, int writerIdleTimeSeconds, int allIdleTimeSeconds)
timer - the Timer that is used to trigger the scheduled event.
The recommended Timer implementation is HashedWheelTimer.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(Timer timer, long readerIdleTime, long writerIdleTime, long allIdleTime, java.util.concurrent.TimeUnit unit)
timer - the Timer that is used to trigger the scheduled event.
The recommended Timer implementation is HashedWheelTimer.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 releaseExternalResources()
Timer which was specified in the constructor of this
handler. You should not call this method if the Timer is in use
by other objects.releaseExternalResources in interface ExternalResourceReleasablepublic void beforeAdd(ChannelHandlerContext ctx) throws java.lang.Exception
beforeAdd in interface LifeCycleAwareChannelHandlerjava.lang.Exceptionpublic void afterAdd(ChannelHandlerContext ctx) throws java.lang.Exception
afterAdd in interface LifeCycleAwareChannelHandlerjava.lang.Exceptionpublic void beforeRemove(ChannelHandlerContext ctx) throws java.lang.Exception
beforeRemove in interface LifeCycleAwareChannelHandlerjava.lang.Exceptionpublic void afterRemove(ChannelHandlerContext ctx) throws java.lang.Exception
afterRemove in interface LifeCycleAwareChannelHandlerjava.lang.Exceptionpublic void channelOpen(ChannelHandlerContext ctx, ChannelStateEvent e) throws java.lang.Exception
SimpleChannelUpstreamHandlerChannel is open, but not bound nor connected.
channelOpen in class SimpleChannelUpstreamHandlerjava.lang.Exceptionpublic void channelClosed(ChannelHandlerContext ctx, ChannelStateEvent e) throws java.lang.Exception
SimpleChannelUpstreamHandlerChannel was closed and all its related resources
were released.channelClosed in class SimpleChannelUpstreamHandlerjava.lang.Exceptionpublic void messageReceived(ChannelHandlerContext ctx, MessageEvent e) throws java.lang.Exception
SimpleChannelUpstreamHandlerChannelBuffer) was received
from a remote peer.messageReceived in class SimpleChannelUpstreamHandlerjava.lang.Exceptionpublic void writeComplete(ChannelHandlerContext ctx, WriteCompletionEvent e) throws java.lang.Exception
SimpleChannelUpstreamHandlerChannel.writeComplete in class SimpleChannelUpstreamHandlerjava.lang.Exceptionprivate void initialize(ChannelHandlerContext ctx)
private static void destroy(ChannelHandlerContext ctx)
private static IdleStateHandler.State state(ChannelHandlerContext ctx)
private void fireChannelIdle(ChannelHandlerContext ctx, IdleState state, long lastActivityTimeMillis)
protected void channelIdle(ChannelHandlerContext ctx, IdleState state, long lastActivityTimeMillis) throws java.lang.Exception
java.lang.Exception