Skip to content

Commit 5677811

Browse files
committed
added dedicated onClose notification to differentiate between manual stop and close-driven stop
1 parent 4e35c59 commit 5677811

File tree

3 files changed

+18
-30
lines changed

3 files changed

+18
-30
lines changed

org.springframework.context/src/main/java/org/springframework/context/LifecycleProcessor.java

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,12 +18,21 @@
1818

1919
/**
2020
* Strategy interface for processing Lifecycle beans within the ApplicationContext.
21-
*
21+
*
2222
* @author Mark Fisher
23+
* @author Juergen Hoeller
2324
* @since 3.0
2425
*/
2526
public interface LifecycleProcessor extends Lifecycle {
2627

28+
/**
29+
* Notification of context refresh, e.g. for auto-starting components.
30+
*/
2731
void onRefresh();
2832

33+
/**
34+
* Notification of context close phase, e.g. for auto-stopping components.
35+
*/
36+
void onClose();
37+
2938
}

org.springframework.context/src/main/java/org/springframework/context/support/AbstractApplicationContext.java

Lines changed: 4 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,6 @@
2222
import java.util.ArrayList;
2323
import java.util.Collection;
2424
import java.util.Date;
25-
import java.util.LinkedHashMap;
2625
import java.util.LinkedHashSet;
2726
import java.util.List;
2827
import java.util.Locale;
@@ -50,7 +49,6 @@
5049
import org.springframework.context.ApplicationListener;
5150
import org.springframework.context.ConfigurableApplicationContext;
5251
import org.springframework.context.HierarchicalMessageSource;
53-
import org.springframework.context.Lifecycle;
5452
import org.springframework.context.LifecycleProcessor;
5553
import org.springframework.context.MessageSource;
5654
import org.springframework.context.MessageSourceAware;
@@ -955,7 +953,7 @@ protected void doClose() {
955953
}
956954

957955
// Stop all Lifecycle beans, to avoid delays during individual destruction.
958-
this.getLifecycleProcessor().stop();
956+
getLifecycleProcessor().onClose();
959957

960958
// Destroy all cached singletons in the context's BeanFactory.
961959
destroyBeans();
@@ -1177,40 +1175,17 @@ public Resource[] getResources(String locationPattern) throws IOException {
11771175
//---------------------------------------------------------------------
11781176

11791177
public void start() {
1180-
this.getLifecycleProcessor().start();
1178+
getLifecycleProcessor().start();
11811179
publishEvent(new ContextStartedEvent(this));
11821180
}
11831181

11841182
public void stop() {
1185-
this.getLifecycleProcessor().stop();
1183+
getLifecycleProcessor().stop();
11861184
publishEvent(new ContextStoppedEvent(this));
11871185
}
11881186

11891187
public boolean isRunning() {
1190-
for (Lifecycle lifecycle : getLifecycleBeans().values()) {
1191-
if (!lifecycle.isRunning()) {
1192-
return false;
1193-
}
1194-
}
1195-
return true;
1196-
}
1197-
1198-
/**
1199-
* Return a Map of all singleton beans that implement the
1200-
* Lifecycle interface in this context.
1201-
* @return Map of Lifecycle beans with bean name as key
1202-
*/
1203-
private Map<String, Lifecycle> getLifecycleBeans() {
1204-
ConfigurableListableBeanFactory beanFactory = getBeanFactory();
1205-
String[] beanNames = beanFactory.getSingletonNames();
1206-
Map<String, Lifecycle> beans = new LinkedHashMap<String, Lifecycle>();
1207-
for (String beanName : beanNames) {
1208-
Object bean = beanFactory.getSingleton(beanName);
1209-
if (bean instanceof Lifecycle) {
1210-
beans.put(beanName, (Lifecycle) bean);
1211-
}
1212-
}
1213-
return beans;
1188+
return getLifecycleProcessor().isRunning();
12141189
}
12151190

12161191

org.springframework.context/src/main/java/org/springframework/context/support/DefaultLifecycleProcessor.java

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -125,6 +125,10 @@ public void onRefresh() {
125125
}
126126
}
127127

128+
public void onClose() {
129+
stop();
130+
}
131+
128132
/**
129133
* Start the specified bean as part of the given set of Lifecycle beans,
130134
* making sure that any beans that it depends on are started first.

0 commit comments

Comments
 (0)