You are viewing a plain text version of this content. The canonical link for it is here.
Posted to scm@geronimo.apache.org by ga...@apache.org on 2008/04/21 22:21:45 UTC
svn commit: r650245 - in
/geronimo/sandbox/concurrent/geronimo-concurrent/src/main/java/org/apache/geronimo/concurrent/impl/thread:
ManagedThreadFactoryGBean.java StandaloneManagedThreadFactory.java
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<ManagedThread> threads =
+ Collections.synchronizedList(new ArrayList<ManagedThread>());
+
+ 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<AbstractName, ModuleLifecycleListener> listeners =
+ new HashMap<AbstractName, ModuleLifecycleListener>();
+
+ private final AbstractName moduleName;
+ private boolean running;
+ private final List<StandaloneManagedThreadFactory> threadFactories =
+ new ArrayList<StandaloneManagedThreadFactory>();
+
+ 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