webappClassLoader.clearThreadLocal=A web application created a ThreadLocal with key of type [{0}] (value [{1}]) and a value of type [{2}] (value [{3}]) but failed to remove it when the web application was stopped. To prevent a memory leak, the ThreadLocal has been forcibly removed.
webappClassLoader.clearThreadLocalFail=Failed to clear ThreadLocal references
webappClassLoader.stopThreadFail=Failed to terminate thread named [{0}]
+webappClassLoader.stopTimerThreadFail=Failed to terminate TimerThread named [{0}]
webappClassLoader.validationErrorJarPath=Unable to validate JAR entry with name {0}
webappClassLoader.warnThread=A web application appears to have started a thread named [{0}] but has failed to stop it. This is very likely to create a memory leak.
+webappClassLoader.warnTimerThread=A web application appears to have started a TimerThread named [{0}] via the java.util.Timer API but has failed to stop it. To prevent a memory leak, the timer (and hence the associated thread) has been forcibly cancelled.
webappClassLoader.wrongVersion=(unable to load class {0})
webappLoader.addRepository=Adding repository {0}
webappLoader.deploy=Deploying class repositories to work directory {0}
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;
+import java.util.TimerTask;
import java.util.Vector;
import java.util.concurrent.ThreadPoolExecutor;
import java.util.jar.Attributes;
* instability. As such, enabling this should be viewed as an option of last
* resort in a development environment and is not recommended in a
* production environment. If not specified, the default value of
- * <code>false</code> will be used.
+ * <code>false</code> will be used. Note that instances of
+ * java.util.TimerThread will always be terminate since a safe method exists
+ * to do so.
*/
private boolean clearReferencesStopThreads = false;
continue;
}
+ // TimerThread is not normally visible
+ if (thread.getClass().getName().equals(
+ "java.util.TimerThread")) {
+ clearReferencesStopTimerThread(thread);
+ continue;
+ }
+
log.error(sm.getString("webappClassLoader.warnThread",
thread.getName()));
}
+ private void clearReferencesStopTimerThread(Thread thread) {
+
+ // Need to get references to:
+ // - newTasksMayBeScheduled field
+ // - queue field
+ // - queue.clear()
+
+ try {
+ Field newTasksMayBeScheduledField =
+ thread.getClass().getDeclaredField("newTasksMayBeScheduled");
+ newTasksMayBeScheduledField.setAccessible(true);
+ Field queueField = thread.getClass().getDeclaredField("queue");
+ queueField.setAccessible(true);
+
+ Object queue = queueField.get(thread);
+
+ Method clearMethod = queue.getClass().getDeclaredMethod("clear");
+ clearMethod.setAccessible(true);
+
+ synchronized(queue) {
+ newTasksMayBeScheduledField.setBoolean(thread, false);
+ clearMethod.invoke(queue);
+ queue.notify(); // In case queue was already empty.
+ }
+
+ log.error(sm.getString("webappClassLoader.warnTimerThread",
+ thread.getName()));
+
+ } catch (NoSuchFieldException e) {
+ log.warn(sm.getString(
+ "webappClassLoader.stopTimerThreadFail",
+ thread.getName()), e);
+ } catch (IllegalAccessException e) {
+ log.warn(sm.getString(
+ "webappClassLoader.stopTimerThreadFail",
+ thread.getName()), e);
+ } catch (NoSuchMethodException e) {
+ log.warn(sm.getString(
+ "webappClassLoader.stopTimerThreadFail",
+ thread.getName()), e);
+ } catch (InvocationTargetException e) {
+ log.warn(sm.getString(
+ "webappClassLoader.stopTimerThreadFail",
+ thread.getName()), e);
+ }
+ }
+
private void clearReferencesThreadLocals() {
Thread[] threads = getThreads();