Skip to content

Commit

Permalink
Ensure context cache stats are logged when ApplicationContext fails t…
Browse files Browse the repository at this point in the history
…o load

Closes gh-30635
  • Loading branch information
sbrannen committed Jun 11, 2023
1 parent 439bcd6 commit 1a26e17
Showing 1 changed file with 37 additions and 34 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -109,48 +109,51 @@ public ApplicationContext loadContext(MergedContextConfiguration mergedContextCo
mergedContextConfiguration = replaceIfNecessary(mergedContextConfiguration);
synchronized (this.contextCache) {
ApplicationContext context = this.contextCache.get(mergedContextConfiguration);
if (context == null) {
try {
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));
try {
if (context == null) {
try {
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);
}
this.contextCache.put(mergedContextConfiguration, context);
}
catch (Exception ex) {
Throwable cause = ex;
if (ex instanceof ContextLoadException cle) {
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));
catch (Exception ex) {
Throwable cause = ex;
if (ex instanceof ContextLoadException cle) {
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);
}
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));
else {
if (logger.isTraceEnabled()) {
logger.trace("Retrieved ApplicationContext [%s] from cache with key %s".formatted(
System.identityHashCode(context), mergedContextConfiguration));
}
}
}

this.contextCache.logStatistics();
finally {
this.contextCache.logStatistics();
}

return context;
}
Expand Down

0 comments on commit 1a26e17

Please sign in to comment.