Change Executor to Timer from Netty, in reference to Issue #345

This commit is contained in:
Frédéric Brégier 2012-05-20 11:25:28 +03:00
parent f3a7029c0f
commit 9ba9107267

View File

@ -15,13 +15,12 @@
*/ */
package org.jboss.netty.handler.traffic; package org.jboss.netty.handler.traffic;
import java.util.concurrent.Executor;
import org.jboss.netty.channel.ChannelHandler.Sharable; import org.jboss.netty.channel.ChannelHandler.Sharable;
import org.jboss.netty.handler.execution.ExecutionHandler; import org.jboss.netty.handler.execution.ExecutionHandler;
import org.jboss.netty.handler.execution.MemoryAwareThreadPoolExecutor; import org.jboss.netty.handler.execution.MemoryAwareThreadPoolExecutor;
import org.jboss.netty.handler.execution.OrderedMemoryAwareThreadPoolExecutor; import org.jboss.netty.handler.execution.OrderedMemoryAwareThreadPoolExecutor;
import org.jboss.netty.util.ObjectSizeEstimator; import org.jboss.netty.util.ObjectSizeEstimator;
import org.jboss.netty.util.Timer;
/** /**
* This implementation of the {@link AbstractTrafficShapingHandler} is for global * This implementation of the {@link AbstractTrafficShapingHandler} is for global
@ -31,8 +30,8 @@ import org.jboss.netty.util.ObjectSizeEstimator;
* The general use should be as follow:<br> * The general use should be as follow:<br>
* <ul> * <ul>
* <li>Create your unique GlobalTrafficShapingHandler like:<br><br> * <li>Create your unique GlobalTrafficShapingHandler like:<br><br>
* <tt>GlobalTrafficShapingHandler myHandler = new GlobalTrafficShapingHandler(executor);</tt><br><br> * <tt>GlobalTrafficShapingHandler myHandler = new GlobalTrafficShapingHandler(timer);</tt><br><br>
* executor could be created using <tt>Executors.newCachedThreadPool();</tt><br> * timer could be created using <tt>HashedWheelTimer<tt><br>
* <tt>pipeline.addLast("GLOBAL_TRAFFIC_SHAPING", myHandler);</tt><br><br> * <tt>pipeline.addLast("GLOBAL_TRAFFIC_SHAPING", myHandler);</tt><br><br>
* *
* <b>Note that this handler has a Pipeline Coverage of "all" which means only one such handler must be created * <b>Note that this handler has a Pipeline Coverage of "all" which means only one such handler must be created
@ -52,7 +51,7 @@ import org.jboss.netty.util.ObjectSizeEstimator;
* {@link OrderedMemoryAwareThreadPoolExecutor} or {@link MemoryAwareThreadPoolExecutor}).<br> * {@link OrderedMemoryAwareThreadPoolExecutor} or {@link MemoryAwareThreadPoolExecutor}).<br>
* <tt>pipeline.addLast("GLOBAL_TRAFFIC_SHAPING", myHandler);</tt><br><br> * <tt>pipeline.addLast("GLOBAL_TRAFFIC_SHAPING", myHandler);</tt><br><br>
* </li> * </li>
* <li>When you shutdown your application, release all the external resources like the executor * <li>When you shutdown your application, release all the external resources (except the timer internal itself)
* by calling:<br> * by calling:<br>
* <tt>myHandler.releaseExternalResources();</tt><br> * <tt>myHandler.releaseExternalResources();</tt><br>
* </li> * </li>
@ -64,96 +63,61 @@ public class GlobalTrafficShapingHandler extends AbstractTrafficShapingHandler {
* Create the global TrafficCounter * Create the global TrafficCounter
*/ */
void createGlobalTrafficCounter() { void createGlobalTrafficCounter() {
TrafficCounter tc = new TrafficCounter(this, executor, "GlobalTC", TrafficCounter tc;
checkInterval); if (timer != null) {
setTrafficCounter(tc); tc = new TrafficCounter(this, timer, "GlobalTC",
tc.start(); checkInterval);
setTrafficCounter(tc);
tc.start();
}
} }
/** public GlobalTrafficShapingHandler(Timer timer, long writeLimit,
* @param executor
* @param writeLimit
* @param readLimit
* @param checkInterval
*/
public GlobalTrafficShapingHandler(Executor executor, long writeLimit,
long readLimit, long checkInterval) { long readLimit, long checkInterval) {
super(executor, writeLimit, readLimit, checkInterval); super(timer, writeLimit, readLimit, checkInterval);
createGlobalTrafficCounter(); createGlobalTrafficCounter();
} }
/** public GlobalTrafficShapingHandler(Timer timer, long writeLimit,
* @param executor
* @param writeLimit
* @param readLimit
*/
public GlobalTrafficShapingHandler(Executor executor, long writeLimit,
long readLimit) { long readLimit) {
super(executor, writeLimit, readLimit); super(timer, writeLimit, readLimit);
createGlobalTrafficCounter();
}
/**
* @param executor
* @param checkInterval
*/
public GlobalTrafficShapingHandler(Executor executor, long checkInterval) {
super(executor, checkInterval);
createGlobalTrafficCounter(); createGlobalTrafficCounter();
} }
/** public GlobalTrafficShapingHandler(Timer timer, long checkInterval) {
* @param executor super(timer, checkInterval);
*/ createGlobalTrafficCounter();
public GlobalTrafficShapingHandler(Executor executor) { }
super(executor);
public GlobalTrafficShapingHandler(Timer timer) {
super(timer);
createGlobalTrafficCounter(); createGlobalTrafficCounter();
} }
/**
* @param objectSizeEstimator
* @param executor
* @param writeLimit
* @param readLimit
* @param checkInterval
*/
public GlobalTrafficShapingHandler(ObjectSizeEstimator objectSizeEstimator, public GlobalTrafficShapingHandler(ObjectSizeEstimator objectSizeEstimator,
Executor executor, long writeLimit, long readLimit, Timer timer, long writeLimit, long readLimit,
long checkInterval) { long checkInterval) {
super(objectSizeEstimator, executor, writeLimit, readLimit, super(objectSizeEstimator, timer, writeLimit, readLimit,
checkInterval); checkInterval);
createGlobalTrafficCounter(); createGlobalTrafficCounter();
} }
/**
* @param objectSizeEstimator
* @param executor
* @param writeLimit
* @param readLimit
*/
public GlobalTrafficShapingHandler(ObjectSizeEstimator objectSizeEstimator, public GlobalTrafficShapingHandler(ObjectSizeEstimator objectSizeEstimator,
Executor executor, long writeLimit, long readLimit) { Timer timer, long writeLimit, long readLimit) {
super(objectSizeEstimator, executor, writeLimit, readLimit); super(objectSizeEstimator, timer, writeLimit, readLimit);
createGlobalTrafficCounter(); createGlobalTrafficCounter();
} }
/**
* @param objectSizeEstimator
* @param executor
* @param checkInterval
*/
public GlobalTrafficShapingHandler(ObjectSizeEstimator objectSizeEstimator, public GlobalTrafficShapingHandler(ObjectSizeEstimator objectSizeEstimator,
Executor executor, long checkInterval) { Timer timer, long checkInterval) {
super(objectSizeEstimator, executor, checkInterval); super(objectSizeEstimator, timer, checkInterval);
createGlobalTrafficCounter(); createGlobalTrafficCounter();
} }
/**
* @param objectSizeEstimator
* @param executor
*/
public GlobalTrafficShapingHandler(ObjectSizeEstimator objectSizeEstimator, public GlobalTrafficShapingHandler(ObjectSizeEstimator objectSizeEstimator,
Executor executor) { Timer timer) {
super(objectSizeEstimator, executor); super(objectSizeEstimator, timer);
createGlobalTrafficCounter(); createGlobalTrafficCounter();
} }
} }