Skip to content

Commit

Permalink
Change executor phase to MAX_VALUE/2 and reduce timeout to 10 seconds
Browse files Browse the repository at this point in the history
Closes gh-32152
  • Loading branch information
jhoeller committed Feb 16, 2024
1 parent 7ee8e66 commit 6791ea9
Show file tree
Hide file tree
Showing 4 changed files with 32 additions and 5 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -72,9 +72,12 @@ public interface SmartLifecycle extends Lifecycle, Phased {
* {@link Lifecycle} implementations, putting the typically auto-started
* {@code SmartLifecycle} beans into a later startup phase and an earlier
* shutdown phase.
* <p>Note that certain {@code SmartLifecycle} components come with a different
* default phase: e.g. executors/schedulers with {@code Integer.MAX_VALUE / 2}.
* @since 5.1
* @see #getPhase()
* @see org.springframework.context.support.DefaultLifecycleProcessor#getPhase(Lifecycle)
* @see org.springframework.scheduling.concurrent.ExecutorConfigurationSupport#DEFAULT_PHASE
* @see org.springframework.context.support.DefaultLifecycleProcessor#setTimeoutPerShutdownPhase
*/
int DEFAULT_PHASE = Integer.MAX_VALUE;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -106,7 +106,7 @@ public class DefaultLifecycleProcessor implements LifecycleProcessor, BeanFactor

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

private volatile long timeoutPerShutdownPhase = 30000;
private volatile long timeoutPerShutdownPhase = 10000;

private volatile boolean running;

Expand Down Expand Up @@ -135,7 +135,7 @@ else if (checkpointOnRefresh) {
/**
* Specify the maximum time allotted in milliseconds for the shutdown of any
* phase (group of {@link SmartLifecycle} beans with the same 'phase' value).
* <p>The default value is 30000 milliseconds (30 seconds).
* <p>The default value is 10000 milliseconds (10 seconds) as of 6.2.
* @see SmartLifecycle#getPhase()
*/
public void setTimeoutPerShutdownPhase(long timeoutPerShutdownPhase) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@
import org.springframework.context.ApplicationContext;
import org.springframework.context.ApplicationContextAware;
import org.springframework.context.ApplicationListener;
import org.springframework.context.Lifecycle;
import org.springframework.context.SmartLifecycle;
import org.springframework.context.event.ContextClosedEvent;
import org.springframework.lang.Nullable;
Expand All @@ -58,6 +59,20 @@ public abstract class ExecutorConfigurationSupport extends CustomizableThreadFac
implements BeanNameAware, ApplicationContextAware, InitializingBean, DisposableBean,
SmartLifecycle, ApplicationListener<ContextClosedEvent> {

/**
* The default phase for an executor {@link SmartLifecycle}: {@code Integer.MAX_VALUE / 2}.
* <p>This is different from the default phase {@code Integer.MAX_VALUE} associated with
* other {@link SmartLifecycle} implementations, putting the typically auto-started
* executor/scheduler beans into an earlier startup phase and a later shutdown phase while
* still leaving room for regular {@link Lifecycle} components with the common phase 0.
* @since 6.2
* @see #getPhase()
* @see SmartLifecycle#DEFAULT_PHASE
* @see org.springframework.context.support.DefaultLifecycleProcessor#setTimeoutPerShutdownPhase
*/
public static final int DEFAULT_PHASE = Integer.MAX_VALUE / 2;


protected final Log logger = LogFactory.getLog(getClass());

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

/**
* Specify the lifecycle phase for pausing and resuming this executor.
* The default is {@link #DEFAULT_PHASE}.
* <p>The default for executors/schedulers is {@link #DEFAULT_PHASE} as of 6.2,
* for stopping after other {@link SmartLifecycle} implementations.
* @since 6.1
* @see SmartLifecycle#getPhase()
*/
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright 2002-2023 the original author or authors.
* Copyright 2002-2024 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
Expand Down Expand Up @@ -91,6 +91,14 @@
public class SimpleAsyncTaskScheduler extends SimpleAsyncTaskExecutor implements TaskScheduler,
ApplicationContextAware, SmartLifecycle, ApplicationListener<ContextClosedEvent> {

/**
* The default phase for an executor {@link SmartLifecycle}: {@code Integer.MAX_VALUE / 2}.
* @since 6.2
* @see #getPhase()
* @see ExecutorConfigurationSupport#DEFAULT_PHASE
*/
public static final int DEFAULT_PHASE = ExecutorConfigurationSupport.DEFAULT_PHASE;

private static final TimeUnit NANO = TimeUnit.NANOSECONDS;


Expand Down

0 comments on commit 6791ea9

Please sign in to comment.