Browse Source

DefaultLifecycleProcessor properly counts dependent beans in same phase

Includes log level revision towards TRACE for cleaner DEBUG experience.

Issue: SPR-16901
Issue: SPR-16946
pull/1918/merge
Juergen Hoeller 6 years ago
parent
commit
b3ca2d50a2
  1. 26
      spring-context/src/main/java/org/springframework/context/support/DefaultLifecycleProcessor.java

26
spring-context/src/main/java/org/springframework/context/support/DefaultLifecycleProcessor.java

@ -19,6 +19,7 @@ package org.springframework.context.support;
import java.util.ArrayList; import java.util.ArrayList;
import java.util.Collections; import java.util.Collections;
import java.util.HashMap; import java.util.HashMap;
import java.util.HashSet;
import java.util.LinkedHashMap; import java.util.LinkedHashMap;
import java.util.LinkedHashSet; import java.util.LinkedHashSet;
import java.util.List; import java.util.List;
@ -174,8 +175,8 @@ public class DefaultLifecycleProcessor implements LifecycleProcessor, BeanFactor
} }
if (!bean.isRunning() && if (!bean.isRunning() &&
(!autoStartupOnly || !(bean instanceof SmartLifecycle) || ((SmartLifecycle) bean).isAutoStartup())) { (!autoStartupOnly || !(bean instanceof SmartLifecycle) || ((SmartLifecycle) bean).isAutoStartup())) {
if (logger.isDebugEnabled()) { if (logger.isTraceEnabled()) {
logger.debug("Starting bean '" + beanName + "' of type [" + bean.getClass() + "]"); logger.trace("Starting bean '" + beanName + "' of type [" + bean.getClass().getName() + "]");
} }
try { try {
bean.start(); bean.start();
@ -229,8 +230,9 @@ public class DefaultLifecycleProcessor implements LifecycleProcessor, BeanFactor
try { try {
if (bean.isRunning()) { if (bean.isRunning()) {
if (bean instanceof SmartLifecycle) { if (bean instanceof SmartLifecycle) {
if (logger.isDebugEnabled()) { if (logger.isTraceEnabled()) {
logger.debug("Asking bean '" + beanName + "' of type [" + bean.getClass() + "] to stop"); logger.trace("Asking bean '" + beanName + "' of type [" +
bean.getClass().getName() + "] to stop");
} }
countDownBeanNames.add(beanName); countDownBeanNames.add(beanName);
((SmartLifecycle) bean).stop(() -> { ((SmartLifecycle) bean).stop(() -> {
@ -242,8 +244,9 @@ public class DefaultLifecycleProcessor implements LifecycleProcessor, BeanFactor
}); });
} }
else { else {
if (logger.isDebugEnabled()) { if (logger.isTraceEnabled()) {
logger.debug("Stopping bean '" + beanName + "' of type [" + bean.getClass() + "]"); logger.trace("Stopping bean '" + beanName + "' of type [" +
bean.getClass().getName() + "]");
} }
bean.stop(); bean.stop();
if (logger.isDebugEnabled()) { if (logger.isDebugEnabled()) {
@ -354,9 +357,7 @@ public class DefaultLifecycleProcessor implements LifecycleProcessor, BeanFactor
} }
Collections.sort(this.members); Collections.sort(this.members);
for (LifecycleGroupMember member : this.members) { for (LifecycleGroupMember member : this.members) {
if (this.lifecycleBeans.containsKey(member.name)) { doStart(this.lifecycleBeans, member.name, this.autoStartupOnly);
doStart(this.lifecycleBeans, member.name, this.autoStartupOnly);
}
} }
} }
@ -370,8 +371,9 @@ public class DefaultLifecycleProcessor implements LifecycleProcessor, BeanFactor
this.members.sort(Collections.reverseOrder()); this.members.sort(Collections.reverseOrder());
CountDownLatch latch = new CountDownLatch(this.smartMemberCount); CountDownLatch latch = new CountDownLatch(this.smartMemberCount);
Set<String> countDownBeanNames = Collections.synchronizedSet(new LinkedHashSet<>()); Set<String> countDownBeanNames = Collections.synchronizedSet(new LinkedHashSet<>());
Set<String> lifecycleBeanNames = new HashSet<>(this.lifecycleBeans.keySet());
for (LifecycleGroupMember member : this.members) { for (LifecycleGroupMember member : this.members) {
if (this.lifecycleBeans.containsKey(member.name)) { if (lifecycleBeanNames.contains(member.name)) {
doStop(this.lifecycleBeans, member.name, latch, countDownBeanNames); doStop(this.lifecycleBeans, member.name, latch, countDownBeanNames);
} }
else if (member.bean instanceof SmartLifecycle) { else if (member.bean instanceof SmartLifecycle) {
@ -381,8 +383,8 @@ public class DefaultLifecycleProcessor implements LifecycleProcessor, BeanFactor
} }
try { try {
latch.await(this.timeout, TimeUnit.MILLISECONDS); latch.await(this.timeout, TimeUnit.MILLISECONDS);
if (latch.getCount() > 0 && !countDownBeanNames.isEmpty() && logger.isWarnEnabled()) { if (latch.getCount() > 0 && !countDownBeanNames.isEmpty() && logger.isInfoEnabled()) {
logger.warn("Failed to shut down " + countDownBeanNames.size() + " bean" + logger.info("Failed to shut down " + countDownBeanNames.size() + " bean" +
(countDownBeanNames.size() > 1 ? "s" : "") + " with phase value " + (countDownBeanNames.size() > 1 ? "s" : "") + " with phase value " +
this.phase + " within timeout of " + this.timeout + ": " + countDownBeanNames); this.phase + " within timeout of " + this.timeout + ": " + countDownBeanNames);
} }

Loading…
Cancel
Save