This commit is contained in:
Trustin Lee 2009-06-19 11:17:38 +00:00
parent f19cfb45e9
commit c630a6ea65
12 changed files with 165 additions and 3 deletions

View File

@ -32,6 +32,8 @@ import org.jboss.netty.channel.Channel;
import org.jboss.netty.channel.ChannelFuture;
/**
* The default {@link IdleStateEvent} implementation.
*
* @author The Netty Project (netty-dev@lists.jboss.org)
* @author Trustin Lee (tlee@redhat.com)
* @version $Rev$, $Date$
@ -42,6 +44,9 @@ public class DefaultIdleStateEvent implements IdleStateEvent {
private final IdleState state;
private final long lastActivityTimeMillis;
/**
* Creates a new instance.
*/
public DefaultIdleStateEvent(
Channel channel, IdleState state, long lastActivityTimeMillis) {
if (channel == null) {

View File

@ -22,14 +22,27 @@
*/
package org.jboss.netty.handler.timeout;
import org.jboss.netty.channel.Channel;
/**
* An {@link Enum} that represents the idle state of a {@link Channel}.
*
* @author The Netty Project (netty-dev@lists.jboss.org)
* @author Trustin Lee (tlee@redhat.com)
* @version $Rev$, $Date$
*/
public enum IdleState {
/**
* No data was received for a while.
*/
READER_IDLE,
/**
* No data was sent for a while.
*/
WRITER_IDLE,
/**
* No data was either received or sent for a while.
*/
ALL_IDLE;
}

View File

@ -22,11 +22,15 @@
*/
package org.jboss.netty.handler.timeout;
import org.jboss.netty.channel.Channel;
import org.jboss.netty.channel.ChannelEvent;
import org.jboss.netty.channel.ChannelHandlerContext;
import org.jboss.netty.channel.SimpleChannelHandler;
/**
* An extended {@link SimpleChannelHandler} that adds the handler method for
* an {@link IdleStateEvent}.
*
* @author The Netty Project (netty-dev@lists.jboss.org)
* @author Trustin Lee (tlee@redhat.com)
* @version $Rev$, $Date$
@ -43,6 +47,9 @@ public class IdleStateAwareChannelHandler extends SimpleChannelHandler {
}
}
/**
* Invoked when a {@link Channel} has been idle for a while.
*/
public void channelIdle(ChannelHandlerContext ctx, IdleStateEvent e) {
ctx.sendUpstream(e);
}

View File

@ -22,11 +22,15 @@
*/
package org.jboss.netty.handler.timeout;
import org.jboss.netty.channel.Channel;
import org.jboss.netty.channel.ChannelEvent;
import org.jboss.netty.channel.ChannelHandlerContext;
import org.jboss.netty.channel.SimpleChannelUpstreamHandler;
/**
* An extended {@link SimpleChannelUpstreamHandler} that adds the handler method
* for an {@link IdleStateEvent}.
*
* @author The Netty Project (netty-dev@lists.jboss.org)
* @author Trustin Lee (tlee@redhat.com)
* @version $Rev$, $Date$
@ -43,6 +47,9 @@ public class IdleStateAwareChannelUpstreamHandler extends SimpleChannelUpstreamH
}
}
/**
* Invoked when a {@link Channel} has been idle for a while.
*/
public void channelIdle(ChannelHandlerContext ctx, IdleStateEvent e) {
ctx.sendUpstream(e);
}

View File

@ -22,14 +22,25 @@
*/
package org.jboss.netty.handler.timeout;
import org.jboss.netty.channel.Channel;
import org.jboss.netty.channel.ChannelEvent;
/**
* A {@link ChannelEvent} that is triggered when a {@link Channel} has been idle
* for a while.
*
* @author The Netty Project (netty-dev@lists.jboss.org)
* @author Trustin Lee (tlee@redhat.com)
* @version $Rev$, $Date$
*/
public interface IdleStateEvent extends ChannelEvent {
/**
* Returns the detailed idle state.
*/
IdleState getState();
/**
* Returns the last time when I/O occurred in milliseconds.
*/
long getLastActivityTimeMillis();
}

View File

@ -26,6 +26,7 @@ import static org.jboss.netty.channel.Channels.*;
import java.util.concurrent.TimeUnit;
import org.jboss.netty.channel.Channel;
import org.jboss.netty.channel.ChannelHandlerContext;
import org.jboss.netty.channel.ChannelPipelineCoverage;
import org.jboss.netty.channel.ChannelStateEvent;
@ -39,6 +40,34 @@ import org.jboss.netty.util.Timer;
import org.jboss.netty.util.TimerTask;
/**
* Triggers an {@link IdleStateEvent} when a {@link Channel} has not performed
* read, write, or both operation for a while.
*
* <h3>Supported idle states</h3>
* <table>
* <tr>
* <th>Property</th><th>Meaning</th>
* </tr>
* <tr>
* <td>{@code readerIdleTime}</td>
* <td>an {@link IdleStateEvent} whose state is {@link IdleState#READER_IDLE}
* will be triggered when no read was performed for the specified period of
* time. Specify {@code 0} to disable.</td>
* </tr>
* <tr>
* <td>{@code writerIdleTime}</td>
* <td>an {@link IdleStateEvent} whose state is {@link IdleState#WRITER_IDLE}
* will be triggered when no write was performed for the specified period of
* time. Specify {@code 0} to disable.</td>
* </tr>
* <tr>
* <td>{@code allIdleTime}</td>
* <td>an {@link IdleStateEvent} whose state is {@link IdleState#ALL_IDLE}
* will be triggered when neither read nor write was performed for the
* specified period of time. Specify {@code 0} to disable.</td>
* </tr>
* </table>
*
* @author The Netty Project (netty-dev@lists.jboss.org)
* @author Trustin Lee (tlee@redhat.com)
* @version $Rev$, $Date$
@ -64,6 +93,24 @@ public class IdleStateHandler extends SimpleChannelUpstreamHandler
volatile Timeout allIdleTimeout;
private volatile AllIdleTimeoutTask allIdleTimeoutTask;
/**
* Creates a new instance.
*
* @param timer
* the {@link Timer} that is used to trigger the scheduled event
* @param readerIdleTimeSeconds
* an {@link IdleStateEvent} whose state is {@link IdleState#READER_IDLE}
* will be triggered when no read was performed for the specified
* period of time. Specify {@code 0} to disable.
* @param writerIdleTimeSeconds
* an {@link IdleStateEvent} whose state is {@link IdleState#WRITER_IDLE}
* will be triggered when no write was performed for the specified
* period of time. Specify {@code 0} to disable.
* @param allIdleTimeSeconds
* an {@link IdleStateEvent} whose state is {@link IdleState#ALL_IDLE}
* will be triggered when neither read nor write was performed for
* the specified period of time. Specify {@code 0} to disable.
*/
public IdleStateHandler(
Timer timer,
int readerIdleTimeSeconds,
@ -75,6 +122,27 @@ public class IdleStateHandler extends SimpleChannelUpstreamHandler
TimeUnit.SECONDS);
}
/**
* Creates a new instance.
*
* @param timer
* the {@link Timer} that is used to trigger the scheduled event
* @param readerIdleTime
* an {@link IdleStateEvent} whose state is {@link IdleState#READER_IDLE}
* will be triggered when no read was performed for the specified
* period of time. Specify {@code 0} to disable.
* @param writerIdleTime
* an {@link IdleStateEvent} whose state is {@link IdleState#WRITER_IDLE}
* will be triggered when no write was performed for the specified
* period of time. Specify {@code 0} to disable.
* @param allIdleTime
* an {@link IdleStateEvent} whose state is {@link IdleState#ALL_IDLE}
* will be triggered when neither read nor write was performed for
* the specified period of time. Specify {@code 0} to disable.
* @param unit
* the {@link TimeUnit} of {@code readerIdleTime},
* {@code writeIdleTime}, and {@code allIdleTime}
*/
public IdleStateHandler(
Timer timer,
long readerIdleTime, long writerIdleTime, long allIdleTime,
@ -93,6 +161,11 @@ public class IdleStateHandler extends SimpleChannelUpstreamHandler
allIdleTimeMillis = unit.toMillis(allIdleTime);
}
/**
* Stops the {@link Timer} which was specified in the constructor of this
* handler. You should not call this method if the {@link Timer} is in use
* by other objects.
*/
public void releaseExternalResources() {
timer.stop();
}

View File

@ -22,8 +22,10 @@
*/
package org.jboss.netty.handler.timeout;
/**
* A {@link TimeoutException} raised by {@link ReadTimeoutHandler} when no data
* was read within a certain period of time.
*
* @author The Netty Project (netty-dev@lists.jboss.org)
* @author Trustin Lee (tlee@redhat.com)
* @version $Rev$, $Date$

View File

@ -72,6 +72,11 @@ public class ReadTimeoutHandler extends SimpleChannelUpstreamHandler
timeoutMillis = unit.toMillis(timeout);
}
/**
* Stops the {@link Timer} which was specified in the constructor of this
* handler. You should not call this method if the {@link Timer} is in use
* by other objects.
*/
public void releaseExternalResources() {
timer.stop();
}

View File

@ -25,6 +25,9 @@ package org.jboss.netty.handler.timeout;
import org.jboss.netty.channel.ChannelException;
/**
* A {@link TimeoutException} when no data was either read or written within a
* certain period of time.
*
* @author The Netty Project (netty-dev@lists.jboss.org)
* @author Trustin Lee (tlee@redhat.com)
* @version $Rev$, $Date$

View File

@ -24,6 +24,9 @@ package org.jboss.netty.handler.timeout;
/**
* A {@link TimeoutException} raised by {@link WriteTimeoutHandler} when no data
* was written within a certain period of time.
*
* @author The Netty Project (netty-dev@lists.jboss.org)
* @author Trustin Lee (tlee@redhat.com)
* @version $Rev$, $Date$

View File

@ -68,6 +68,11 @@ public class WriteTimeoutHandler extends SimpleChannelDownstreamHandler
timeoutMillis = unit.toMillis(timeout);
}
/**
* Stops the {@link Timer} which was specified in the constructor of this
* handler. You should not call this method if the {@link Timer} is in use
* by other objects.
*/
public void releaseExternalResources() {
timer.stop();
}
@ -136,11 +141,11 @@ public class WriteTimeoutHandler extends SimpleChannelDownstreamHandler
*/
private static final class TimeoutCanceller implements ChannelFutureListener {
private final Timeout timeout;
TimeoutCanceller(Timeout timeout) {
this.timeout = timeout;
}
public void operationComplete(ChannelFuture future) throws Exception {
timeout.cancel();
}

View File

@ -0,0 +1,28 @@
/*
* JBoss, Home of Professional Open Source
*
* Copyright 2008, Red Hat Middleware LLC, and individual contributors
* by the @author tags. See the COPYRIGHT.txt in the distribution for a
* full listing of individual contributors.
*
* This is free software; you can redistribute it and/or modify it
* under the terms of the GNU Lesser General Public License as
* published by the Free Software Foundation; either version 2.1 of
* the License, or (at your option) any later version.
*
* This software is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public
* License along with this software; if not, write to the Free
* Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
* 02110-1301 USA, or see the FSF site: http://www.fsf.org.
*/
/**
* Adds support for read and write timeout and idle connection notification
* using a {@link org.jboss.netty.util.Timer}.
*/
package org.jboss.netty.handler.timeout;