• Home
  • Features
  • Pricing
  • Docs
  • Announcements
  • Sign In

TAKETODAY / today-infrastructure / 17175530323

23 Aug 2025 12:36PM UTC coverage: 81.854% (+0.009%) from 81.845%
17175530323

push

github

TAKETODAY
:art:

59686 of 77901 branches covered (76.62%)

Branch coverage included in aggregate %.

141140 of 167446 relevant lines covered (84.29%)

3.6 hits per line

Source File
Press 'n' to go to next uncovered line, 'b' for previous

83.33
today-context/src/main/java/infra/context/SmartLifecycle.java
1
/*
2
 * Copyright 2017 - 2025 the original author or authors.
3
 *
4
 * This program is free software: you can redistribute it and/or modify
5
 * it under the terms of the GNU General Public License as published by
6
 * the Free Software Foundation, either version 3 of the License, or
7
 * (at your option) any later version.
8
 *
9
 * This program is distributed in the hope that it will be useful,
10
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
12
 * GNU General Public License for more details.
13
 *
14
 * You should have received a copy of the GNU General Public License
15
 * along with this program. If not, see [https://www.gnu.org/licenses/]
16
 */
17

18
package infra.context;
19

20
import infra.context.support.DefaultLifecycleProcessor;
21
import infra.scheduling.concurrent.ExecutorConfigurationSupport;
22

23
/**
24
 * An extension of the {@link Lifecycle} interface for those objects that require
25
 * to be started upon {@code ApplicationContext} refresh and/or shutdown in a
26
 * particular order.
27
 *
28
 * <p>The {@link #isAutoStartup()} return value indicates whether this object should
29
 * be started at the time of a context refresh. The callback-accepting
30
 * {@link #stop(Runnable)} method is useful for objects that have an asynchronous
31
 * shutdown process. Any implementation of this interface <i>must</i> invoke the
32
 * callback's {@code run()} method upon shutdown completion to avoid unnecessary
33
 * delays in the overall {@code ApplicationContext} shutdown.
34
 *
35
 * <p>This interface extends {@link Phased}, and the {@link #getPhase()} method's
36
 * return value indicates the phase within which this {@code Lifecycle} component
37
 * should be started and stopped. The startup process begins with the <i>lowest</i>
38
 * phase value and ends with the <i>highest</i> phase value ({@code Integer.MIN_VALUE}
39
 * is the lowest possible, and {@code Integer.MAX_VALUE} is the highest possible).
40
 * The shutdown process will apply the reverse order. Any components with the
41
 * same value will be arbitrarily ordered within the same phase.
42
 *
43
 * <p>Example: if component B depends on component A having already started,
44
 * then component A should have a lower phase value than component B. During
45
 * the shutdown process, component B would be stopped before component A.
46
 *
47
 * <p>Any explicit "depends-on" relationship will take precedence over the phase
48
 * order such that the dependent bean always starts after its dependency and
49
 * always stops before its dependency.
50
 *
51
 * <p>Any {@code Lifecycle} components within the context that do not also
52
 * implement {@code SmartLifecycle} will be treated as if they have a phase
53
 * value of {@code 0}. This allows a {@code SmartLifecycle} component to start
54
 * before those {@code Lifecycle} components if the {@code SmartLifecycle}
55
 * component has a negative phase value, or the {@code SmartLifecycle} component
56
 * may start after those {@code Lifecycle} components if the {@code SmartLifecycle}
57
 * component has a positive phase value.
58
 *
59
 * <p>Note that, due to the auto-startup support in {@code SmartLifecycle}, a
60
 * {@code SmartLifecycle} bean instance will usually get initialized on startup
61
 * of the application context in any case. As a consequence, the bean definition
62
 * lazy-init flag has very limited actual effect on {@code SmartLifecycle} beans.
63
 *
64
 * @author Mark Fisher
65
 * @author Juergen Hoeller
66
 * @author Sam Brannen
67
 * @author <a href="https://github.com/TAKETODAY">海子 Yang</a>
68
 * @see LifecycleProcessor
69
 * @see ConfigurableApplicationContext
70
 * @since 4.0 2021/11/12 16:28
71
 */
