Polish "Add support for task executor shutdown related properties"
Closes gh-15951
This commit is contained in:
@@ -22,6 +22,7 @@ import org.springframework.beans.factory.ObjectProvider;
|
|||||||
import org.springframework.boot.autoconfigure.EnableAutoConfiguration;
|
import org.springframework.boot.autoconfigure.EnableAutoConfiguration;
|
||||||
import org.springframework.boot.autoconfigure.condition.ConditionalOnClass;
|
import org.springframework.boot.autoconfigure.condition.ConditionalOnClass;
|
||||||
import org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean;
|
import org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean;
|
||||||
|
import org.springframework.boot.autoconfigure.task.TaskExecutionProperties.Shutdown;
|
||||||
import org.springframework.boot.context.properties.EnableConfigurationProperties;
|
import org.springframework.boot.context.properties.EnableConfigurationProperties;
|
||||||
import org.springframework.boot.task.TaskExecutorBuilder;
|
import org.springframework.boot.task.TaskExecutorBuilder;
|
||||||
import org.springframework.boot.task.TaskExecutorCustomizer;
|
import org.springframework.boot.task.TaskExecutorCustomizer;
|
||||||
@@ -74,10 +75,10 @@ public class TaskExecutionAutoConfiguration {
|
|||||||
builder = builder.maxPoolSize(pool.getMaxSize());
|
builder = builder.maxPoolSize(pool.getMaxSize());
|
||||||
builder = builder.allowCoreThreadTimeOut(pool.isAllowCoreThreadTimeout());
|
builder = builder.allowCoreThreadTimeOut(pool.isAllowCoreThreadTimeout());
|
||||||
builder = builder.keepAlive(pool.getKeepAlive());
|
builder = builder.keepAlive(pool.getKeepAlive());
|
||||||
|
Shutdown shutdown = this.properties.getShutdown();
|
||||||
|
builder = builder.awaitTermination(shutdown.isAwaitTermination());
|
||||||
|
builder = builder.awaitTerminationPeriod(shutdown.getAwaitTerminationPeriod());
|
||||||
builder = builder.threadNamePrefix(this.properties.getThreadNamePrefix());
|
builder = builder.threadNamePrefix(this.properties.getThreadNamePrefix());
|
||||||
builder = builder.awaitTermination(this.properties.getAwaitTermination());
|
|
||||||
builder = builder.waitForTasksToCompleteOnShutdown(
|
|
||||||
this.properties.isWaitForTasksToCompleteOnShutdown());
|
|
||||||
builder = builder.customizers(this.taskExecutorCustomizers);
|
builder = builder.customizers(this.taskExecutorCustomizers);
|
||||||
builder = builder.taskDecorator(this.taskDecorator.getIfUnique());
|
builder = builder.taskDecorator(this.taskDecorator.getIfUnique());
|
||||||
return builder;
|
return builder;
|
||||||
|
|||||||
@@ -1,5 +1,5 @@
|
|||||||
/*
|
/*
|
||||||
* Copyright 2012-2018 the original author or authors.
|
* Copyright 2012-2019 the original author or authors.
|
||||||
*
|
*
|
||||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||||
* you may not use this file except in compliance with the License.
|
* you may not use this file except in compliance with the License.
|
||||||
@@ -17,10 +17,8 @@
|
|||||||
package org.springframework.boot.autoconfigure.task;
|
package org.springframework.boot.autoconfigure.task;
|
||||||
|
|
||||||
import java.time.Duration;
|
import java.time.Duration;
|
||||||
import java.time.temporal.ChronoUnit;
|
|
||||||
|
|
||||||
import org.springframework.boot.context.properties.ConfigurationProperties;
|
import org.springframework.boot.context.properties.ConfigurationProperties;
|
||||||
import org.springframework.boot.convert.DurationUnit;
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Configuration properties for task execution.
|
* Configuration properties for task execution.
|
||||||
@@ -34,29 +32,21 @@ public class TaskExecutionProperties {
|
|||||||
|
|
||||||
private final Pool pool = new Pool();
|
private final Pool pool = new Pool();
|
||||||
|
|
||||||
|
private final Shutdown shutdown = new Shutdown();
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Prefix to use for the names of newly created threads.
|
* Prefix to use for the names of newly created threads.
|
||||||
*/
|
*/
|
||||||
private String threadNamePrefix = "task-";
|
private String threadNamePrefix = "task-";
|
||||||
|
|
||||||
/**
|
|
||||||
* Maximum number of time that the executor is supposed to block on shutdown waiting
|
|
||||||
* for remaining tasks to complete. This is particularly useful if your remaining
|
|
||||||
* tasks are likely to need access to other resources that are also managed by the
|
|
||||||
* container. If a duration suffix is not specified, seconds will be used.
|
|
||||||
*/
|
|
||||||
@DurationUnit(ChronoUnit.SECONDS)
|
|
||||||
private Duration awaitTermination;
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Whether the executor should wait for scheduled tasks to complete on shutdown.
|
|
||||||
*/
|
|
||||||
private boolean waitForTasksToCompleteOnShutdown = false;
|
|
||||||
|
|
||||||
public Pool getPool() {
|
public Pool getPool() {
|
||||||
return this.pool;
|
return this.pool;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public Shutdown getShutdown() {
|
||||||
|
return this.shutdown;
|
||||||
|
}
|
||||||
|
|
||||||
public String getThreadNamePrefix() {
|
public String getThreadNamePrefix() {
|
||||||
return this.threadNamePrefix;
|
return this.threadNamePrefix;
|
||||||
}
|
}
|
||||||
@@ -65,23 +55,6 @@ public class TaskExecutionProperties {
|
|||||||
this.threadNamePrefix = threadNamePrefix;
|
this.threadNamePrefix = threadNamePrefix;
|
||||||
}
|
}
|
||||||
|
|
||||||
public Duration getAwaitTermination() {
|
|
||||||
return this.awaitTermination;
|
|
||||||
}
|
|
||||||
|
|
||||||
public void setAwaitTermination(Duration awaitTermination) {
|
|
||||||
this.awaitTermination = awaitTermination;
|
|
||||||
}
|
|
||||||
|
|
||||||
public boolean isWaitForTasksToCompleteOnShutdown() {
|
|
||||||
return this.waitForTasksToCompleteOnShutdown;
|
|
||||||
}
|
|
||||||
|
|
||||||
public void setWaitForTasksToCompleteOnShutdown(
|
|
||||||
boolean waitForTasksToCompleteOnShutdown) {
|
|
||||||
this.waitForTasksToCompleteOnShutdown = waitForTasksToCompleteOnShutdown;
|
|
||||||
}
|
|
||||||
|
|
||||||
public static class Pool {
|
public static class Pool {
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@@ -155,4 +128,34 @@ public class TaskExecutionProperties {
|
|||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public static class Shutdown {
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Whether the executor should wait for scheduled tasks to complete on shutdown.
|
||||||
|
*/
|
||||||
|
private boolean awaitTermination;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Maximum time the executor should wait for remaining tasks to complete.
|
||||||
|
*/
|
||||||
|
private Duration awaitTerminationPeriod;
|
||||||
|
|
||||||
|
public boolean isAwaitTermination() {
|
||||||
|
return this.awaitTermination;
|
||||||
|
}
|
||||||
|
|
||||||
|
public void setAwaitTermination(boolean awaitTermination) {
|
||||||
|
this.awaitTermination = awaitTermination;
|
||||||
|
}
|
||||||
|
|
||||||
|
public Duration getAwaitTerminationPeriod() {
|
||||||
|
return this.awaitTerminationPeriod;
|
||||||
|
}
|
||||||
|
|
||||||
|
public void setAwaitTerminationPeriod(Duration awaitTerminationPeriod) {
|
||||||
|
this.awaitTerminationPeriod = awaitTerminationPeriod;
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -63,15 +63,15 @@ public class TaskExecutionAutoConfigurationTests {
|
|||||||
|
|
||||||
@Test
|
@Test
|
||||||
public void taskExecutorBuilderShouldApplyCustomSettings() {
|
public void taskExecutorBuilderShouldApplyCustomSettings() {
|
||||||
this.contextRunner.withPropertyValues(
|
this.contextRunner
|
||||||
"spring.task.execution.pool.queue-capacity=10",
|
.withPropertyValues("spring.task.execution.pool.queue-capacity=10",
|
||||||
"spring.task.execution.pool.core-size=2",
|
"spring.task.execution.pool.core-size=2",
|
||||||
"spring.task.execution.pool.max-size=4",
|
"spring.task.execution.pool.max-size=4",
|
||||||
"spring.task.execution.pool.allow-core-thread-timeout=true",
|
"spring.task.execution.pool.allow-core-thread-timeout=true",
|
||||||
"spring.task.execution.pool.keep-alive=5s",
|
"spring.task.execution.pool.keep-alive=5s",
|
||||||
"spring.task.execution.thread-name-prefix=mytest-",
|
"spring.task.execution.shutdown.await-termination=true",
|
||||||
"spring.task.execution.await-termination=30s",
|
"spring.task.execution.shutdown.await-termination-period=30s",
|
||||||
"spring.task.execution.wait-for-tasks-to-complete-on-shutdown=true")
|
"spring.task.execution.thread-name-prefix=mytest-")
|
||||||
.run(assertTaskExecutor((taskExecutor) -> {
|
.run(assertTaskExecutor((taskExecutor) -> {
|
||||||
assertThat(taskExecutor).hasFieldOrPropertyWithValue("queueCapacity",
|
assertThat(taskExecutor).hasFieldOrPropertyWithValue("queueCapacity",
|
||||||
10);
|
10);
|
||||||
@@ -80,11 +80,11 @@ public class TaskExecutionAutoConfigurationTests {
|
|||||||
assertThat(taskExecutor)
|
assertThat(taskExecutor)
|
||||||
.hasFieldOrPropertyWithValue("allowCoreThreadTimeOut", true);
|
.hasFieldOrPropertyWithValue("allowCoreThreadTimeOut", true);
|
||||||
assertThat(taskExecutor.getKeepAliveSeconds()).isEqualTo(5);
|
assertThat(taskExecutor.getKeepAliveSeconds()).isEqualTo(5);
|
||||||
assertThat(taskExecutor.getThreadNamePrefix()).isEqualTo("mytest-");
|
|
||||||
assertThat(taskExecutor)
|
|
||||||
.hasFieldOrPropertyWithValue("awaitTerminationSeconds", 30);
|
|
||||||
assertThat(taskExecutor).hasFieldOrPropertyWithValue(
|
assertThat(taskExecutor).hasFieldOrPropertyWithValue(
|
||||||
"waitForTasksToCompleteOnShutdown", true);
|
"waitForTasksToCompleteOnShutdown", true);
|
||||||
|
assertThat(taskExecutor)
|
||||||
|
.hasFieldOrPropertyWithValue("awaitTerminationSeconds", 30);
|
||||||
|
assertThat(taskExecutor.getThreadNamePrefix()).isEqualTo("mytest-");
|
||||||
}));
|
}));
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -1,5 +1,5 @@
|
|||||||
/*
|
/*
|
||||||
* Copyright 2012-2018 the original author or authors.
|
* Copyright 2012-2019 the original author or authors.
|
||||||
*
|
*
|
||||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||||
* you may not use this file except in compliance with the License.
|
* you may not use this file except in compliance with the License.
|
||||||
@@ -55,12 +55,12 @@ public class TaskExecutorBuilder {
|
|||||||
|
|
||||||
private final Duration keepAlive;
|
private final Duration keepAlive;
|
||||||
|
|
||||||
|
private final Boolean awaitTermination;
|
||||||
|
|
||||||
|
private final Duration awaitTerminationPeriod;
|
||||||
|
|
||||||
private final String threadNamePrefix;
|
private final String threadNamePrefix;
|
||||||
|
|
||||||
private final Duration awaitTermination;
|
|
||||||
|
|
||||||
private final Boolean waitForTasksToCompleteOnShutdown;
|
|
||||||
|
|
||||||
private final TaskDecorator taskDecorator;
|
private final TaskDecorator taskDecorator;
|
||||||
|
|
||||||
private final Set<TaskExecutorCustomizer> customizers;
|
private final Set<TaskExecutorCustomizer> customizers;
|
||||||
@@ -71,26 +71,26 @@ public class TaskExecutorBuilder {
|
|||||||
this.maxPoolSize = null;
|
this.maxPoolSize = null;
|
||||||
this.allowCoreThreadTimeOut = null;
|
this.allowCoreThreadTimeOut = null;
|
||||||
this.keepAlive = null;
|
this.keepAlive = null;
|
||||||
this.threadNamePrefix = null;
|
|
||||||
this.awaitTermination = null;
|
this.awaitTermination = null;
|
||||||
this.waitForTasksToCompleteOnShutdown = null;
|
this.awaitTerminationPeriod = null;
|
||||||
|
this.threadNamePrefix = null;
|
||||||
this.taskDecorator = null;
|
this.taskDecorator = null;
|
||||||
this.customizers = null;
|
this.customizers = null;
|
||||||
}
|
}
|
||||||
|
|
||||||
private TaskExecutorBuilder(Integer queueCapacity, Integer corePoolSize,
|
private TaskExecutorBuilder(Integer queueCapacity, Integer corePoolSize,
|
||||||
Integer maxPoolSize, Boolean allowCoreThreadTimeOut, Duration keepAlive,
|
Integer maxPoolSize, Boolean allowCoreThreadTimeOut, Duration keepAlive,
|
||||||
String threadNamePrefix, Duration awaitTermination,
|
Boolean awaitTermination, Duration awaitTerminationPeriod,
|
||||||
Boolean waitForTasksToCompleteOnShutdown, TaskDecorator taskDecorator,
|
String threadNamePrefix, TaskDecorator taskDecorator,
|
||||||
Set<TaskExecutorCustomizer> customizers) {
|
Set<TaskExecutorCustomizer> customizers) {
|
||||||
this.queueCapacity = queueCapacity;
|
this.queueCapacity = queueCapacity;
|
||||||
this.corePoolSize = corePoolSize;
|
this.corePoolSize = corePoolSize;
|
||||||
this.maxPoolSize = maxPoolSize;
|
this.maxPoolSize = maxPoolSize;
|
||||||
this.allowCoreThreadTimeOut = allowCoreThreadTimeOut;
|
this.allowCoreThreadTimeOut = allowCoreThreadTimeOut;
|
||||||
this.keepAlive = keepAlive;
|
this.keepAlive = keepAlive;
|
||||||
this.threadNamePrefix = threadNamePrefix;
|
|
||||||
this.awaitTermination = awaitTermination;
|
this.awaitTermination = awaitTermination;
|
||||||
this.waitForTasksToCompleteOnShutdown = waitForTasksToCompleteOnShutdown;
|
this.awaitTerminationPeriod = awaitTerminationPeriod;
|
||||||
|
this.threadNamePrefix = threadNamePrefix;
|
||||||
this.taskDecorator = taskDecorator;
|
this.taskDecorator = taskDecorator;
|
||||||
this.customizers = customizers;
|
this.customizers = customizers;
|
||||||
}
|
}
|
||||||
@@ -103,9 +103,9 @@ public class TaskExecutorBuilder {
|
|||||||
*/
|
*/
|
||||||
public TaskExecutorBuilder queueCapacity(int queueCapacity) {
|
public TaskExecutorBuilder queueCapacity(int queueCapacity) {
|
||||||
return new TaskExecutorBuilder(queueCapacity, this.corePoolSize, this.maxPoolSize,
|
return new TaskExecutorBuilder(queueCapacity, this.corePoolSize, this.maxPoolSize,
|
||||||
this.allowCoreThreadTimeOut, this.keepAlive, this.threadNamePrefix,
|
this.allowCoreThreadTimeOut, this.keepAlive, this.awaitTermination,
|
||||||
this.awaitTermination, this.waitForTasksToCompleteOnShutdown,
|
this.awaitTerminationPeriod, this.threadNamePrefix, this.taskDecorator,
|
||||||
this.taskDecorator, this.customizers);
|
this.customizers);
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@@ -119,9 +119,9 @@ public class TaskExecutorBuilder {
|
|||||||
*/
|
*/
|
||||||
public TaskExecutorBuilder corePoolSize(int corePoolSize) {
|
public TaskExecutorBuilder corePoolSize(int corePoolSize) {
|
||||||
return new TaskExecutorBuilder(this.queueCapacity, corePoolSize, this.maxPoolSize,
|
return new TaskExecutorBuilder(this.queueCapacity, corePoolSize, this.maxPoolSize,
|
||||||
this.allowCoreThreadTimeOut, this.keepAlive, this.threadNamePrefix,
|
this.allowCoreThreadTimeOut, this.keepAlive, this.awaitTermination,
|
||||||
this.awaitTermination, this.waitForTasksToCompleteOnShutdown,
|
this.awaitTerminationPeriod, this.threadNamePrefix, this.taskDecorator,
|
||||||
this.taskDecorator, this.customizers);
|
this.customizers);
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@@ -135,9 +135,9 @@ public class TaskExecutorBuilder {
|
|||||||
*/
|
*/
|
||||||
public TaskExecutorBuilder maxPoolSize(int maxPoolSize) {
|
public TaskExecutorBuilder maxPoolSize(int maxPoolSize) {
|
||||||
return new TaskExecutorBuilder(this.queueCapacity, this.corePoolSize, maxPoolSize,
|
return new TaskExecutorBuilder(this.queueCapacity, this.corePoolSize, maxPoolSize,
|
||||||
this.allowCoreThreadTimeOut, this.keepAlive, this.threadNamePrefix,
|
this.allowCoreThreadTimeOut, this.keepAlive, this.awaitTermination,
|
||||||
this.awaitTermination, this.waitForTasksToCompleteOnShutdown,
|
this.awaitTerminationPeriod, this.threadNamePrefix, this.taskDecorator,
|
||||||
this.taskDecorator, this.customizers);
|
this.customizers);
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@@ -149,9 +149,8 @@ public class TaskExecutorBuilder {
|
|||||||
public TaskExecutorBuilder allowCoreThreadTimeOut(boolean allowCoreThreadTimeOut) {
|
public TaskExecutorBuilder allowCoreThreadTimeOut(boolean allowCoreThreadTimeOut) {
|
||||||
return new TaskExecutorBuilder(this.queueCapacity, this.corePoolSize,
|
return new TaskExecutorBuilder(this.queueCapacity, this.corePoolSize,
|
||||||
this.maxPoolSize, allowCoreThreadTimeOut, this.keepAlive,
|
this.maxPoolSize, allowCoreThreadTimeOut, this.keepAlive,
|
||||||
this.threadNamePrefix, this.awaitTermination,
|
this.awaitTermination, this.awaitTerminationPeriod, this.threadNamePrefix,
|
||||||
this.waitForTasksToCompleteOnShutdown, this.taskDecorator,
|
this.taskDecorator, this.customizers);
|
||||||
this.customizers);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@@ -162,9 +161,39 @@ public class TaskExecutorBuilder {
|
|||||||
public TaskExecutorBuilder keepAlive(Duration keepAlive) {
|
public TaskExecutorBuilder keepAlive(Duration keepAlive) {
|
||||||
return new TaskExecutorBuilder(this.queueCapacity, this.corePoolSize,
|
return new TaskExecutorBuilder(this.queueCapacity, this.corePoolSize,
|
||||||
this.maxPoolSize, this.allowCoreThreadTimeOut, keepAlive,
|
this.maxPoolSize, this.allowCoreThreadTimeOut, keepAlive,
|
||||||
this.threadNamePrefix, this.awaitTermination,
|
this.awaitTermination, this.awaitTerminationPeriod, this.threadNamePrefix,
|
||||||
this.waitForTasksToCompleteOnShutdown, this.taskDecorator,
|
this.taskDecorator, this.customizers);
|
||||||
this.customizers);
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Set whether the executor should wait for scheduled tasks to complete on shutdown,
|
||||||
|
* not interrupting running tasks and executing all tasks in the queue.
|
||||||
|
* @param awaitTermination whether the executor needs to wait for the tasks to
|
||||||
|
* complete on shutdown
|
||||||
|
* @return a new builder instance
|
||||||
|
* @see #awaitTerminationPeriod(Duration)
|
||||||
|
*/
|
||||||
|
public TaskExecutorBuilder awaitTermination(boolean awaitTermination) {
|
||||||
|
return new TaskExecutorBuilder(this.queueCapacity, this.corePoolSize,
|
||||||
|
this.maxPoolSize, this.allowCoreThreadTimeOut, this.keepAlive,
|
||||||
|
awaitTermination, this.awaitTerminationPeriod, this.threadNamePrefix,
|
||||||
|
this.taskDecorator, this.customizers);
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Set the maximum time the executor is supposed to block on shutdown. When set, the
|
||||||
|
* executor blocks on shutdown in order to wait for remaining tasks to complete their
|
||||||
|
* execution before the rest of the container continues to shut down. This is
|
||||||
|
* particularly useful if your remaining tasks are likely to need access to other
|
||||||
|
* resources that are also managed by the container.
|
||||||
|
* @param awaitTerminationPeriod the await termination period to set
|
||||||
|
* @return a new builder instance
|
||||||
|
*/
|
||||||
|
public TaskExecutorBuilder awaitTerminationPeriod(Duration awaitTerminationPeriod) {
|
||||||
|
return new TaskExecutorBuilder(this.queueCapacity, this.corePoolSize,
|
||||||
|
this.maxPoolSize, this.allowCoreThreadTimeOut, this.keepAlive,
|
||||||
|
this.awaitTermination, awaitTerminationPeriod, this.threadNamePrefix,
|
||||||
|
this.taskDecorator, this.customizers);
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@@ -175,41 +204,8 @@ public class TaskExecutorBuilder {
|
|||||||
public TaskExecutorBuilder threadNamePrefix(String threadNamePrefix) {
|
public TaskExecutorBuilder threadNamePrefix(String threadNamePrefix) {
|
||||||
return new TaskExecutorBuilder(this.queueCapacity, this.corePoolSize,
|
return new TaskExecutorBuilder(this.queueCapacity, this.corePoolSize,
|
||||||
this.maxPoolSize, this.allowCoreThreadTimeOut, this.keepAlive,
|
this.maxPoolSize, this.allowCoreThreadTimeOut, this.keepAlive,
|
||||||
threadNamePrefix, this.awaitTermination,
|
this.awaitTermination, this.awaitTerminationPeriod, threadNamePrefix,
|
||||||
this.waitForTasksToCompleteOnShutdown, this.taskDecorator,
|
this.taskDecorator, this.customizers);
|
||||||
this.customizers);
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Set the maximum number of time that the executor is supposed to block on shutdown
|
|
||||||
* in order to wait for remaining tasks to complete their execution before the rest of
|
|
||||||
* the container continues to shut down. This is particularly useful if your remaining
|
|
||||||
* tasks are likely to need access to other resources that are also managed by the
|
|
||||||
* container.
|
|
||||||
* @param awaitTermination the await termination to set
|
|
||||||
* @return a new builder instance
|
|
||||||
*/
|
|
||||||
public TaskExecutorBuilder awaitTermination(Duration awaitTermination) {
|
|
||||||
return new TaskExecutorBuilder(this.queueCapacity, this.corePoolSize,
|
|
||||||
this.maxPoolSize, this.allowCoreThreadTimeOut, this.keepAlive,
|
|
||||||
this.threadNamePrefix, awaitTermination,
|
|
||||||
this.waitForTasksToCompleteOnShutdown, this.taskDecorator,
|
|
||||||
this.customizers);
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Set whether the executor should wait for scheduled tasks to complete on shutdown,
|
|
||||||
* not interrupting running tasks and executing all tasks in the queue.
|
|
||||||
* @param waitForTasksToCompleteOnShutdown if executor needs to wait for the tasks to
|
|
||||||
* complete on shutdown
|
|
||||||
* @return a new builder instance
|
|
||||||
*/
|
|
||||||
public TaskExecutorBuilder waitForTasksToCompleteOnShutdown(
|
|
||||||
boolean waitForTasksToCompleteOnShutdown) {
|
|
||||||
return new TaskExecutorBuilder(this.queueCapacity, this.corePoolSize,
|
|
||||||
this.maxPoolSize, this.allowCoreThreadTimeOut, this.keepAlive,
|
|
||||||
this.threadNamePrefix, this.awaitTermination,
|
|
||||||
waitForTasksToCompleteOnShutdown, this.taskDecorator, this.customizers);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@@ -220,8 +216,8 @@ public class TaskExecutorBuilder {
|
|||||||
public TaskExecutorBuilder taskDecorator(TaskDecorator taskDecorator) {
|
public TaskExecutorBuilder taskDecorator(TaskDecorator taskDecorator) {
|
||||||
return new TaskExecutorBuilder(this.queueCapacity, this.corePoolSize,
|
return new TaskExecutorBuilder(this.queueCapacity, this.corePoolSize,
|
||||||
this.maxPoolSize, this.allowCoreThreadTimeOut, this.keepAlive,
|
this.maxPoolSize, this.allowCoreThreadTimeOut, this.keepAlive,
|
||||||
this.threadNamePrefix, this.awaitTermination,
|
this.awaitTermination, this.awaitTerminationPeriod, this.threadNamePrefix,
|
||||||
this.waitForTasksToCompleteOnShutdown, taskDecorator, this.customizers);
|
taskDecorator, this.customizers);
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@@ -251,9 +247,8 @@ public class TaskExecutorBuilder {
|
|||||||
Assert.notNull(customizers, "Customizers must not be null");
|
Assert.notNull(customizers, "Customizers must not be null");
|
||||||
return new TaskExecutorBuilder(this.queueCapacity, this.corePoolSize,
|
return new TaskExecutorBuilder(this.queueCapacity, this.corePoolSize,
|
||||||
this.maxPoolSize, this.allowCoreThreadTimeOut, this.keepAlive,
|
this.maxPoolSize, this.allowCoreThreadTimeOut, this.keepAlive,
|
||||||
this.threadNamePrefix, this.awaitTermination,
|
this.awaitTermination, this.awaitTerminationPeriod, this.threadNamePrefix,
|
||||||
this.waitForTasksToCompleteOnShutdown, this.taskDecorator,
|
this.taskDecorator, append(null, customizers));
|
||||||
append(null, customizers));
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@@ -283,9 +278,8 @@ public class TaskExecutorBuilder {
|
|||||||
Assert.notNull(customizers, "Customizers must not be null");
|
Assert.notNull(customizers, "Customizers must not be null");
|
||||||
return new TaskExecutorBuilder(this.queueCapacity, this.corePoolSize,
|
return new TaskExecutorBuilder(this.queueCapacity, this.corePoolSize,
|
||||||
this.maxPoolSize, this.allowCoreThreadTimeOut, this.keepAlive,
|
this.maxPoolSize, this.allowCoreThreadTimeOut, this.keepAlive,
|
||||||
this.threadNamePrefix, this.awaitTermination,
|
this.awaitTermination, this.awaitTerminationPeriod, this.threadNamePrefix,
|
||||||
this.waitForTasksToCompleteOnShutdown, this.taskDecorator,
|
this.taskDecorator, append(this.customizers, customizers));
|
||||||
append(this.customizers, customizers));
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@@ -328,12 +322,12 @@ public class TaskExecutorBuilder {
|
|||||||
map.from(this.keepAlive).asInt(Duration::getSeconds)
|
map.from(this.keepAlive).asInt(Duration::getSeconds)
|
||||||
.to(taskExecutor::setKeepAliveSeconds);
|
.to(taskExecutor::setKeepAliveSeconds);
|
||||||
map.from(this.allowCoreThreadTimeOut).to(taskExecutor::setAllowCoreThreadTimeOut);
|
map.from(this.allowCoreThreadTimeOut).to(taskExecutor::setAllowCoreThreadTimeOut);
|
||||||
|
map.from(this.awaitTermination)
|
||||||
|
.to(taskExecutor::setWaitForTasksToCompleteOnShutdown);
|
||||||
|
map.from(this.awaitTerminationPeriod).asInt(Duration::getSeconds)
|
||||||
|
.to(taskExecutor::setAwaitTerminationSeconds);
|
||||||
map.from(this.threadNamePrefix).whenHasText()
|
map.from(this.threadNamePrefix).whenHasText()
|
||||||
.to(taskExecutor::setThreadNamePrefix);
|
.to(taskExecutor::setThreadNamePrefix);
|
||||||
map.from(this.awaitTermination).asInt(Duration::getSeconds)
|
|
||||||
.to(taskExecutor::setAwaitTerminationSeconds);
|
|
||||||
map.from(this.waitForTasksToCompleteOnShutdown)
|
|
||||||
.to(taskExecutor::setWaitForTasksToCompleteOnShutdown);
|
|
||||||
map.from(this.taskDecorator).to(taskExecutor::setTaskDecorator);
|
map.from(this.taskDecorator).to(taskExecutor::setTaskDecorator);
|
||||||
if (!CollectionUtils.isEmpty(this.customizers)) {
|
if (!CollectionUtils.isEmpty(this.customizers)) {
|
||||||
this.customizers.forEach((customizer) -> customizer.customize(taskExecutor));
|
this.customizers.forEach((customizer) -> customizer.customize(taskExecutor));
|
||||||
|
|||||||
@@ -1,5 +1,5 @@
|
|||||||
/*
|
/*
|
||||||
* Copyright 2012-2018 the original author or authors.
|
* Copyright 2012-2019 the original author or authors.
|
||||||
*
|
*
|
||||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||||
* you may not use this file except in compliance with the License.
|
* you may not use this file except in compliance with the License.
|
||||||
@@ -56,24 +56,23 @@ public class TaskExecutorBuilderTests {
|
|||||||
}
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
public void threadNamePrefixShouldApply() {
|
public void awaitTerminationShouldApply() {
|
||||||
ThreadPoolTaskExecutor executor = this.builder.threadNamePrefix("test-").build();
|
ThreadPoolTaskExecutor executor = this.builder.awaitTermination(true).build();
|
||||||
assertThat(executor.getThreadNamePrefix()).isEqualTo("test-");
|
assertThat(executor)
|
||||||
|
.hasFieldOrPropertyWithValue("waitForTasksToCompleteOnShutdown", true);
|
||||||
}
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
public void awaitTerminationShouldApply() {
|
public void awaitTerminationPeriodShouldApply() {
|
||||||
ThreadPoolTaskExecutor executor = this.builder
|
ThreadPoolTaskExecutor executor = this.builder
|
||||||
.awaitTermination(Duration.ofMinutes(1)).build();
|
.awaitTerminationPeriod(Duration.ofMinutes(1)).build();
|
||||||
assertThat(executor).hasFieldOrPropertyWithValue("awaitTerminationSeconds", 60);
|
assertThat(executor).hasFieldOrPropertyWithValue("awaitTerminationSeconds", 60);
|
||||||
}
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
public void waitForTasksToCompleteOnShutdownShouldApply() {
|
public void threadNamePrefixShouldApply() {
|
||||||
ThreadPoolTaskExecutor executor = this.builder
|
ThreadPoolTaskExecutor executor = this.builder.threadNamePrefix("test-").build();
|
||||||
.waitForTasksToCompleteOnShutdown(true).build();
|
assertThat(executor.getThreadNamePrefix()).isEqualTo("test-");
|
||||||
assertThat(executor)
|
|
||||||
.hasFieldOrPropertyWithValue("waitForTasksToCompleteOnShutdown", true);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
@@ -113,17 +112,17 @@ public class TaskExecutorBuilderTests {
|
|||||||
ThreadPoolTaskExecutor executor = spy(new ThreadPoolTaskExecutor());
|
ThreadPoolTaskExecutor executor = spy(new ThreadPoolTaskExecutor());
|
||||||
this.builder.queueCapacity(10).corePoolSize(4).maxPoolSize(8)
|
this.builder.queueCapacity(10).corePoolSize(4).maxPoolSize(8)
|
||||||
.allowCoreThreadTimeOut(true).keepAlive(Duration.ofMinutes(1))
|
.allowCoreThreadTimeOut(true).keepAlive(Duration.ofMinutes(1))
|
||||||
.threadNamePrefix("test-").awaitTermination(Duration.ofSeconds(30))
|
.awaitTermination(true).awaitTerminationPeriod(Duration.ofSeconds(30))
|
||||||
.waitForTasksToCompleteOnShutdown(true).taskDecorator(taskDecorator)
|
.threadNamePrefix("test-").taskDecorator(taskDecorator)
|
||||||
.additionalCustomizers((taskExecutor) -> {
|
.additionalCustomizers((taskExecutor) -> {
|
||||||
verify(taskExecutor).setQueueCapacity(10);
|
verify(taskExecutor).setQueueCapacity(10);
|
||||||
verify(taskExecutor).setCorePoolSize(4);
|
verify(taskExecutor).setCorePoolSize(4);
|
||||||
verify(taskExecutor).setMaxPoolSize(8);
|
verify(taskExecutor).setMaxPoolSize(8);
|
||||||
verify(taskExecutor).setAllowCoreThreadTimeOut(true);
|
verify(taskExecutor).setAllowCoreThreadTimeOut(true);
|
||||||
verify(taskExecutor).setKeepAliveSeconds(60);
|
verify(taskExecutor).setKeepAliveSeconds(60);
|
||||||
verify(taskExecutor).setThreadNamePrefix("test-");
|
|
||||||
verify(taskExecutor).setAwaitTerminationSeconds(30);
|
|
||||||
verify(taskExecutor).setWaitForTasksToCompleteOnShutdown(true);
|
verify(taskExecutor).setWaitForTasksToCompleteOnShutdown(true);
|
||||||
|
verify(taskExecutor).setAwaitTerminationSeconds(30);
|
||||||
|
verify(taskExecutor).setThreadNamePrefix("test-");
|
||||||
verify(taskExecutor).setTaskDecorator(taskDecorator);
|
verify(taskExecutor).setTaskDecorator(taskDecorator);
|
||||||
});
|
});
|
||||||
this.builder.configure(executor);
|
this.builder.configure(executor);
|
||||||
|
|||||||
Reference in New Issue
Block a user