@ChannelPipelineCoverage(value="one") 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 traffic // (either inbound or outbound) for 30 seconds. ChannelPipeline p = ...; Timer timer = new HashedWheelTimer(); p.addLast("timeout", new IdleStateHandler(timer, 30, 30, 0)); p.addLast("handler", new MyHandler()); // Handler should handle the IdleStateEvent triggered by IdleStateHandler. public class MyHandler extends IdleStateAwareChannelHandler { public void channelIdle(ChannelHandlerContext ctx, IdleStateEvent e) { ctx.getChannel().write(new PingMessage()); } } // To shut down, callreleaseExternalResources()
orTimer.stop()
.
ReadTimeoutHandler
,
WriteTimeoutHandler
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,
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. |
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. |
void |
writeComplete(ChannelHandlerContext ctx,
WriteCompletionEvent e)
Invoked when something was written into a
Channel . |
channelBound, channelConnected, channelDisconnected, channelInterestChanged, channelUnbound, childChannelClosed, childChannelOpen, exceptionCaught, handleUpstream
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, 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 allIdleTime
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 ExternalResourceReleasable
public void beforeAdd(ChannelHandlerContext ctx) throws Exception
beforeAdd
in interface LifeCycleAwareChannelHandler
Exception
public void afterAdd(ChannelHandlerContext ctx) throws Exception
afterAdd
in interface LifeCycleAwareChannelHandler
Exception
public void beforeRemove(ChannelHandlerContext ctx) throws Exception
beforeRemove
in interface LifeCycleAwareChannelHandler
Exception
public void afterRemove(ChannelHandlerContext ctx) throws Exception
afterRemove
in interface LifeCycleAwareChannelHandler
Exception
public void channelOpen(ChannelHandlerContext ctx, ChannelStateEvent e) throws Exception
SimpleChannelUpstreamHandler
Channel
is open, but not bound nor connected.channelOpen
in class SimpleChannelUpstreamHandler
Exception
public void channelClosed(ChannelHandlerContext ctx, ChannelStateEvent e) throws Exception
SimpleChannelUpstreamHandler
Channel
was closed and all its related resources
were released.channelClosed
in class SimpleChannelUpstreamHandler
Exception
public void messageReceived(ChannelHandlerContext ctx, MessageEvent e) throws Exception
SimpleChannelUpstreamHandler
ChannelBuffer
) was received
from a remote peer.messageReceived
in class SimpleChannelUpstreamHandler
Exception
public void writeComplete(ChannelHandlerContext ctx, WriteCompletionEvent e) throws Exception
SimpleChannelUpstreamHandler
Channel
.writeComplete
in class SimpleChannelUpstreamHandler
Exception
protected void channelIdle(ChannelHandlerContext ctx, IdleState state, long lastActivityTimeMillis) throws Exception
Exception
Copyright © 2008-2013 JBoss, by Red Hat. All Rights Reserved.