use SimpleClassLoaderLifeCycle by default

This commit is contained in:
Sebastian Sdorra
2020-02-12 12:36:37 +01:00
parent 56b8dbdb22
commit bca34b829d
6 changed files with 22 additions and 19 deletions

View File

@@ -200,10 +200,6 @@
<name>java.awt.headless</name>
<value>true</value>
</systemProperty>
<systemProperty>
<name>sonia.scm.classloading.lifecycle</name>
<value>simple</value>
</systemProperty>
</systemProperties>
<webApp>
<contextPath>/scm</contextPath>

View File

@@ -734,10 +734,6 @@
<name>scm.stage</name>
<value>${scm.stage}</value>
</systemProperty>
<systemProperty>
<name>sonia.scm.classloading.lifecycle</name>
<value>simple</value>
</systemProperty>
</systemProperties>
<jettyXml>${project.basedir}/src/main/conf/jetty.xml</jettyXml>
<scanIntervalSeconds>0</scanIntervalSeconds>
@@ -823,10 +819,6 @@
<name>scm.home</name>
<value>target/scm-it</value>
</systemProperty>
<systemProperty>
<name>sonia.scm.classloading.lifecycle</name>
<value>simple</value>
</systemProperty>
</systemProperties>
<jettyXml>${project.basedir}/src/main/conf/jetty.xml</jettyXml>
<scanIntervalSeconds>0</scanIntervalSeconds>

View File

@@ -11,6 +11,8 @@ import java.util.concurrent.atomic.AtomicLong;
/**
* Restart strategy which tries to free, every resource used by the context, starts gc and re initializes the context.
* <strong>Warning: </strong> This strategy should only be used with an classloader lifecycle which protects the
* created plugin classloader from classloader leaks.
*/
class InjectionContextRestartStrategy implements RestartStrategy {

View File

@@ -19,18 +19,18 @@ public abstract class ClassLoaderLifeCycle implements LifeCycle {
private static final Logger LOG = LoggerFactory.getLogger(ClassLoaderLifeCycle.class);
@VisibleForTesting
static final String PROPERTY = "sonia.scm.classloading.lifecycle";
static final String PROPERTY = "sonia.scm.lifecycle.classloading";
public static ClassLoaderLifeCycle create() {
ClassLoader webappClassLoader = Thread.currentThread().getContextClassLoader();
String implementation = System.getProperty(PROPERTY);
if (SimpleClassLoaderLifeCycle.NAME.equalsIgnoreCase(implementation)) {
LOG.info("create new simple ClassLoaderLifeCycle");
return new SimpleClassLoaderLifeCycle(webappClassLoader);
}
if (ClassLoaderLifeCycleWithLeakPrevention.NAME.equalsIgnoreCase(implementation)) {
LOG.info("create new ClassLoaderLifeCycle with leak prevention");
return new ClassLoaderLifeCycleWithLeakPrevention(webappClassLoader);
}
LOG.info("create new simple ClassLoaderLifeCycle");
return new SimpleClassLoaderLifeCycle(webappClassLoader);
}
private final ClassLoader webappClassLoader;

View File

@@ -26,6 +26,8 @@ import static se.jiderhamn.classloader.leak.prevention.cleanup.ShutdownHookClean
*/
final class ClassLoaderLifeCycleWithLeakPrevention extends ClassLoaderLifeCycle {
public static final String NAME = "with-leak-prevention";
private static final Logger LOG = LoggerFactory.getLogger(ClassLoaderLifeCycleWithLeakPrevention.class);
private Deque<ClassLoaderAndPreventor> classLoaders = new ArrayDeque<>();

View File

@@ -18,8 +18,19 @@ class ClassLoaderLifeCycleTest {
}
@Test
void shouldCreateDefaultClassLoader() {
void shouldCreateWithLeakPreventionClassLoader() {
System.setProperty(ClassLoaderLifeCycle.PROPERTY, ClassLoaderLifeCycleWithLeakPrevention.NAME);
try {
ClassLoaderLifeCycle classLoaderLifeCycle = ClassLoaderLifeCycle.create();
assertThat(classLoaderLifeCycle).isInstanceOf(ClassLoaderLifeCycleWithLeakPrevention.class);
} finally {
System.clearProperty(ClassLoaderLifeCycle.PROPERTY);
}
}
@Test
void shouldCreateDefaultClassLoader() {
ClassLoaderLifeCycle classLoaderLifeCycle = ClassLoaderLifeCycle.create();
assertThat(classLoaderLifeCycle).isInstanceOf(SimpleClassLoaderLifeCycle.class);
}
}