PL-7544 Stop logback correctly on context stop to prevent memory leaks with perf4j-async-stats-appender-sink-UICoalescingStatistics

This commit is contained in:
Yuriy Artamonov 2016-07-14 12:17:00 +04:00
parent fbf366433d
commit c8899805ac

View File

@ -17,6 +17,7 @@
package com.haulmont.cuba.core.sys; package com.haulmont.cuba.core.sys;
import ch.qos.logback.classic.LoggerContext;
import com.google.common.base.Splitter; import com.google.common.base.Splitter;
import org.apache.commons.io.IOUtils; import org.apache.commons.io.IOUtils;
import org.apache.commons.lang.StringUtils; import org.apache.commons.lang.StringUtils;
@ -43,7 +44,6 @@ import java.util.regex.Pattern;
/** /**
* Base class for {@link AppContext} loaders of web applications. * Base class for {@link AppContext} loaders of web applications.
*
*/ */
public abstract class AbstractWebAppContextLoader extends AbstractAppContextLoader implements ServletContextListener { public abstract class AbstractWebAppContextLoader extends AbstractAppContextLoader implements ServletContextListener {
@ -83,6 +83,10 @@ public abstract class AbstractWebAppContextLoader extends AbstractAppContextLoad
public void contextDestroyed(ServletContextEvent servletContextEvent) { public void contextDestroyed(ServletContextEvent servletContextEvent) {
AppContext.Internals.stopContext(); AppContext.Internals.stopContext();
AppContext.Internals.setApplicationContext(null); AppContext.Internals.setApplicationContext(null);
if (LoggerFactory.getILoggerFactory() instanceof LoggerContext) {
((LoggerContext) LoggerFactory.getILoggerFactory()).stop();
}
} }
protected void initAppComponents(ServletContext sc) { protected void initAppComponents(ServletContext sc) {
@ -127,7 +131,7 @@ public abstract class AbstractWebAppContextLoader extends AbstractAppContextLoad
StrTokenizer tokenizer = new StrTokenizer(propsConfigName); StrTokenizer tokenizer = new StrTokenizer(propsConfigName);
tokenizer.setQuoteChar('"'); tokenizer.setQuoteChar('"');
for (String str : tokenizer.getTokenArray()) { for (String str : tokenizer.getTokenArray()) {
log.trace("Processing properties location: " + str); log.trace("Processing properties location: {}", str);
InputStream stream = null; InputStream stream = null;
try { try {
if (ResourceUtils.isUrl(str) || str.startsWith(ResourceLoader.CLASSPATH_URL_PREFIX)) { if (ResourceUtils.isUrl(str) || str.startsWith(ResourceLoader.CLASSPATH_URL_PREFIX)) {
@ -139,13 +143,13 @@ public abstract class AbstractWebAppContextLoader extends AbstractAppContextLoad
} }
if (stream != null) { if (stream != null) {
log.trace("Loading app properties from " + str); log.trace("Loading app properties from {}", str);
try (Reader reader = new InputStreamReader(stream, StandardCharsets.UTF_8.name())) { try (Reader reader = new InputStreamReader(stream, StandardCharsets.UTF_8)) {
properties.load(reader); properties.load(reader);
} }
} else } else {
log.trace("Resource " + str + " not found, ignore it"); log.trace("Resource {} not found, ignore it", str);
}
} catch (IOException e) { } catch (IOException e) {
throw new RuntimeException(e); throw new RuntimeException(e);
} finally { } finally {