diff --git a/src/main/java/org/jboss/netty/container/guice/NettyModule.java b/src/main/java/org/jboss/netty/container/guice/NettyModule.java index d0fc30ec04..1ac40a5b77 100644 --- a/src/main/java/org/jboss/netty/container/guice/NettyModule.java +++ b/src/main/java/org/jboss/netty/container/guice/NettyModule.java @@ -33,6 +33,7 @@ import org.jboss.netty.channel.socket.nio.NioServerSocketChannelFactory; import org.jboss.netty.channel.socket.oio.OioClientSocketChannelFactory; import org.jboss.netty.channel.socket.oio.OioServerSocketChannelFactory; import org.jboss.netty.util.ExecutorShutdownUtil; +import org.jboss.netty.util.UnterminatableExecutor; import com.google.inject.AbstractModule; import com.google.inject.Scopes; @@ -57,6 +58,8 @@ public class NettyModule extends AbstractModule { "Executor has been shut down already."); } + Executor executor = new UnterminatableExecutor(this.executor); + bind(Executor.class). annotatedWith(ChannelFactoryResource.class). toInstance(executor); diff --git a/src/main/java/org/jboss/netty/container/microcontainer/NettyResourceFactory.java b/src/main/java/org/jboss/netty/container/microcontainer/NettyResourceFactory.java index bd41018a19..5749702eea 100644 --- a/src/main/java/org/jboss/netty/container/microcontainer/NettyResourceFactory.java +++ b/src/main/java/org/jboss/netty/container/microcontainer/NettyResourceFactory.java @@ -28,6 +28,7 @@ import java.util.concurrent.Executors; import org.jboss.netty.logging.InternalLoggerFactory; import org.jboss.netty.logging.JBossLoggerFactory; import org.jboss.netty.util.ExecutorShutdownUtil; +import org.jboss.netty.util.UnterminatableExecutor; /** * @author The Netty Project (netty-dev@lists.jboss.org) @@ -35,27 +36,30 @@ import org.jboss.netty.util.ExecutorShutdownUtil; * @version $Rev$, $Date$ */ public class NettyResourceFactory { - private volatile Executor channelFactoryExecutor; + private Executor executor; + private Executor unterminatableExecutor; - public void create() { - channelFactoryExecutor = Executors.newCachedThreadPool(); + public synchronized void create() { + executor = Executors.newCachedThreadPool(); + unterminatableExecutor = new UnterminatableExecutor(executor); } public void start() { InternalLoggerFactory.setDefaultFactory(new JBossLoggerFactory()); } - public void stop() { - if (channelFactoryExecutor != null) { - ExecutorShutdownUtil.shutdown(channelFactoryExecutor); + public synchronized void stop() { + if (executor != null) { + ExecutorShutdownUtil.shutdown(executor); } } - public void destroy() { - channelFactoryExecutor = null; + public synchronized void destroy() { + executor = null; + unterminatableExecutor = null; } public Executor getChannelFactoryExecutor() { - return channelFactoryExecutor; + return unterminatableExecutor; } } diff --git a/src/main/java/org/jboss/netty/container/osgi/NettyBundleActivator.java b/src/main/java/org/jboss/netty/container/osgi/NettyBundleActivator.java index 2a0b8c2ee1..aedab4259f 100644 --- a/src/main/java/org/jboss/netty/container/osgi/NettyBundleActivator.java +++ b/src/main/java/org/jboss/netty/container/osgi/NettyBundleActivator.java @@ -59,9 +59,8 @@ public class NettyBundleActivator implements BundleActivator { public void start(BundleContext ctx) throws Exception { initLoggerFactory(ctx); - InternalLoggerFactory.getInstance(NettyBundleActivator.class).info("YAY"); - executor = Executors.newCachedThreadPool(); + Executor executor = this.executor = Executors.newCachedThreadPool(); // The default transport is NIO. register(ctx, @@ -77,8 +76,10 @@ public class NettyBundleActivator implements BundleActivator { public void stop(BundleContext ctx) throws Exception { unregisterAll(); - ExecutorShutdownUtil.shutdown(executor); - executor = null; + if (executor != null) { + ExecutorShutdownUtil.shutdown(executor); + executor = null; + } } private void initLoggerFactory(BundleContext ctx) { diff --git a/src/main/java/org/jboss/netty/util/UnterminatableExecutor.java b/src/main/java/org/jboss/netty/util/UnterminatableExecutor.java new file mode 100644 index 0000000000..36caa6dd16 --- /dev/null +++ b/src/main/java/org/jboss/netty/util/UnterminatableExecutor.java @@ -0,0 +1,48 @@ +/* + * JBoss, Home of Professional Open Source + * + * Copyright 2009, 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. + */ +package org.jboss.netty.util; + +import java.util.concurrent.Executor; + +/** + * Disables shutdown of an {@link Executor} by wrapping the {@link Executor}. + * + * @author The Netty Project (netty-dev@lists.jboss.org) + * @author Trustin Lee (tlee@redhat.com) + * @version $Rev$, $Date$ + */ +public class UnterminatableExecutor implements Executor { + + private final Executor executor; + + public UnterminatableExecutor(Executor executor) { + if (executor == null) { + throw new NullPointerException("executor"); + } + this.executor = executor; + } + + public void execute(Runnable command) { + executor.execute(command); + } +}