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/23 19:34:21 UTC
svn commit: r650948 - in
/geronimo/sandbox/concurrent/geronimo-concurrent/src/main/java/org/apache/geronimo/concurrent/impl:
executor/ thread/
Author: gawor
Date: Wed Apr 23 10:34:20 2008
New Revision: 650948
URL: http://svn.apache.org/viewvc?rev=650948&view=rev
Log:
refactored managed thread factory code a bit
Added:
geronimo/sandbox/concurrent/geronimo-concurrent/src/main/java/org/apache/geronimo/concurrent/impl/thread/TrackingManagedThreadFactory.java (with props)
Modified:
geronimo/sandbox/concurrent/geronimo-concurrent/src/main/java/org/apache/geronimo/concurrent/impl/executor/ServerManagedThreadFactory.java
geronimo/sandbox/concurrent/geronimo-concurrent/src/main/java/org/apache/geronimo/concurrent/impl/thread/ManagedThreadFactoryGBean.java
geronimo/sandbox/concurrent/geronimo-concurrent/src/main/java/org/apache/geronimo/concurrent/impl/thread/StandaloneManagedThreadFactory.java
Modified: geronimo/sandbox/concurrent/geronimo-concurrent/src/main/java/org/apache/geronimo/concurrent/impl/executor/ServerManagedThreadFactory.java
URL: http://svn.apache.org/viewvc/geronimo/sandbox/concurrent/geronimo-concurrent/src/main/java/org/apache/geronimo/concurrent/impl/executor/ServerManagedThreadFactory.java?rev=650948&r1=650947&r2=650948&view=diff
==============================================================================
--- geronimo/sandbox/concurrent/geronimo-concurrent/src/main/java/org/apache/geronimo/concurrent/impl/executor/ServerManagedThreadFactory.java (original)
+++ geronimo/sandbox/concurrent/geronimo-concurrent/src/main/java/org/apache/geronimo/concurrent/impl/executor/ServerManagedThreadFactory.java Wed Apr 23 10:34:20 2008
@@ -16,37 +16,26 @@
*/
package org.apache.geronimo.concurrent.impl.executor;
-import java.util.ArrayList;
-import java.util.Collections;
-import java.util.List;
import java.util.Properties;
-import javax.util.concurrent.ManagedThreadFactory;
-
import org.apache.geronimo.concurrent.impl.NotificationHelper;
import org.apache.geronimo.concurrent.impl.thread.GeronimoManagedThread;
import org.apache.geronimo.concurrent.impl.thread.GeronimoManagedThreadFactory;
import org.apache.geronimo.concurrent.impl.thread.ManagedThreadGBean;
-import org.apache.geronimo.concurrent.thread.ManagedThread;
-import org.apache.geronimo.concurrent.thread.ThreadLifecycleListener;
+import org.apache.geronimo.concurrent.impl.thread.TrackingManagedThreadFactory;
import org.apache.geronimo.management.ManagedConstants;
/**
* ThreadFactory to be used within
* ServerManagedExecutorService or ServerManagedScheduledExecutorService.
*/
-public class ServerManagedThreadFactory
- implements ManagedThreadFactory, ThreadLifecycleListener {
+public class ServerManagedThreadFactory extends TrackingManagedThreadFactory {
- private GeronimoManagedThreadFactory factory;
private NotificationHelper notificationHelper;
-
- private List<ManagedThread> threads =
- Collections.synchronizedList(new ArrayList<ManagedThread>());
-
+
public ServerManagedThreadFactory(GeronimoManagedThreadFactory factory,
- NotificationHelper notificationHelper) {
- this.factory = factory;
+ NotificationHelper notificationHelper) {
+ super(factory);
this.notificationHelper = notificationHelper;
}
@@ -59,35 +48,16 @@
}
}
+ @Override
public Thread newThread(Runnable runnable) {
- GeronimoManagedThread thread = (GeronimoManagedThread)this.factory.newThread(runnable);
-
- // set listener so that this class gets notifications of thread stop
- thread.setThreadLifecycleListener(this);
-
- this.threads.add(thread);
-
+ GeronimoManagedThread thread = (GeronimoManagedThread)super.newThread(runnable);
+
// send JMX notification
sendNotification(thread.getGbean());
return thread;
}
-
- public void threadStarted(Thread thread) {
- this.factory.threadStarted(thread);
- }
-
- public void threadStopped(Thread thread) {
- this.threads.remove(thread);
- this.factory.threadStopped(thread);
- }
-
- private List<ManagedThread> getThreadList() {
- synchronized(this.threads) {
- return new ArrayList<ManagedThread>(this.threads);
- }
- }
-
+
public String[] getEventTypes() {
return new String[] { ManagedConstants.NEW_THREAD_EVENT };
}
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=650948&r1=650947&r2=650948&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 Wed Apr 23 10:34:20 2008
@@ -27,7 +27,6 @@
import org.apache.geronimo.concurrent.impl.GBeanBuilder;
import org.apache.geronimo.concurrent.impl.NotificationHelper;
import org.apache.geronimo.concurrent.naming.ModuleAwareResourceSource;
-import org.apache.geronimo.concurrent.thread.ManagedThreadFactoryUtils;
import org.apache.geronimo.gbean.AbstractName;
import org.apache.geronimo.gbean.GBeanData;
import org.apache.geronimo.gbean.GBeanInfo;
Modified: 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=650948&r1=650947&r2=650948&view=diff
==============================================================================
--- geronimo/sandbox/concurrent/geronimo-concurrent/src/main/java/org/apache/geronimo/concurrent/impl/thread/StandaloneManagedThreadFactory.java (original)
+++ geronimo/sandbox/concurrent/geronimo-concurrent/src/main/java/org/apache/geronimo/concurrent/impl/thread/StandaloneManagedThreadFactory.java Wed Apr 23 10:34:20 2008
@@ -17,37 +17,29 @@
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.ThreadLifecycleListener;
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, ThreadLifecycleListener {
-
- private GeronimoManagedThreadFactory threadFactory;
+public class StandaloneManagedThreadFactory extends TrackingManagedThreadFactory {
+
private ManagedContextHandler contextHandler;
- private ModuleLifecycleListener moduleLifecyleListener;
- private List<ManagedThread> threads =
- Collections.synchronizedList(new ArrayList<ManagedThread>());
+ private ModuleLifecycleListener moduleLifecyleListener;
private ManagedContext managedContext;
public StandaloneManagedThreadFactory(GeronimoManagedThreadFactory threadFactory,
ManagedContextHandler contextHandler,
AbstractName moduleName) {
- this.threadFactory = threadFactory;
+ super(threadFactory);
this.contextHandler = contextHandler;
this.moduleLifecyleListener = ModuleLifecycleListener.getModuleLifecycleListener(moduleName);
this.moduleLifecyleListener.addThreadFactory(this);
@@ -56,38 +48,24 @@
this.managedContext = ManagedContext.captureContext(this.contextHandler);
}
+ @Override
public Thread newThread(Runnable runnable) {
if (!isRunning()) {
throw new IllegalArgumentException(
"Component that created this thread factory is no longer running");
}
- runnable = new ManagedRunnable(runnable, this.managedContext, true);
- ManagedThread thread = (ManagedThread)this.threadFactory.newThread(runnable);
-
- this.threads.add(thread);
+ Runnable managedRunnable = new ManagedRunnable(runnable, this.managedContext, true);
+ Thread thread = super.newThread(managedRunnable);
return thread;
}
protected boolean isRunning() {
return this.moduleLifecyleListener.isRunning();
}
-
- public void threadStarted(Thread thread) {
- this.threadFactory.threadStarted(thread);
- }
-
- 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();
- }
- }
+ interruptThreads();
}
private static class ModuleLifecycleListener implements LifecycleListener {
Added: geronimo/sandbox/concurrent/geronimo-concurrent/src/main/java/org/apache/geronimo/concurrent/impl/thread/TrackingManagedThreadFactory.java
URL: http://svn.apache.org/viewvc/geronimo/sandbox/concurrent/geronimo-concurrent/src/main/java/org/apache/geronimo/concurrent/impl/thread/TrackingManagedThreadFactory.java?rev=650948&view=auto
==============================================================================
--- geronimo/sandbox/concurrent/geronimo-concurrent/src/main/java/org/apache/geronimo/concurrent/impl/thread/TrackingManagedThreadFactory.java (added)
+++ geronimo/sandbox/concurrent/geronimo-concurrent/src/main/java/org/apache/geronimo/concurrent/impl/thread/TrackingManagedThreadFactory.java Wed Apr 23 10:34:20 2008
@@ -0,0 +1,85 @@
+/**
+ * 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.List;
+
+import javax.util.concurrent.ManagedThreadFactory;
+
+import org.apache.commons.logging.Log;
+import org.apache.commons.logging.LogFactory;
+import org.apache.geronimo.concurrent.thread.ManagedThread;
+import org.apache.geronimo.concurrent.thread.ThreadLifecycleListener;
+
+public class TrackingManagedThreadFactory implements ManagedThreadFactory, ThreadLifecycleListener {
+
+ private final static Log LOG = LogFactory.getLog(TrackingManagedThreadFactory.class);
+
+ protected GeronimoManagedThreadFactory threadFactory;
+ protected List<ManagedThread> threads =
+ Collections.synchronizedList(new ArrayList<ManagedThread>());
+
+ public TrackingManagedThreadFactory(GeronimoManagedThreadFactory threadFactory) {
+ this.threadFactory = threadFactory;
+ if (this.threadFactory == null) {
+ throw new NullPointerException("threadFactory is null");
+ }
+ }
+
+ public Thread newThread(Runnable runnable) {
+ ManagedThread thread = (ManagedThread)this.threadFactory.newThread(runnable);
+
+ // set listener so that this class gets notifications of thread lifecycle events
+ thread.setThreadLifecycleListener(this);
+
+ this.threads.add(thread);
+
+ LOG.debug("Thread created: " + thread);
+
+ return thread;
+ }
+
+ public void threadStarted(Thread thread) {
+ this.threadFactory.threadStarted(thread);
+
+ LOG.debug("Thread started: " + thread);
+ }
+
+ public void threadStopped(Thread thread) {
+ this.threads.remove(thread);
+ this.threadFactory.threadStopped(thread);
+
+ LOG.debug("Thread stopped: " + thread);
+ }
+
+ protected List<ManagedThread> getThreadList() {
+ synchronized(this.threads) {
+ return new ArrayList<ManagedThread>(this.threads);
+ }
+ }
+
+ protected void interruptThreads() {
+ synchronized(this.threads) {
+ for (Thread thread : this.threads) {
+ thread.interrupt();
+ }
+ }
+ }
+
+}
Propchange: geronimo/sandbox/concurrent/geronimo-concurrent/src/main/java/org/apache/geronimo/concurrent/impl/thread/TrackingManagedThreadFactory.java
------------------------------------------------------------------------------
svn:eol-style = native