72
public interface SmartLifecycle extends Lifecycle, Phased {
73

74
  /**
75
   * The default phase for {@code SmartLifecycle}: {@code Integer.MAX_VALUE}.
76
   * <p>This is different from the common phase {@code 0} associated with regular
77
   * {@link Lifecycle} implementations, putting the typically auto-started
78
   * {@code SmartLifecycle} beans into a later startup phase and an earlier
79
   * shutdown phase.
80
   * <p>Note that certain {@code SmartLifecycle} components come with a different
81
   * default phase: e.g. executors/schedulers with {@code Integer.MAX_VALUE / 2}.
82
   *
83
   * @see #getPhase()
84
   * @see ExecutorConfigurationSupport#DEFAULT_PHASE
85
   * @see DefaultLifecycleProcessor#setTimeoutPerShutdownPhase
86
   */
87
  int DEFAULT_PHASE = Integer.MAX_VALUE;
88

89
  /**
90
   * Returns {@code true} if this {@code Lifecycle} component should get
91
   * started automatically by the container at the time that the containing
92
   * {@link ApplicationContext} gets refreshed or restarted.
93
   * <p>A value of {@code false} indicates that the component is intended to
94
   * be started through an explicit {@link #start()} call instead, analogous
95
   * to a plain {@link Lifecycle} implementation.
96
   * <p>The default implementation returns {@code true}.
97
   *
98
   * @see #start()
99
   * @see #getPhase()
100
   * @see LifecycleProcessor#onRefresh()
101
   * @see LifecycleProcessor#onRestart()
102
   * @see ConfigurableApplicationContext#refresh()
103
   * @see ConfigurableApplicationContext#restart()
104
   */
105
  default boolean isAutoStartup() {
106
    return true;
2✔
107
  }
108

109
  /**
110
   * Returns {@code true} if this {@code Lifecycle} component is able to
111
   * participate in a restart sequence, receiving corresponding {@link #stop()}
112
   * and {@link #start()} calls with a potential pause in-between.
113
   * <p>A value of {@code false} indicates that the component prefers to
114
   * be skipped in a pause scenario, neither receiving a {@link #stop()}
115
   * call nor a subsequent {@link #start()} call, analogous to a plain
116
   * {@link Lifecycle} implementation. It will only receive a {@link #stop()}
117
   * call on close and on explicit context-wide stopping but not on pause.
118
   * <p>The default implementation returns {@code true}.
119
   *
120
   * @see #stop()
121
   * @see LifecycleProcessor#onPause()
122
   * @see LifecycleProcessor#onClose()
123
   * @see ConfigurableApplicationContext#pause()
124
   * @see ConfigurableApplicationContext#close()
125
   * @since 5.0
126
   */
127
  default boolean isPausable() {
128
    return true;
×
129
  }
130

131
  /**
132
   * Indicates that a Lifecycle component must stop if it is currently running.
133
   * <p>The provided callback is used by the {@link LifecycleProcessor} to support
134
   * an ordered, and potentially concurrent, shutdown of all components having a
135
   * common shutdown order value. The callback <b>must</b> be executed after
136
   * the {@code SmartLifecycle} component does indeed stop.
137
   * <p>The {@link LifecycleProcessor} will call <i>only</i> this variant of the
138
   * {@code stop} method; i.e. {@link Lifecycle#stop()} will not be called for
139
   * {@code SmartLifecycle} implementations unless explicitly delegated to within
140
   * the implementation of this method.
141
   * <p>The default implementation delegates to {@link #stop()} and immediately
142
   * triggers the given callback in the calling thread. Note that there is no
143
   * synchronization between the two, so custom implementations may at least
144
   * want to put the same steps within their common lifecycle monitor (if any).
145
   *
146
   * @see #stop()
147
   * @see #getPhase()
148
   */
149
  default void stop(Runnable callback) {
150
    stop();
2✔
151
    callback.run();
2✔
152
  }
1✔
153

154
  /**
155
   * Return the phase that this lifecycle object is supposed to run in.
156
   * <p>The default implementation returns {@link #DEFAULT_PHASE} in order to
157
   * let {@code stop()} callbacks execute after regular {@code Lifecycle}
158
   * implementations.
159
   *
160
   * @see #isAutoStartup()
161
   * @see #start()
162
   * @see #stop(Runnable)
163
   * @see DefaultLifecycleProcessor#getPhase(Lifecycle)
164
   */
165
  @Override
166
  default int getPhase() {
167
    return DEFAULT_PHASE;
2✔
168
  }
169

170
}
STATUS · Troubleshooting · Open an Issue · Sales · Support · CAREERS · ENTERPRISE · START FREE TRIAL · SCHEDULE DEMO
ANNOUNCEMENTS · TWITTER · TOS & SLA · Supported CI Services · What's a CI service? · Automated Testing

© 2026 Coveralls, Inc