Ensure context cache stats are logged when ApplicationContext fails to load

Closes gh-30635
This commit is contained in:
Sam Brannen 2023-06-11 15:46:28 +02:00
parent 439bcd6715
commit 1a26e17f41
1 changed files with 37 additions and 34 deletions

View File

@ -109,49 +109,52 @@ public class DefaultCacheAwareContextLoaderDelegate implements CacheAwareContext
mergedContextConfiguration = replaceIfNecessary(mergedContextConfiguration); mergedContextConfiguration = replaceIfNecessary(mergedContextConfiguration);
synchronized (this.contextCache) { synchronized (this.contextCache) {
ApplicationContext context = this.contextCache.get(mergedContextConfiguration); ApplicationContext context = this.contextCache.get(mergedContextConfiguration);
if (context == null) { try {
try { if (context == null) {
if (mergedContextConfiguration instanceof AotMergedContextConfiguration aotMergedConfig) { try {
context = loadContextInAotMode(aotMergedConfig); if (mergedContextConfiguration instanceof AotMergedContextConfiguration aotMergedConfig) {
context = loadContextInAotMode(aotMergedConfig);
}
else {
context = loadContextInternal(mergedContextConfiguration);
}
if (logger.isTraceEnabled()) {
logger.trace("Storing ApplicationContext [%s] in cache under key %s".formatted(
System.identityHashCode(context), mergedContextConfiguration));
}
this.contextCache.put(mergedContextConfiguration, context);
} }
else { catch (Exception ex) {
context = loadContextInternal(mergedContextConfiguration); Throwable cause = ex;
} if (ex instanceof ContextLoadException cle) {
if (logger.isTraceEnabled()) { cause = cle.getCause();
logger.trace("Storing ApplicationContext [%s] in cache under key %s".formatted( for (ApplicationContextFailureProcessor contextFailureProcessor : this.contextFailureProcessors) {
System.identityHashCode(context), mergedContextConfiguration)); try {
} contextFailureProcessor.processLoadFailure(cle.getApplicationContext(), cause);
this.contextCache.put(mergedContextConfiguration, context); }
} catch (Throwable throwable) {
catch (Exception ex) { if (logger.isDebugEnabled()) {
Throwable cause = ex; logger.debug("Ignoring exception thrown from ApplicationContextFailureProcessor [%s]: %s"
if (ex instanceof ContextLoadException cle) { .formatted(contextFailureProcessor, throwable));
cause = cle.getCause(); }
for (ApplicationContextFailureProcessor contextFailureProcessor : this.contextFailureProcessors) {
try {
contextFailureProcessor.processLoadFailure(cle.getApplicationContext(), cause);
}
catch (Throwable throwable) {
if (logger.isDebugEnabled()) {
logger.debug("Ignoring exception thrown from ApplicationContextFailureProcessor [%s]: %s"
.formatted(contextFailureProcessor, throwable));
} }
} }
} }
throw new IllegalStateException(
"Failed to load ApplicationContext for " + mergedContextConfiguration, cause);
}
}
else {
if (logger.isTraceEnabled()) {
logger.trace("Retrieved ApplicationContext [%s] from cache with key %s".formatted(
System.identityHashCode(context), mergedContextConfiguration));
} }
throw new IllegalStateException(
"Failed to load ApplicationContext for " + mergedContextConfiguration, cause);
} }
} }
else { finally {
if (logger.isTraceEnabled()) { this.contextCache.logStatistics();
logger.trace("Retrieved ApplicationContext [%s] from cache with key %s".formatted(
System.identityHashCode(context), mergedContextConfiguration));
}
} }
this.contextCache.logStatistics();
return context; return context;
} }
} }