public class ClassLoaderLeakPreventorListener extends Object implements javax.servlet.ServletContextListener
Activate protection by adding this class as a context listener
in your web.xml, like this:
<listener>
<listener-class>se.jiderhamn.classloader.leak.prevention.ClassLoaderLeakPreventor</listener-class>
</listener>
You should usually declare this listener before any other listeners, to make it "outermost".
web.xml,
i.e.:
<context-param>
<param-name>ClassLoaderLeakPreventor.stopThreads</param-name>
<param-value>false</param-value>
</context-param>
The available settings are
| Parameter name | Default value | Description |
|---|---|---|
ClassLoaderLeakPreventor.stopThreads |
true |
Should threads tied to the web app classloader be forced to stop at application shutdown? |
ClassLoaderLeakPreventor.stopTimerThreads |
true |
Should Timer threads tied to the web app classloader be forced to stop at application shutdown? |
ClassLoaderLeakPreventor.executeShutdownHooks |
true |
Should shutdown hooks registered from the application be executed at application shutdown? |
ClassLoaderLeakPreventor.threadWaitMs |
5000 (5 seconds) |
No of milliseconds to wait for threads to finish execution, before stopping them. |
ClassLoaderLeakPreventor.shutdownHookWaitMs |
10000 (10 seconds) |
No of milliseconds to wait for shutdown hooks to finish execution, before stopping them. If set to -1 there will be no waiting at all, but Thread is allowed to run until finished. |
This code is licensed under the Apache 2 license, which allows you to include modified versions of the code in your distributed software, without having to release your source code.
For more info, see here.
If you want to help improve this component, you should be aware of the design goals
Primary design goal: Zero dependencies. The component should build and run using nothing but the JDK and the Servlet API. Specifically we should not depend on any logging framework, since they are part of the problem. We also don't want to use any utility libraries, in order not to impose any further dependencies into any project that just wants to get rid of classloader leaks. Access to anything outside of the standard JDK (in order to prevent a known leak) should be managed with reflection.
Secondary design goal: Keep the runtime component in a single .java file. It should be possible to
just add this one .java file into your own source tree.
| Modifier and Type | Field and Description |
|---|---|
protected ClassLoaderLeakPreventor |
classLoaderLeakPreventor |
protected List<javax.servlet.ServletContextListener> |
otherListeners
Other
ServletContextListeners to use also |
| Constructor and Description |
|---|
ClassLoaderLeakPreventorListener() |
| Modifier and Type | Method and Description |
|---|---|
void |
contextDestroyed(javax.servlet.ServletContextEvent servletContextEvent) |
void |
contextInitialized(javax.servlet.ServletContextEvent servletContextEvent) |
protected static int |
getIntInitParameter(javax.servlet.ServletContext servletContext,
String parameterName,
int defaultValue)
Parse init parameter for integer value, returning default if not found or invalid
|
protected String |
getLogPrefix() |
protected void |
info(String s)
To "turn off" info logging override this method in a subclass and make that subclass method empty.
|
protected ClassLoaderLeakPreventor classLoaderLeakPreventor
protected final List<javax.servlet.ServletContextListener> otherListeners
ServletContextListeners to use alsopublic void contextInitialized(javax.servlet.ServletContextEvent servletContextEvent)
contextInitialized in interface javax.servlet.ServletContextListenerpublic void contextDestroyed(javax.servlet.ServletContextEvent servletContextEvent)
contextDestroyed in interface javax.servlet.ServletContextListenerprotected static int getIntInitParameter(javax.servlet.ServletContext servletContext,
String parameterName,
int defaultValue)
protected String getLogPrefix()
protected void info(String s)
Copyright © 2017. All rights reserved.