* Removed an unnecessary log statement in the OSGi BundleActivator impl

* Added UnterminatableExecutor
* Prohibited an attempt to release the shared resources when initialized via containers
This commit is contained in:
Trustin Lee 2009-01-12 10:48:56 +00:00
parent 04af4356ef
commit 587d623789
4 changed files with 69 additions and 13 deletions

View File

@ -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);

View File

@ -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;
}
}

View File

@ -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) {

View File

@ -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);
}
}