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. 24
      spring-context/src/main/java/org/springframework/context/support/DefaultLifecycleProcessor.java

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

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

Loading…
Cancel
Save