Skip to content

Commit 2ec8fa9

Browse files
committed
SmartLifecycle default methods for auto-startup in default phase
Issue: SPR-17188
1 parent 446a604 commit 2ec8fa9

File tree

15 files changed

+97
-144
lines changed

15 files changed

+97
-144
lines changed

spring-context-support/src/main/java/org/springframework/scheduling/quartz/SchedulerFactoryBean.java

Lines changed: 1 addition & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -207,7 +207,7 @@ public static DataSource getConfigTimeNonTransactionalDataSource() {
207207

208208
private int startupDelay = 0;
209209

210-
private int phase = Integer.MAX_VALUE;
210+
private int phase = DEFAULT_PHASE;
211211

212212
private boolean exposeSchedulerInRepository = false;
213213

@@ -796,12 +796,6 @@ public void stop() throws SchedulingException {
796796
}
797797
}
798798

799-
@Override
800-
public void stop(Runnable callback) throws SchedulingException {
801-
stop();
802-
callback.run();
803-
}
804-
805799
@Override
806800
public boolean isRunning() throws SchedulingException {
807801
if (this.scheduler != null) {

spring-context/src/main/java/org/springframework/context/Lifecycle.java

Lines changed: 12 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2002-2015 the original author or authors.
2+
* Copyright 2002-2018 the original author or authors.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -34,10 +34,11 @@
3434
* restricting the visibility of activity-controlled components to the Lifecycle
3535
* interface.
3636
*
37-
* <p>Note that the Lifecycle interface is only supported on <b>top-level singleton
38-
* beans</b>. On any other component, the Lifecycle interface will remain undetected
39-
* and hence ignored. Also, note that the extended {@link SmartLifecycle} interface
40-
* provides integration with the application context's startup and shutdown phases.
37+
* <p>Note that the present {@code Lifecycle} interface is only supported on
38+
* <b>top-level singleton beans</b>. On any other component, the {@code Lifecycle}
39+
* interface will remain undetected and hence ignored. Also, note that the extended
40+
* {@link SmartLifecycle} interface provides sophisticated integration with the
41+
* application context's startup and shutdown phases.
4142
*
4243
* @author Juergen Hoeller
4344
* @since 2.0
@@ -61,11 +62,12 @@ public interface Lifecycle {
6162
* Stop this component, typically in a synchronous fashion, such that the component is
6263
* fully stopped upon return of this method. Consider implementing {@link SmartLifecycle}
6364
* and its {@code stop(Runnable)} variant when asynchronous stop behavior is necessary.
64-
* <p>Note that this stop notification is not guaranteed to come before destruction: On
65-
* regular shutdown, {@code Lifecycle} beans will first receive a stop notification before
66-
* the general destruction callbacks are being propagated; however, on hot refresh during a
67-
* context's lifetime or on aborted refresh attempts, only destroy methods will be called.
68-
* <p>Should not throw an exception if the component isn't started yet.
65+
* <p>Note that this stop notification is not guaranteed to come before destruction:
66+
* On regular shutdown, {@code Lifecycle} beans will first receive a stop notification
67+
* before the general destruction callbacks are being propagated; however, on hot
68+
* refresh during a context's lifetime or on aborted refresh attempts, a given bean's
69+
* destroy method will be called without any consideration of stop signals upfront.
70+
* <p>Should not throw an exception if the component is not running (not started yet).
6971
* <p>In the case of a container, this will propagate the stop signal to all components
7072
* that apply.
7173
* @see SmartLifecycle#stop(Runnable)
Lines changed: 61 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2002-2015 the original author or authors.
2+
* Copyright 2002-2018 the original author or authors.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -23,56 +23,72 @@
2323
* be started at the time of a context refresh. The callback-accepting
2424
* {@link #stop(Runnable)} method is useful for objects that have an asynchronous
2525
* shutdown process. Any implementation of this interface <i>must</i> invoke the
26-
* callback's run() method upon shutdown completion to avoid unnecessary delays
27-
* in the overall ApplicationContext shutdown.
26+
* callback's {@code run()} method upon shutdown completion to avoid unnecessary
27+
* delays in the overall ApplicationContext shutdown.
2828
*
2929
* <p>This interface extends {@link Phased}, and the {@link #getPhase()} method's
3030
* return value indicates the phase within which this Lifecycle component should
31-
* be started and stopped. The startup process begins with the <i>lowest</i>
32-
* phase value and ends with the <i>highest</i> phase value (Integer.MIN_VALUE
33-
* is the lowest possible, and Integer.MAX_VALUE is the highest possible). The
34-
* shutdown process will apply the reverse order. Any components with the
31+
* be started and stopped. The startup process begins with the <i>lowest</i> phase
32+
* value and ends with the <i>highest</i> phase value ({@code Integer.MIN_VALUE}
33+
* is the lowest possible, and {@code Integer.MAX_VALUE} is the highest possible).
34+
* The shutdown process will apply the reverse order. Any components with the
3535
* same value will be arbitrarily ordered within the same phase.
3636
*
37-
* <p>Example: if component B depends on component A having already started, then
38-
* component A should have a lower phase value than component B. During the
39-
* shutdown process, component B would be stopped before component A.
37+
* <p>Example: if component B depends on component A having already started,
38+
* then component A should have a lower phase value than component B. During
39+
* the shutdown process, component B would be stopped before component A.
4040
*
41-
* <p>Any explicit "depends-on" relationship will take precedence over
42-
* the phase order such that the dependent bean always starts after its
43-
* dependency and always stops before its dependency.
41+
* <p>Any explicit "depends-on" relationship will take precedence over the phase
42+
* order such that the dependent bean always starts after its dependency and
43+
* always stops before its dependency.
4444
*
45-
* <p>Any Lifecycle components within the context that do not also implement
46-
* SmartLifecycle will be treated as if they have a phase value of 0. That
47-
* way a SmartLifecycle implementation may start before those Lifecycle
48-
* components if it has a negative phase value, or it may start after
49-
* those components if it has a positive phase value.
45+
* <p>Any {@code Lifecycle} components within the context that do not also
46+
* implement {@code SmartLifecycle} will be treated as if they have a phase
47+
* value of 0. That way a {@code SmartLifecycle} implementation may start
48+
* before those {@code Lifecycle} components if it has a negative phase value,
49+
* or it may start after those components if it has a positive phase value.
5050
*
51-
* <p>Note that, due to the auto-startup support in SmartLifecycle,
52-
* a SmartLifecycle bean instance will get initialized on startup of the
53-
* application context in any case. As a consequence, the bean definition
54-
* lazy-init flag has very limited actual effect on SmartLifecycle beans.
51+
* <p>Note that, due to the auto-startup support in {@code SmartLifecycle}, a
52+
* {@code SmartLifecycle} bean instance will usually get initialized on startup
53+
* of the application context in any case. As a consequence, the bean definition
54+
* lazy-init flag has very limited actual effect on {@code SmartLifecycle} beans.
5555
*
5656
* @author Mark Fisher
57+
* @author Juergen Hoeller
5758
* @since 3.0
5859
* @see LifecycleProcessor
5960
* @see ConfigurableApplicationContext
6061
*/
6162
public interface SmartLifecycle extends Lifecycle, Phased {
6263

64+
/**
65+
* The default phase for {@code SmartLifecycle}: {@code Integer.MAX_VALUE}.
66+
* <p>This is different from the common phase 0 associated with regular
67+
* {@link Lifecycle} implementations, putting the typically auto-started
68+
* {@code SmartLifecycle} beans into a separate later shutdown phase.
69+
* @since 5.1
70+
* @see #getPhase()
71+
* @see org.springframework.context.support.DefaultLifecycleProcessor#getPhase(Lifecycle)
72+
*/
73+
int DEFAULT_PHASE = Integer.MAX_VALUE;
74+
75+
6376
/**
6477
* Returns {@code true} if this {@code Lifecycle} component should get
6578
* started automatically by the container at the time that the containing
6679
* {@link ApplicationContext} gets refreshed.
6780
* <p>A value of {@code false} indicates that the component is intended to
6881
* be started through an explicit {@link #start()} call instead, analogous
6982
* to a plain {@link Lifecycle} implementation.
83+
* <p>The default implementation returns {@code true}.
7084
* @see #start()
7185
* @see #getPhase()
7286
* @see LifecycleProcessor#onRefresh()
7387
* @see ConfigurableApplicationContext#refresh()
7488
*/
75-
boolean isAutoStartup();
89+
default boolean isAutoStartup() {
90+
return true;
91+
}
7692

7793
/**
7894
* Indicates that a Lifecycle component must stop if it is currently running.
@@ -84,9 +100,30 @@ public interface SmartLifecycle extends Lifecycle, Phased {
84100
* {@code stop} method; i.e. {@link Lifecycle#stop()} will not be called for
85101
* {@code SmartLifecycle} implementations unless explicitly delegated to within
86102
* the implementation of this method.
103+
* <p>The default implementation delegates to {@link #stop()} and immediately
104+
* triggers the given callback in the calling thread. Note that there is no
105+
* synchronization between the two, so custom implementations may at least
106+
* want to put the same steps within their common lifecycle monitor (if any).
87107
* @see #stop()
88108
* @see #getPhase()
89109
*/
90-
void stop(Runnable callback);
110+
default void stop(Runnable callback) {
111+
stop();
112+
callback.run();
113+
}
114+
115+
/**
116+
* Return the phase that this lifecycle object is supposed to run in.
117+
* <p>The default implementation returns {@link #DEFAULT_PHASE} in order to
118+
* let stop callbacks execute after regular {@code Lifecycle} implementations.
119+
* @see #isAutoStartup()
120+
* @see #start()
121+
* @see #stop(Runnable)
122+
* @see org.springframework.context.support.DefaultLifecycleProcessor#getPhase(Lifecycle)
123+
*/
124+
@Override
125+
default int getPhase() {
126+
return DEFAULT_PHASE;
127+
}
91128

92129
}

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

Lines changed: 11 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -195,11 +195,11 @@ private void stopBeans() {
195195
Map<String, Lifecycle> lifecycleBeans = getLifecycleBeans();
196196
Map<Integer, LifecycleGroup> phases = new HashMap<>();
197197
lifecycleBeans.forEach((beanName, bean) -> {
198-
int shutdownOrder = getPhase(bean);
199-
LifecycleGroup group = phases.get(shutdownOrder);
198+
int shutdownPhase = getPhase(bean);
199+
LifecycleGroup group = phases.get(shutdownPhase);
200200
if (group == null) {
201-
group = new LifecycleGroup(shutdownOrder, this.timeoutPerShutdownPhase, lifecycleBeans, false);
202-
phases.put(shutdownOrder, group);
201+
group = new LifecycleGroup(shutdownPhase, this.timeoutPerShutdownPhase, lifecycleBeans, false);
202+
phases.put(shutdownPhase, group);
203203
}
204204
group.add(beanName, bean);
205205
});
@@ -302,11 +302,11 @@ private boolean matchesBeanType(Class<?> targetType, String beanName, BeanFactor
302302

303303
/**
304304
* Determine the lifecycle phase of the given bean.
305-
* <p>The default implementation checks for the {@link Phased} interface.
306-
* Can be overridden to apply other/further policies.
305+
* <p>The default implementation checks for the {@link Phased} interface, using
306+
* a default of 0 otherwise. Can be overridden to apply other/further policies.
307307
* @param bean the bean to introspect
308-
* @return the phase an integer value. The suggested default is 0.
309-
* @see Phased
308+
* @return the phase (an integer value)
309+
* @see Phased#getPhase()
310310
* @see SmartLifecycle
311311
*/
312312
protected int getPhase(Lifecycle bean) {
@@ -412,9 +412,9 @@ private class LifecycleGroupMember implements Comparable<LifecycleGroupMember> {
412412

413413
@Override
414414
public int compareTo(LifecycleGroupMember other) {
415-
int thisOrder = getPhase(this.bean);
416-
int otherOrder = getPhase(other.bean);
417-
return Integer.compare(thisOrder, otherOrder);
415+
int thisPhase = getPhase(this.bean);
416+
int otherPhase = getPhase(other.bean);
417+
return Integer.compare(thisPhase, otherPhase);
418418
}
419419
}
420420

spring-jms/src/main/java/org/springframework/jms/config/JmsListenerEndpointRegistry.java

Lines changed: 1 addition & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -66,7 +66,7 @@ public class JmsListenerEndpointRegistry implements DisposableBean, SmartLifecyc
6666
private final Map<String, MessageListenerContainer> listenerContainers =
6767
new ConcurrentHashMap<>();
6868

69-
private int phase = Integer.MAX_VALUE;
69+
private int phase = DEFAULT_PHASE;
7070

7171
@Nullable
7272
private ApplicationContext applicationContext;
@@ -198,11 +198,6 @@ public int getPhase() {
198198
return this.phase;
199199
}
200200

201-
@Override
202-
public boolean isAutoStartup() {
203-
return true;
204-
}
205-
206201
@Override
207202
public void start() {
208203
for (MessageListenerContainer listenerContainer : getListenerContainers()) {

spring-jms/src/main/java/org/springframework/jms/listener/AbstractJmsListeningContainer.java

Lines changed: 1 addition & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -66,7 +66,7 @@ public abstract class AbstractJmsListeningContainer extends JmsDestinationAccess
6666

6767
private boolean autoStartup = true;
6868

69-
private int phase = Integer.MAX_VALUE;
69+
private int phase = DEFAULT_PHASE;
7070

7171
@Nullable
7272
private String beanName;
@@ -319,12 +319,6 @@ public void stop() throws JmsException {
319319
}
320320
}
321321

322-
@Override
323-
public void stop(Runnable callback) {
324-
stop();
325-
callback.run();
326-
}
327-
328322
/**
329323
* Notify all invoker tasks and stop the shared Connection, if any.
330324
* @throws JMSException if thrown by JMS API methods

spring-messaging/src/main/java/org/springframework/messaging/simp/annotation/support/SimpAnnotationMethodMessageHandler.java

Lines changed: 0 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -268,16 +268,6 @@ public MessageHeaderInitializer getHeaderInitializer() {
268268
}
269269

270270

271-
@Override
272-
public boolean isAutoStartup() {
273-
return true;
274-
}
275-
276-
@Override
277-
public int getPhase() {
278-
return Integer.MAX_VALUE;
279-
}
280-
281271
@Override
282272
public final void start() {
283273
synchronized (this.lifecycleMonitor) {

spring-messaging/src/main/java/org/springframework/messaging/simp/broker/AbstractBrokerMessageHandler.java

Lines changed: 0 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -173,11 +173,6 @@ public boolean isAutoStartup() {
173173
return this.autoStartup;
174174
}
175175

176-
@Override
177-
public int getPhase() {
178-
return Integer.MAX_VALUE;
179-
}
180-
181176

182177
@Override
183178
public void start() {

spring-messaging/src/main/java/org/springframework/messaging/simp/user/UserDestinationMessageHandler.java

Lines changed: 0 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -147,16 +147,6 @@ public MessageHeaderInitializer getHeaderInitializer() {
147147
}
148148

149149

150-
@Override
151-
public int getPhase() {
152-
return Integer.MAX_VALUE;
153-
}
154-
155-
@Override
156-
public boolean isAutoStartup() {
157-
return true;
158-
}
159-
160150
@Override
161151
public final void start() {
162152
synchronized (this.lifecycleMonitor) {

spring-tx/src/main/java/org/springframework/jca/endpoint/GenericMessageEndpointManager.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -159,7 +159,7 @@ public class GenericMessageEndpointManager implements SmartLifecycle, Initializi
159159

160160
private boolean autoStartup = true;
161161

162-
private int phase = Integer.MAX_VALUE;
162+
private int phase = DEFAULT_PHASE;
163163

164164
private volatile boolean running = false;
165165

0 commit comments

Comments
 (0)