From abd3e6a97825e4212b7ebbc31f3e0d8069140566 Mon Sep 17 00:00:00 2001 From: Tadaya Tsuyukubo Date: Sun, 9 Feb 2020 23:20:43 -0800 Subject: [PATCH] Take Duration to await executor termination Add "ExecutorConfigurationSupport#setAwaitTerminationDuration" which takes "Duration" to await internal executor termination. --- .../ExecutorConfigurationSupport.java | 23 ++++++++++++++----- 1 file changed, 17 insertions(+), 6 deletions(-) diff --git a/spring-context/src/main/java/org/springframework/scheduling/concurrent/ExecutorConfigurationSupport.java b/spring-context/src/main/java/org/springframework/scheduling/concurrent/ExecutorConfigurationSupport.java index 85351467846f..ae1a054c4f2e 100644 --- a/spring-context/src/main/java/org/springframework/scheduling/concurrent/ExecutorConfigurationSupport.java +++ b/spring-context/src/main/java/org/springframework/scheduling/concurrent/ExecutorConfigurationSupport.java @@ -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. @@ -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; @@ -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; @@ -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); + } + + /** + * + * @param awaitTerminationDuration time to await to shutdown internal executor + * @since 5.2.4 + */ + public void setAwaitTerminationDuration(Duration awaitTerminationDuration) { + this.awaitTerminationDuration = awaitTerminationDuration; } @Override @@ -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");