Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

ExecutorConfigurationSupport to allow millisecond precision for await termination period #24496

Closed
wants to merge 1 commit into from
Closed
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright 2002-2018 the original author or authors.
* Copyright 2002-2020 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 All @@ -16,6 +16,7 @@

package org.springframework.scheduling.concurrent;

import java.time.Duration;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Future;
import java.util.concurrent.RejectedExecutionHandler;
Expand Down Expand Up @@ -59,7 +60,7 @@ public abstract class ExecutorConfigurationSupport extends CustomizableThreadFac

private boolean waitForTasksToCompleteOnShutdown = false;

private int awaitTerminationSeconds = 0;
private Duration awaitTerminationDuration = Duration.ZERO;

@Nullable
private String beanName;
Expand Down Expand Up @@ -145,7 +146,16 @@ public void setWaitForTasksToCompleteOnShutdown(boolean waitForJobsToCompleteOnS
* @see java.util.concurrent.ExecutorService#awaitTermination
*/
public void setAwaitTerminationSeconds(int awaitTerminationSeconds) {
this.awaitTerminationSeconds = awaitTerminationSeconds;
this.awaitTerminationDuration = Duration.ofSeconds(awaitTerminationSeconds);
}

/**
*
rstoyanchev marked this conversation as resolved.
Show resolved Hide resolved
* @param awaitTerminationDuration time to await to shutdown internal executor
* @since 5.2.4
*/
public void setAwaitTerminationDuration(Duration awaitTerminationDuration) {
this.awaitTerminationDuration = awaitTerminationDuration;
rstoyanchev marked this conversation as resolved.
Show resolved Hide resolved
rstoyanchev marked this conversation as resolved.
Show resolved Hide resolved
}

@Override
Expand Down Expand Up @@ -236,12 +246,13 @@ protected void cancelRemainingTask(Runnable task) {

/**
* Wait for the executor to terminate, according to the value of the
* {@link #setAwaitTerminationSeconds "awaitTerminationSeconds"} property.
* {@link #setAwaitTerminationDuration "awaitTerminationDuration"} property.
*/
private void awaitTerminationIfNecessary(ExecutorService executor) {
if (this.awaitTerminationSeconds > 0) {
boolean awaitTermination = !this.awaitTerminationDuration.isNegative() && !this.awaitTerminationDuration.isZero();
if (awaitTermination) {
try {
if (!executor.awaitTermination(this.awaitTerminationSeconds, TimeUnit.SECONDS)) {
if (!executor.awaitTermination(this.awaitTerminationDuration.toMillis(), TimeUnit.MILLISECONDS)) {
if (logger.isWarnEnabled()) {
logger.warn("Timed out while waiting for executor" +
(this.beanName != null ? " '" + this.beanName + "'" : "") + " to terminate");
Expand Down