Return-Path: Delivered-To: apmail-geronimo-scm-archive@www.apache.org Received: (qmail 99271 invoked from network); 21 Apr 2008 20:22:13 -0000 Received: from hermes.apache.org (HELO mail.apache.org) (140.211.11.2) by minotaur.apache.org with SMTP; 21 Apr 2008 20:22:13 -0000 Received: (qmail 75536 invoked by uid 500); 21 Apr 2008 20:22:13 -0000 Delivered-To: apmail-geronimo-scm-archive@geronimo.apache.org Received: (qmail 75495 invoked by uid 500); 21 Apr 2008 20:22:13 -0000 Mailing-List: contact scm-help@geronimo.apache.org; run by ezmlm Precedence: bulk list-help: list-unsubscribe: List-Post: Reply-To: dev@geronimo.apache.org List-Id: Delivered-To: mailing list scm@geronimo.apache.org Received: (qmail 75486 invoked by uid 99); 21 Apr 2008 20:22:13 -0000 Received: from nike.apache.org (HELO nike.apache.org) (192.87.106.230) by apache.org (qpsmtpd/0.29) with ESMTP; Mon, 21 Apr 2008 13:22:13 -0700 X-ASF-Spam-Status: No, hits=-2000.0 required=10.0 tests=ALL_TRUSTED X-Spam-Check-By: apache.org Received: from [140.211.11.3] (HELO eris.apache.org) (140.211.11.3) by apache.org (qpsmtpd/0.29) with ESMTP; Mon, 21 Apr 2008 20:21:28 +0000 Received: by eris.apache.org (Postfix, from userid 65534) id 611FC1A9832; Mon, 21 Apr 2008 13:21:48 -0700 (PDT) Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit Subject: svn commit: r650245 - in /geronimo/sandbox/concurrent/geronimo-concurrent/src/main/java/org/apache/geronimo/concurrent/impl/thread: ManagedThreadFactoryGBean.java StandaloneManagedThreadFactory.java Date: Mon, 21 Apr 2008 20:21:45 -0000 To: scm@geronimo.apache.org From: gawor@apache.org X-Mailer: svnmailer-1.0.8 Message-Id: <20080421202148.611FC1A9832@eris.apache.org> X-Virus-Checked: Checked by ClamAV on apache.org Author: gawor Date: Mon Apr 21 13:21:43 2008 New Revision: 650245 URL: http://svn.apache.org/viewvc?rev=650245&view=rev Log: standalone thread factory that interuppts threads if the component that created the factory is stopped Added: geronimo/sandbox/concurrent/geronimo-concurrent/src/main/java/org/apache/geronimo/concurrent/impl/thread/StandaloneManagedThreadFactory.java (with props) Modified: geronimo/sandbox/concurrent/geronimo-concurrent/src/main/java/org/apache/geronimo/concurrent/impl/thread/ManagedThreadFactoryGBean.java Modified: geronimo/sandbox/concurrent/geronimo-concurrent/src/main/java/org/apache/geronimo/concurrent/impl/thread/ManagedThreadFactoryGBean.java URL: http://svn.apache.org/viewvc/geronimo/sandbox/concurrent/geronimo-concurrent/src/main/java/org/apache/geronimo/concurrent/impl/thread/ManagedThreadFactoryGBean.java?rev=650245&r1=650244&r2=650245&view=diff ============================================================================== --- geronimo/sandbox/concurrent/geronimo-concurrent/src/main/java/org/apache/geronimo/concurrent/impl/thread/ManagedThreadFactoryGBean.java (original) +++ geronimo/sandbox/concurrent/geronimo-concurrent/src/main/java/org/apache/geronimo/concurrent/impl/thread/ManagedThreadFactoryGBean.java Mon Apr 21 13:21:43 2008 @@ -154,8 +154,7 @@ public Object $getResource(AbstractName moduleID) { GeronimoManagedThreadFactory threadFactory = getManagedThreadFactory(); - return ManagedThreadFactoryUtils.createStandaloneThreadFactory(threadFactory, - this.mainContextHandler); + return new StandaloneManagedThreadFactory(threadFactory, this.mainContextHandler, moduleID); } public void doStart() throws Exception { Added: geronimo/sandbox/concurrent/geronimo-concurrent/src/main/java/org/apache/geronimo/concurrent/impl/thread/StandaloneManagedThreadFactory.java URL: http://svn.apache.org/viewvc/geronimo/sandbox/concurrent/geronimo-concurrent/src/main/java/org/apache/geronimo/concurrent/impl/thread/StandaloneManagedThreadFactory.java?rev=650245&view=auto ============================================================================== --- geronimo/sandbox/concurrent/geronimo-concurrent/src/main/java/org/apache/geronimo/concurrent/impl/thread/StandaloneManagedThreadFactory.java (added) +++ geronimo/sandbox/concurrent/geronimo-concurrent/src/main/java/org/apache/geronimo/concurrent/impl/thread/StandaloneManagedThreadFactory.java Mon Apr 21 13:21:43 2008 @@ -0,0 +1,177 @@ +/** + * Licensed to the Apache Software Foundation (ASF) under one or more + * contributor license agreements. See the NOTICE file distributed with + * this work for additional information regarding copyright ownership. + * The ASF licenses this file to You under the Apache License, Version 2.0 + * (the "License"); you may not use this file except in compliance with + * the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +package org.apache.geronimo.concurrent.impl.thread; + +import java.util.ArrayList; +import java.util.Collections; +import java.util.HashMap; +import java.util.List; +import java.util.Map; + +import javax.util.concurrent.ManagedThreadFactory; + +import org.apache.geronimo.concurrent.ManagedContext; +import org.apache.geronimo.concurrent.ManagedContextHandler; +import org.apache.geronimo.concurrent.thread.ManagedRunnable; +import org.apache.geronimo.concurrent.thread.ManagedThread; +import org.apache.geronimo.concurrent.thread.ThreadStoppedListener; +import org.apache.geronimo.gbean.AbstractName; +import org.apache.geronimo.gbean.AbstractNameQuery; +import org.apache.geronimo.kernel.Kernel; +import org.apache.geronimo.kernel.KernelRegistry; +import org.apache.geronimo.kernel.lifecycle.LifecycleListener; + +public class StandaloneManagedThreadFactory implements ManagedThreadFactory, ThreadStoppedListener { + + private GeronimoManagedThreadFactory threadFactory; + private ManagedContextHandler contextHandler; + private ModuleLifecycleListener moduleLifecyleListener; + private List threads = + Collections.synchronizedList(new ArrayList()); + + public StandaloneManagedThreadFactory(GeronimoManagedThreadFactory threadFactory, + ManagedContextHandler contextHandler, + AbstractName moduleName) { + this.threadFactory = threadFactory; + this.contextHandler = contextHandler; + this.moduleLifecyleListener = ModuleLifecycleListener.getModuleLifecycleListener(moduleName); + this.moduleLifecyleListener.addThreadFactory(this); + } + + public Thread newThread(Runnable runnable) { + if (!isRunning()) { + throw new IllegalStateException("Component is not running"); + } + + ManagedContext managedContext = ManagedContext.captureContext(this.contextHandler); + runnable = new ManagedRunnable(runnable, managedContext, true); + ManagedThread thread = (ManagedThread)this.threadFactory.newThread(runnable); + + this.threads.add(thread); + return thread; + } + + protected boolean isRunning() { + return this.moduleLifecyleListener.isRunning(); + } + + public void threadStopped(Thread thread) { + this.threads.remove(thread); + this.threadFactory.threadStopped(thread); + } + + protected void shutdown() { + synchronized(this.threads) { + for (Thread thread : this.threads) { + thread.interrupt(); + } + } + } + + private static class ModuleLifecycleListener implements LifecycleListener { + + private static final Map listeners = + new HashMap(); + + private final AbstractName moduleName; + private boolean running; + private final List threadFactories = + new ArrayList(); + + public ModuleLifecycleListener(AbstractName moduleName) { + this.moduleName = moduleName; + this.running = true; + } + + public synchronized void addThreadFactory(StandaloneManagedThreadFactory threadFactory) { + if (this.running) { + this.threadFactories.add(threadFactory); + } + } + + public synchronized void shutdown() { + for (StandaloneManagedThreadFactory threadFactory : this.threadFactories) { + try { + threadFactory.shutdown(); + } catch (Exception e) { + // ignore + } + } + this.threadFactories.clear(); + } + + public boolean isRunning() { + return this.running; + } + + public AbstractName getModuleName() { + return this.moduleName; + } + + public void failed(AbstractName arg0) { + this.running = false; + } + + public void loaded(AbstractName arg0) { + this.running = false; + } + + public void running(AbstractName arg0) { + this.running = true; + } + + public void starting(AbstractName arg0) { + this.running = false; + } + + public void stopped(AbstractName arg0) { + this.running = false; + } + + public void stopping(AbstractName arg0) { + this.running = false; + shutdown(); + } + + public void unloaded(AbstractName arg0) { + this.running = false; + shutdown(); + removeModuleLifecycleListener(this.moduleName); + } + + private static synchronized ModuleLifecycleListener getModuleLifecycleListener(AbstractName moduleName) { + ModuleLifecycleListener listener = listeners.get(moduleName); + if (listener == null) { + listener = new ModuleLifecycleListener(moduleName); + + // register listener with Kernel + AbstractNameQuery query = new AbstractNameQuery(moduleName); + Kernel kernel = KernelRegistry.getSingleKernel(); + kernel.getLifecycleMonitor().addLifecycleListener(listener, query); + + listeners.put(moduleName, listener); + } + return listener; + } + + private static synchronized void removeModuleLifecycleListener(AbstractName moduleName) { + listeners.remove(moduleName); + } + + } + +} Propchange: geronimo/sandbox/concurrent/geronimo-concurrent/src/main/java/org/apache/geronimo/concurrent/impl/thread/StandaloneManagedThreadFactory.java ------------------------------------------------------------------------------ svn:eol-style = native