Add support for SimpleTaskExecutor#reject-tasks-when-limit-reached
See gh-45155 Signed-off-by: Yanming Zhou <zhouyanming@gmail.com>
This commit is contained in:
committed by
Stéphane Nicoll
parent
10b15e301b
commit
597c58f43d
@@ -83,6 +83,11 @@ public class TaskExecutionProperties {
|
||||
*/
|
||||
private Integer concurrencyLimit;
|
||||
|
||||
/**
|
||||
* Specify whether to reject tasks when the concurrency limit has been reached.
|
||||
*/
|
||||
private boolean rejectTasksWhenLimitReached;
|
||||
|
||||
public Integer getConcurrencyLimit() {
|
||||
return this.concurrencyLimit;
|
||||
}
|
||||
@@ -91,6 +96,14 @@ public class TaskExecutionProperties {
|
||||
this.concurrencyLimit = concurrencyLimit;
|
||||
}
|
||||
|
||||
public boolean isRejectTasksWhenLimitReached() {
|
||||
return this.rejectTasksWhenLimitReached;
|
||||
}
|
||||
|
||||
public void setRejectTasksWhenLimitReached(boolean rejectTasksWhenLimitReached) {
|
||||
this.rejectTasksWhenLimitReached = rejectTasksWhenLimitReached;
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
public static class Pool {
|
||||
|
||||
@@ -136,6 +136,7 @@ class TaskExecutorConfigurations {
|
||||
builder = builder.taskDecorator(this.taskDecorator.getIfUnique());
|
||||
TaskExecutionProperties.Simple simple = this.properties.getSimple();
|
||||
builder = builder.concurrencyLimit(simple.getConcurrencyLimit());
|
||||
builder = builder.rejectTasksWhenLimitReached(simple.isRejectTasksWhenLimitReached());
|
||||
TaskExecutionProperties.Shutdown shutdown = this.properties.getShutdown();
|
||||
if (shutdown.isAwaitTermination()) {
|
||||
builder = builder.taskTerminationTimeout(shutdown.getAwaitTerminationPeriod());
|
||||
|
||||
@@ -84,10 +84,12 @@ class TaskExecutionAutoConfigurationTests {
|
||||
this.contextRunner
|
||||
.withPropertyValues("spring.task.execution.thread-name-prefix=mytest-",
|
||||
"spring.task.execution.simple.concurrency-limit=1",
|
||||
"spring.task.execution.simple.reject-tasks-when-limit-reached=true",
|
||||
"spring.task.execution.shutdown.await-termination=true",
|
||||
"spring.task.execution.shutdown.await-termination-period=30s")
|
||||
.run(assertSimpleAsyncTaskExecutor((taskExecutor) -> {
|
||||
assertThat(taskExecutor.getConcurrencyLimit()).isEqualTo(1);
|
||||
assertThat(taskExecutor).hasFieldOrPropertyWithValue("rejectTasksWhenLimitReached", true);
|
||||
assertThat(taskExecutor.getThreadNamePrefix()).isEqualTo("mytest-");
|
||||
assertThat(taskExecutor).hasFieldOrPropertyWithValue("taskTerminationTimeout", 30000L);
|
||||
}));
|
||||
|
||||
@@ -41,6 +41,7 @@ import org.springframework.util.CollectionUtils;
|
||||
* @author Stephane Nicoll
|
||||
* @author Filip Hrisafov
|
||||
* @author Moritz Halbritter
|
||||
* @author Yanming Zhou
|
||||
* @since 3.2.0
|
||||
*/
|
||||
public class SimpleAsyncTaskExecutorBuilder {
|
||||
@@ -51,6 +52,8 @@ public class SimpleAsyncTaskExecutorBuilder {
|
||||
|
||||
private final Integer concurrencyLimit;
|
||||
|
||||
private final boolean rejectTasksWhenLimitReached;
|
||||
|
||||
private final TaskDecorator taskDecorator;
|
||||
|
||||
private final Set<SimpleAsyncTaskExecutorCustomizer> customizers;
|
||||
@@ -58,15 +61,16 @@ public class SimpleAsyncTaskExecutorBuilder {
|
||||
private final Duration taskTerminationTimeout;
|
||||
|
||||
public SimpleAsyncTaskExecutorBuilder() {
|
||||
this(null, null, null, null, null, null);
|
||||
this(null, null, null, false, null, null, null);
|
||||
}
|
||||
|
||||
private SimpleAsyncTaskExecutorBuilder(Boolean virtualThreads, String threadNamePrefix, Integer concurrencyLimit,
|
||||
TaskDecorator taskDecorator, Set<SimpleAsyncTaskExecutorCustomizer> customizers,
|
||||
Duration taskTerminationTimeout) {
|
||||
boolean rejectTasksWhenLimitReached, TaskDecorator taskDecorator,
|
||||
Set<SimpleAsyncTaskExecutorCustomizer> customizers, Duration taskTerminationTimeout) {
|
||||
this.virtualThreads = virtualThreads;
|
||||
this.threadNamePrefix = threadNamePrefix;
|
||||
this.concurrencyLimit = concurrencyLimit;
|
||||
this.rejectTasksWhenLimitReached = rejectTasksWhenLimitReached;
|
||||
this.taskDecorator = taskDecorator;
|
||||
this.customizers = customizers;
|
||||
this.taskTerminationTimeout = taskTerminationTimeout;
|
||||
@@ -79,7 +83,7 @@ public class SimpleAsyncTaskExecutorBuilder {
|
||||
*/
|
||||
public SimpleAsyncTaskExecutorBuilder threadNamePrefix(String threadNamePrefix) {
|
||||
return new SimpleAsyncTaskExecutorBuilder(this.virtualThreads, threadNamePrefix, this.concurrencyLimit,
|
||||
this.taskDecorator, this.customizers, this.taskTerminationTimeout);
|
||||
this.rejectTasksWhenLimitReached, this.taskDecorator, this.customizers, this.taskTerminationTimeout);
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -89,7 +93,7 @@ public class SimpleAsyncTaskExecutorBuilder {
|
||||
*/
|
||||
public SimpleAsyncTaskExecutorBuilder virtualThreads(Boolean virtualThreads) {
|
||||
return new SimpleAsyncTaskExecutorBuilder(virtualThreads, this.threadNamePrefix, this.concurrencyLimit,
|
||||
this.taskDecorator, this.customizers, this.taskTerminationTimeout);
|
||||
this.rejectTasksWhenLimitReached, this.taskDecorator, this.customizers, this.taskTerminationTimeout);
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -99,7 +103,19 @@ public class SimpleAsyncTaskExecutorBuilder {
|
||||
*/
|
||||
public SimpleAsyncTaskExecutorBuilder concurrencyLimit(Integer concurrencyLimit) {
|
||||
return new SimpleAsyncTaskExecutorBuilder(this.virtualThreads, this.threadNamePrefix, concurrencyLimit,
|
||||
this.taskDecorator, this.customizers, this.taskTerminationTimeout);
|
||||
this.rejectTasksWhenLimitReached, this.taskDecorator, this.customizers, this.taskTerminationTimeout);
|
||||
}
|
||||
|
||||
/**
|
||||
* Specify whether to reject tasks when the concurrency limit has been reached.
|
||||
* @param rejectTasksWhenLimitReached whether to reject tasks when the concurrency
|
||||
* limit has been reached
|
||||
* @return a new builder instance
|
||||
* @since 3.5.0
|
||||
*/
|
||||
public SimpleAsyncTaskExecutorBuilder rejectTasksWhenLimitReached(boolean rejectTasksWhenLimitReached) {
|
||||
return new SimpleAsyncTaskExecutorBuilder(this.virtualThreads, this.threadNamePrefix, this.concurrencyLimit,
|
||||
rejectTasksWhenLimitReached, this.taskDecorator, this.customizers, this.taskTerminationTimeout);
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -109,7 +125,7 @@ public class SimpleAsyncTaskExecutorBuilder {
|
||||
*/
|
||||
public SimpleAsyncTaskExecutorBuilder taskDecorator(TaskDecorator taskDecorator) {
|
||||
return new SimpleAsyncTaskExecutorBuilder(this.virtualThreads, this.threadNamePrefix, this.concurrencyLimit,
|
||||
taskDecorator, this.customizers, this.taskTerminationTimeout);
|
||||
this.rejectTasksWhenLimitReached, taskDecorator, this.customizers, this.taskTerminationTimeout);
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -120,7 +136,7 @@ public class SimpleAsyncTaskExecutorBuilder {
|
||||
*/
|
||||
public SimpleAsyncTaskExecutorBuilder taskTerminationTimeout(Duration taskTerminationTimeout) {
|
||||
return new SimpleAsyncTaskExecutorBuilder(this.virtualThreads, this.threadNamePrefix, this.concurrencyLimit,
|
||||
this.taskDecorator, this.customizers, taskTerminationTimeout);
|
||||
this.rejectTasksWhenLimitReached, this.taskDecorator, this.customizers, taskTerminationTimeout);
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -150,7 +166,8 @@ public class SimpleAsyncTaskExecutorBuilder {
|
||||
Iterable<? extends SimpleAsyncTaskExecutorCustomizer> customizers) {
|
||||
Assert.notNull(customizers, "'customizers' must not be null");
|
||||
return new SimpleAsyncTaskExecutorBuilder(this.virtualThreads, this.threadNamePrefix, this.concurrencyLimit,
|
||||
this.taskDecorator, append(null, customizers), this.taskTerminationTimeout);
|
||||
this.rejectTasksWhenLimitReached, this.taskDecorator, append(null, customizers),
|
||||
this.taskTerminationTimeout);
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -178,7 +195,8 @@ public class SimpleAsyncTaskExecutorBuilder {
|
||||
Iterable<? extends SimpleAsyncTaskExecutorCustomizer> customizers) {
|
||||
Assert.notNull(customizers, "'customizers' must not be null");
|
||||
return new SimpleAsyncTaskExecutorBuilder(this.virtualThreads, this.threadNamePrefix, this.concurrencyLimit,
|
||||
this.taskDecorator, append(this.customizers, customizers), this.taskTerminationTimeout);
|
||||
this.rejectTasksWhenLimitReached, this.taskDecorator, append(this.customizers, customizers),
|
||||
this.taskTerminationTimeout);
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -218,6 +236,7 @@ public class SimpleAsyncTaskExecutorBuilder {
|
||||
map.from(this.virtualThreads).to(taskExecutor::setVirtualThreads);
|
||||
map.from(this.threadNamePrefix).whenHasText().to(taskExecutor::setThreadNamePrefix);
|
||||
map.from(this.concurrencyLimit).to(taskExecutor::setConcurrencyLimit);
|
||||
map.from(this.rejectTasksWhenLimitReached).to(taskExecutor::setRejectTasksWhenLimitReached);
|
||||
map.from(this.taskDecorator).to(taskExecutor::setTaskDecorator);
|
||||
map.from(this.taskTerminationTimeout).as(Duration::toMillis).to(taskExecutor::setTaskTerminationTimeout);
|
||||
if (!CollectionUtils.isEmpty(this.customizers)) {
|
||||
|
||||
@@ -40,6 +40,7 @@ import static org.mockito.Mockito.spy;
|
||||
* @author Stephane Nicoll
|
||||
* @author Filip Hrisafov
|
||||
* @author Moritz Halbritter
|
||||
* @author Yanming Zhou
|
||||
*/
|
||||
class SimpleAsyncTaskExecutorBuilderTests {
|
||||
|
||||
@@ -64,6 +65,12 @@ class SimpleAsyncTaskExecutorBuilderTests {
|
||||
assertThat(executor.getConcurrencyLimit()).isEqualTo(1);
|
||||
}
|
||||
|
||||
@Test
|
||||
void rejectTasksWhenLimitReachedShouldApply() {
|
||||
SimpleAsyncTaskExecutor executor = this.builder.rejectTasksWhenLimitReached(true).build();
|
||||
assertThat(executor).extracting("rejectTasksWhenLimitReached").isEqualTo(true);
|
||||
}
|
||||
|
||||
@Test
|
||||
void taskDecoratorShouldApply() {
|
||||
TaskDecorator taskDecorator = mock(TaskDecorator.class);
|
||||
|
||||
Reference in New Issue
Block a user