Allow to pass in the Timer via constructor and also stop it when the bootstrap was shutdown

This commit is contained in:
Norman Maurer 2012-09-18 20:44:39 +02:00
parent b1f846b0d7
commit bd99d0f3e4
2 changed files with 17 additions and 5 deletions

View File

@ -26,6 +26,8 @@ import org.jboss.netty.channel.group.ChannelGroup;
import org.jboss.netty.channel.socket.ClientSocketChannelFactory;
import org.jboss.netty.channel.socket.SocketChannel;
import org.jboss.netty.util.ExternalResourceReleasable;
import org.jboss.netty.util.HashedWheelTimer;
import org.jboss.netty.util.Timer;
import org.jboss.netty.util.internal.ExecutorUtil;
/**
@ -86,6 +88,7 @@ public class NioClientSocketChannelFactory implements ClientSocketChannelFactory
private final Executor bossExecutor;
private final WorkerPool<NioWorker> workerPool;
private final NioClientSocketPipelineSink sink;
private final Timer timer;
/**
* Creates a new {@link NioClientSocketChannelFactory} which uses {@link Executors#newCachedThreadPool()}
@ -152,6 +155,12 @@ public class NioClientSocketChannelFactory implements ClientSocketChannelFactory
public NioClientSocketChannelFactory(
Executor bossExecutor, int bossCount,
WorkerPool<NioWorker> workerPool) {
this(bossExecutor, bossCount, workerPool, new HashedWheelTimer());
}
public NioClientSocketChannelFactory(
Executor bossExecutor, int bossCount,
WorkerPool<NioWorker> workerPool, Timer timer) {
if (bossExecutor == null) {
throw new NullPointerException("bossExecutor");
@ -168,8 +177,9 @@ public class NioClientSocketChannelFactory implements ClientSocketChannelFactory
this.bossExecutor = bossExecutor;
this.workerPool = workerPool;
this.timer = timer;
sink = new NioClientSocketPipelineSink(
bossExecutor, bossCount, workerPool);
bossExecutor, bossCount, workerPool, timer);
}
public SocketChannel newChannel(ChannelPipeline pipeline) {
@ -178,6 +188,7 @@ public class NioClientSocketChannelFactory implements ClientSocketChannelFactory
public void releaseExternalResources() {
ExecutorUtil.terminate(bossExecutor);
timer.stop();
if (workerPool instanceof ExternalResourceReleasable) {
((ExternalResourceReleasable) workerPool).releaseExternalResources();
}

View File

@ -56,7 +56,6 @@ import org.jboss.netty.util.internal.DeadLockProofWorker;
class NioClientSocketPipelineSink extends AbstractNioChannelSink {
private static final AtomicInteger nextId = new AtomicInteger();
private static final Timer TIMER = new HashedWheelTimer();
static final InternalLogger logger =
InternalLoggerFactory.getInstance(NioClientSocketPipelineSink.class);
@ -70,11 +69,13 @@ class NioClientSocketPipelineSink extends AbstractNioChannelSink {
private final WorkerPool<NioWorker> workerPool;
private final Timer timer;
NioClientSocketPipelineSink(
Executor bossExecutor, int bossCount, WorkerPool<NioWorker> workerPool) {
Executor bossExecutor, int bossCount, WorkerPool<NioWorker> workerPool, Timer timer) {
this.bossExecutor = bossExecutor;
this.timer = timer;
bosses = new Boss[bossCount];
for (int i = 0; i < bosses.length; i ++) {
bosses[i] = new Boss(i);
@ -249,7 +250,7 @@ class NioClientSocketPipelineSink extends AbstractNioChannelSink {
int timeout = channel.getConfig().getConnectTimeoutMillis();
if (timeout > 0) {
if (!channel.isConnected()) {
channel.timoutTimer = TIMER.newTimeout(wakeupTask,
channel.timoutTimer = timer.newTimeout(wakeupTask,
timeout, TimeUnit.MILLISECONDS);
}
}