Skip to content

Commit 6791ea9

Browse files
committed
Change executor phase to MAX_VALUE/2 and reduce timeout to 10 seconds
Closes gh-32152
1 parent 7ee8e66 commit 6791ea9

File tree

4 files changed

+32
-5
lines changed

4 files changed

+32
-5
lines changed

Diff for: spring-context/src/main/java/org/springframework/context/SmartLifecycle.java

+4-1
Original file line numberDiff line numberDiff line change
@@ -72,9 +72,12 @@ public interface SmartLifecycle extends Lifecycle, Phased {
7272
* {@link Lifecycle} implementations, putting the typically auto-started
7373
* {@code SmartLifecycle} beans into a later startup phase and an earlier
7474
* shutdown phase.
75+
* <p>Note that certain {@code SmartLifecycle} components come with a different
76+
* default phase: e.g. executors/schedulers with {@code Integer.MAX_VALUE / 2}.
7577
* @since 5.1
7678
* @see #getPhase()
77-
* @see org.springframework.context.support.DefaultLifecycleProcessor#getPhase(Lifecycle)
79+
* @see org.springframework.scheduling.concurrent.ExecutorConfigurationSupport#DEFAULT_PHASE
80+
* @see org.springframework.context.support.DefaultLifecycleProcessor#setTimeoutPerShutdownPhase
7881
*/
7982
int DEFAULT_PHASE = Integer.MAX_VALUE;
8083

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

+2-2
Original file line numberDiff line numberDiff line change
@@ -106,7 +106,7 @@ public class DefaultLifecycleProcessor implements LifecycleProcessor, BeanFactor
106106

107107
private final Log logger = LogFactory.getLog(getClass());
108108

109-
private volatile long timeoutPerShutdownPhase = 30000;
109+
private volatile long timeoutPerShutdownPhase = 10000;
110110

111111
private volatile boolean running;
112112

@@ -135,7 +135,7 @@ else if (checkpointOnRefresh) {
135135
/**
136136
* Specify the maximum time allotted in milliseconds for the shutdown of any
137137
* phase (group of {@link SmartLifecycle} beans with the same 'phase' value).
138-
* <p>The default value is 30000 milliseconds (30 seconds).
138+
* <p>The default value is 10000 milliseconds (10 seconds) as of 6.2.
139139
* @see SmartLifecycle#getPhase()
140140
*/
141141
public void setTimeoutPerShutdownPhase(long timeoutPerShutdownPhase) {

Diff for: spring-context/src/main/java/org/springframework/scheduling/concurrent/ExecutorConfigurationSupport.java

+17-1
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,7 @@
3333
import org.springframework.context.ApplicationContext;
3434
import org.springframework.context.ApplicationContextAware;
3535
import org.springframework.context.ApplicationListener;
36+
import org.springframework.context.Lifecycle;
3637
import org.springframework.context.SmartLifecycle;
3738
import org.springframework.context.event.ContextClosedEvent;
3839
import org.springframework.lang.Nullable;
@@ -58,6 +59,20 @@ public abstract class ExecutorConfigurationSupport extends CustomizableThreadFac
5859
implements BeanNameAware, ApplicationContextAware, InitializingBean, DisposableBean,
5960
SmartLifecycle, ApplicationListener<ContextClosedEvent> {
6061

62+
/**
63+
* The default phase for an executor {@link SmartLifecycle}: {@code Integer.MAX_VALUE / 2}.
64+
* <p>This is different from the default phase {@code Integer.MAX_VALUE} associated with
65+
* other {@link SmartLifecycle} implementations, putting the typically auto-started
66+
* executor/scheduler beans into an earlier startup phase and a later shutdown phase while
67+
* still leaving room for regular {@link Lifecycle} components with the common phase 0.
68+
* @since 6.2
69+
* @see #getPhase()
70+
* @see SmartLifecycle#DEFAULT_PHASE
71+
* @see org.springframework.context.support.DefaultLifecycleProcessor#setTimeoutPerShutdownPhase
72+
*/
73+
public static final int DEFAULT_PHASE = Integer.MAX_VALUE / 2;
74+
75+
6176
protected final Log logger = LogFactory.getLog(getClass());
6277

6378
private ThreadFactory threadFactory = this;
@@ -218,7 +233,8 @@ public void setAwaitTerminationMillis(long awaitTerminationMillis) {
218233

219234
/**
220235
* Specify the lifecycle phase for pausing and resuming this executor.
221-
* The default is {@link #DEFAULT_PHASE}.
236+
* <p>The default for executors/schedulers is {@link #DEFAULT_PHASE} as of 6.2,
237+
* for stopping after other {@link SmartLifecycle} implementations.
222238
* @since 6.1
223239
* @see SmartLifecycle#getPhase()
224240
*/

Diff for: spring-context/src/main/java/org/springframework/scheduling/concurrent/SimpleAsyncTaskScheduler.java

+9-1
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2002-2023 the original author or authors.
2+
* Copyright 2002-2024 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.
@@ -91,6 +91,14 @@
9191
public class SimpleAsyncTaskScheduler extends SimpleAsyncTaskExecutor implements TaskScheduler,
9292
ApplicationContextAware, SmartLifecycle, ApplicationListener<ContextClosedEvent> {
9393

94+
/**
95+
* The default phase for an executor {@link SmartLifecycle}: {@code Integer.MAX_VALUE / 2}.
96+
* @since 6.2
97+
* @see #getPhase()
98+
* @see ExecutorConfigurationSupport#DEFAULT_PHASE
99+
*/
100+
public static final int DEFAULT_PHASE = ExecutorConfigurationSupport.DEFAULT_PHASE;
101+
94102
private static final TimeUnit NANO = TimeUnit.NANOSECONDS;
95103

96104

0 commit comments

Comments
 (0)