|
|
|
|
@ -1,5 +1,5 @@
@@ -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"); |
|
|
|
|
* you may not use this file except in compliance with the License. |
|
|
|
|
@ -16,6 +16,7 @@
@@ -16,6 +16,7 @@
|
|
|
|
|
|
|
|
|
|
package org.springframework.boot.task; |
|
|
|
|
|
|
|
|
|
import java.time.Duration; |
|
|
|
|
import java.util.Arrays; |
|
|
|
|
import java.util.Collections; |
|
|
|
|
import java.util.LinkedHashSet; |
|
|
|
|
@ -42,19 +43,28 @@ public class TaskSchedulerBuilder {
@@ -42,19 +43,28 @@ public class TaskSchedulerBuilder {
|
|
|
|
|
|
|
|
|
|
private final Integer poolSize; |
|
|
|
|
|
|
|
|
|
private final Boolean awaitTermination; |
|
|
|
|
|
|
|
|
|
private final Duration awaitTerminationPeriod; |
|
|
|
|
|
|
|
|
|
private final String threadNamePrefix; |
|
|
|
|
|
|
|
|
|
private final Set<TaskSchedulerCustomizer> customizers; |
|
|
|
|
|
|
|
|
|
public TaskSchedulerBuilder() { |
|
|
|
|
this.poolSize = null; |
|
|
|
|
this.awaitTermination = null; |
|
|
|
|
this.awaitTerminationPeriod = null; |
|
|
|
|
this.threadNamePrefix = null; |
|
|
|
|
this.customizers = null; |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
public TaskSchedulerBuilder(Integer poolSize, String threadNamePrefix, |
|
|
|
|
public TaskSchedulerBuilder(Integer poolSize, Boolean awaitTermination, |
|
|
|
|
Duration awaitTerminationPeriod, String threadNamePrefix, |
|
|
|
|
Set<TaskSchedulerCustomizer> taskSchedulerCustomizers) { |
|
|
|
|
this.poolSize = poolSize; |
|
|
|
|
this.awaitTermination = awaitTermination; |
|
|
|
|
this.awaitTerminationPeriod = awaitTerminationPeriod; |
|
|
|
|
this.threadNamePrefix = threadNamePrefix; |
|
|
|
|
this.customizers = taskSchedulerCustomizers; |
|
|
|
|
} |
|
|
|
|
@ -65,8 +75,35 @@ public class TaskSchedulerBuilder {
@@ -65,8 +75,35 @@ public class TaskSchedulerBuilder {
|
|
|
|
|
* @return a new builder instance |
|
|
|
|
*/ |
|
|
|
|
public TaskSchedulerBuilder poolSize(int poolSize) { |
|
|
|
|
return new TaskSchedulerBuilder(poolSize, this.threadNamePrefix, |
|
|
|
|
this.customizers); |
|
|
|
|
return new TaskSchedulerBuilder(poolSize, this.awaitTermination, |
|
|
|
|
this.awaitTerminationPeriod, this.threadNamePrefix, 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 TaskSchedulerBuilder awaitTermination(boolean awaitTermination) { |
|
|
|
|
return new TaskSchedulerBuilder(this.poolSize, awaitTermination, |
|
|
|
|
this.awaitTerminationPeriod, this.threadNamePrefix, 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 TaskSchedulerBuilder awaitTerminationPeriod(Duration awaitTerminationPeriod) { |
|
|
|
|
return new TaskSchedulerBuilder(this.poolSize, this.awaitTermination, |
|
|
|
|
awaitTerminationPeriod, this.threadNamePrefix, this.customizers); |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
/** |
|
|
|
|
@ -75,8 +112,8 @@ public class TaskSchedulerBuilder {
@@ -75,8 +112,8 @@ public class TaskSchedulerBuilder {
|
|
|
|
|
* @return a new builder instance |
|
|
|
|
*/ |
|
|
|
|
public TaskSchedulerBuilder threadNamePrefix(String threadNamePrefix) { |
|
|
|
|
return new TaskSchedulerBuilder(this.poolSize, threadNamePrefix, |
|
|
|
|
this.customizers); |
|
|
|
|
return new TaskSchedulerBuilder(this.poolSize, this.awaitTermination, |
|
|
|
|
this.awaitTerminationPeriod, threadNamePrefix, this.customizers); |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
/** |
|
|
|
|
@ -105,7 +142,8 @@ public class TaskSchedulerBuilder {
@@ -105,7 +142,8 @@ public class TaskSchedulerBuilder {
|
|
|
|
|
public TaskSchedulerBuilder customizers( |
|
|
|
|
Iterable<TaskSchedulerCustomizer> customizers) { |
|
|
|
|
Assert.notNull(customizers, "Customizers must not be null"); |
|
|
|
|
return new TaskSchedulerBuilder(this.poolSize, this.threadNamePrefix, |
|
|
|
|
return new TaskSchedulerBuilder(this.poolSize, this.awaitTermination, |
|
|
|
|
this.awaitTerminationPeriod, this.threadNamePrefix, |
|
|
|
|
append(null, customizers)); |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
@ -134,7 +172,8 @@ public class TaskSchedulerBuilder {
@@ -134,7 +172,8 @@ public class TaskSchedulerBuilder {
|
|
|
|
|
public TaskSchedulerBuilder additionalCustomizers( |
|
|
|
|
Iterable<TaskSchedulerCustomizer> customizers) { |
|
|
|
|
Assert.notNull(customizers, "Customizers must not be null"); |
|
|
|
|
return new TaskSchedulerBuilder(this.poolSize, this.threadNamePrefix, |
|
|
|
|
return new TaskSchedulerBuilder(this.poolSize, this.awaitTermination, |
|
|
|
|
this.awaitTerminationPeriod, this.threadNamePrefix, |
|
|
|
|
append(this.customizers, customizers)); |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
@ -158,6 +197,10 @@ public class TaskSchedulerBuilder {
@@ -158,6 +197,10 @@ public class TaskSchedulerBuilder {
|
|
|
|
|
public <T extends ThreadPoolTaskScheduler> T configure(T taskScheduler) { |
|
|
|
|
PropertyMapper map = PropertyMapper.get().alwaysApplyingWhenNonNull(); |
|
|
|
|
map.from(this.poolSize).to(taskScheduler::setPoolSize); |
|
|
|
|
map.from(this.awaitTermination) |
|
|
|
|
.to(taskScheduler::setWaitForTasksToCompleteOnShutdown); |
|
|
|
|
map.from(this.awaitTerminationPeriod).asInt(Duration::getSeconds) |
|
|
|
|
.to(taskScheduler::setAwaitTerminationSeconds); |
|
|
|
|
map.from(this.threadNamePrefix).to(taskScheduler::setThreadNamePrefix); |
|
|
|
|
if (!CollectionUtils.isEmpty(this.customizers)) { |
|
|
|
|
this.customizers.forEach((customizer) -> customizer.customize(taskScheduler)); |
|
|
|
|
|