Polish contribution

See gh-45155
This commit is contained in:
Stéphane Nicoll 2025-04-11 12:00:36 +02:00
parent 597c58f43d
commit 29a07ba277
5 changed files with 64 additions and 56 deletions

View File

@ -77,25 +77,17 @@ public class TaskExecutionProperties {
public static class Simple {
/**
* Whether to reject tasks when the concurrency limit has been reached.
*/
private boolean rejectTasksWhenLimitReached;
/**
* Set the maximum number of parallel accesses allowed. -1 indicates no
* concurrency limit at all.
*/
private Integer concurrencyLimit;
/**
* Specify whether to reject tasks when the concurrency limit has been reached.
*/
private boolean rejectTasksWhenLimitReached;
public Integer getConcurrencyLimit() {
return this.concurrencyLimit;
}
public void setConcurrencyLimit(Integer concurrencyLimit) {
this.concurrencyLimit = concurrencyLimit;
}
public boolean isRejectTasksWhenLimitReached() {
return this.rejectTasksWhenLimitReached;
}
@ -104,6 +96,14 @@ public class TaskExecutionProperties {
this.rejectTasksWhenLimitReached = rejectTasksWhenLimitReached;
}
public Integer getConcurrencyLimit() {
return this.concurrencyLimit;
}
public void setConcurrencyLimit(Integer concurrencyLimit) {
this.concurrencyLimit = concurrencyLimit;
}
}
public static class Pool {

View File

@ -135,8 +135,8 @@ class TaskExecutorConfigurations {
builder = builder.customizers(this.taskExecutorCustomizers.orderedStream()::iterator);
builder = builder.taskDecorator(this.taskDecorator.getIfUnique());
TaskExecutionProperties.Simple simple = this.properties.getSimple();
builder = builder.concurrencyLimit(simple.getConcurrencyLimit());
builder = builder.rejectTasksWhenLimitReached(simple.isRejectTasksWhenLimitReached());
builder = builder.concurrencyLimit(simple.getConcurrencyLimit());
TaskExecutionProperties.Shutdown shutdown = this.properties.getShutdown();
if (shutdown.isAwaitTermination()) {
builder = builder.taskTerminationTimeout(shutdown.getAwaitTerminationPeriod());

View File

@ -83,13 +83,13 @@ class TaskExecutionAutoConfigurationTests {
void simpleAsyncTaskExecutorBuilderShouldReadProperties() {
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.simple.concurrency-limit=1",
"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.getConcurrencyLimit()).isEqualTo(1);
assertThat(taskExecutor.getThreadNamePrefix()).isEqualTo("mytest-");
assertThat(taskExecutor).hasFieldOrPropertyWithValue("taskTerminationTimeout", 30000L);
}));

View File

@ -50,10 +50,10 @@ public class SimpleAsyncTaskExecutorBuilder {
private final String threadNamePrefix;
private final Integer concurrencyLimit;
private final boolean rejectTasksWhenLimitReached;
private final Integer concurrencyLimit;
private final TaskDecorator taskDecorator;
private final Set<SimpleAsyncTaskExecutorCustomizer> customizers;
@ -61,16 +61,16 @@ public class SimpleAsyncTaskExecutorBuilder {
private final Duration taskTerminationTimeout;
public SimpleAsyncTaskExecutorBuilder() {
this(null, null, null, false, null, null, null);
this(null, null, false, null, null, null, null);
}
private SimpleAsyncTaskExecutorBuilder(Boolean virtualThreads, String threadNamePrefix, Integer concurrencyLimit,
boolean rejectTasksWhenLimitReached, TaskDecorator taskDecorator,
private SimpleAsyncTaskExecutorBuilder(Boolean virtualThreads, String threadNamePrefix,
boolean rejectTasksWhenLimitReached, Integer concurrencyLimit, TaskDecorator taskDecorator,
Set<SimpleAsyncTaskExecutorCustomizer> customizers, Duration taskTerminationTimeout) {
this.virtualThreads = virtualThreads;
this.threadNamePrefix = threadNamePrefix;
this.concurrencyLimit = concurrencyLimit;
this.rejectTasksWhenLimitReached = rejectTasksWhenLimitReached;
this.concurrencyLimit = concurrencyLimit;
this.taskDecorator = taskDecorator;
this.customizers = customizers;
this.taskTerminationTimeout = taskTerminationTimeout;
@ -82,8 +82,9 @@ public class SimpleAsyncTaskExecutorBuilder {
* @return a new builder instance
*/
public SimpleAsyncTaskExecutorBuilder threadNamePrefix(String threadNamePrefix) {
return new SimpleAsyncTaskExecutorBuilder(this.virtualThreads, threadNamePrefix, this.concurrencyLimit,
this.rejectTasksWhenLimitReached, this.taskDecorator, this.customizers, this.taskTerminationTimeout);
return new SimpleAsyncTaskExecutorBuilder(this.virtualThreads, threadNamePrefix,
this.rejectTasksWhenLimitReached, this.concurrencyLimit, this.taskDecorator, this.customizers,
this.taskTerminationTimeout);
}
/**
@ -92,8 +93,24 @@ public class SimpleAsyncTaskExecutorBuilder {
* @return a new builder instance
*/
public SimpleAsyncTaskExecutorBuilder virtualThreads(Boolean virtualThreads) {
return new SimpleAsyncTaskExecutorBuilder(virtualThreads, this.threadNamePrefix, this.concurrencyLimit,
this.rejectTasksWhenLimitReached, this.taskDecorator, this.customizers, this.taskTerminationTimeout);
return new SimpleAsyncTaskExecutorBuilder(virtualThreads, this.threadNamePrefix,
this.rejectTasksWhenLimitReached, this.concurrencyLimit, this.taskDecorator, this.customizers,
this.taskTerminationTimeout);
}
/**
* Set whether to reject tasks when the concurrency limit has been reached. By default
* {@code false} to block the caller until the submission can be accepted. Switch to
* {@code true} for immediate rejection instead.
* @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,
rejectTasksWhenLimitReached, this.concurrencyLimit, this.taskDecorator, this.customizers,
this.taskTerminationTimeout);
}
/**
@ -102,20 +119,9 @@ public class SimpleAsyncTaskExecutorBuilder {
* @return a new builder instance
*/
public SimpleAsyncTaskExecutorBuilder concurrencyLimit(Integer concurrencyLimit) {
return new SimpleAsyncTaskExecutorBuilder(this.virtualThreads, this.threadNamePrefix, concurrencyLimit,
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);
return new SimpleAsyncTaskExecutorBuilder(this.virtualThreads, this.threadNamePrefix,
this.rejectTasksWhenLimitReached, concurrencyLimit, this.taskDecorator, this.customizers,
this.taskTerminationTimeout);
}
/**
@ -124,8 +130,9 @@ public class SimpleAsyncTaskExecutorBuilder {
* @return a new builder instance
*/
public SimpleAsyncTaskExecutorBuilder taskDecorator(TaskDecorator taskDecorator) {
return new SimpleAsyncTaskExecutorBuilder(this.virtualThreads, this.threadNamePrefix, this.concurrencyLimit,
this.rejectTasksWhenLimitReached, taskDecorator, this.customizers, this.taskTerminationTimeout);
return new SimpleAsyncTaskExecutorBuilder(this.virtualThreads, this.threadNamePrefix,
this.rejectTasksWhenLimitReached, this.concurrencyLimit, taskDecorator, this.customizers,
this.taskTerminationTimeout);
}
/**
@ -135,8 +142,9 @@ public class SimpleAsyncTaskExecutorBuilder {
* @since 3.2.1
*/
public SimpleAsyncTaskExecutorBuilder taskTerminationTimeout(Duration taskTerminationTimeout) {
return new SimpleAsyncTaskExecutorBuilder(this.virtualThreads, this.threadNamePrefix, this.concurrencyLimit,
this.rejectTasksWhenLimitReached, this.taskDecorator, this.customizers, taskTerminationTimeout);
return new SimpleAsyncTaskExecutorBuilder(this.virtualThreads, this.threadNamePrefix,
this.rejectTasksWhenLimitReached, this.concurrencyLimit, this.taskDecorator, this.customizers,
taskTerminationTimeout);
}
/**
@ -165,8 +173,8 @@ public class SimpleAsyncTaskExecutorBuilder {
public SimpleAsyncTaskExecutorBuilder customizers(
Iterable<? extends SimpleAsyncTaskExecutorCustomizer> customizers) {
Assert.notNull(customizers, "'customizers' must not be null");
return new SimpleAsyncTaskExecutorBuilder(this.virtualThreads, this.threadNamePrefix, this.concurrencyLimit,
this.rejectTasksWhenLimitReached, this.taskDecorator, append(null, customizers),
return new SimpleAsyncTaskExecutorBuilder(this.virtualThreads, this.threadNamePrefix,
this.rejectTasksWhenLimitReached, this.concurrencyLimit, this.taskDecorator, append(null, customizers),
this.taskTerminationTimeout);
}
@ -194,9 +202,9 @@ public class SimpleAsyncTaskExecutorBuilder {
public SimpleAsyncTaskExecutorBuilder additionalCustomizers(
Iterable<? extends SimpleAsyncTaskExecutorCustomizer> customizers) {
Assert.notNull(customizers, "'customizers' must not be null");
return new SimpleAsyncTaskExecutorBuilder(this.virtualThreads, this.threadNamePrefix, this.concurrencyLimit,
this.rejectTasksWhenLimitReached, this.taskDecorator, append(this.customizers, customizers),
this.taskTerminationTimeout);
return new SimpleAsyncTaskExecutorBuilder(this.virtualThreads, this.threadNamePrefix,
this.rejectTasksWhenLimitReached, this.concurrencyLimit, this.taskDecorator,
append(this.customizers, customizers), this.taskTerminationTimeout);
}
/**
@ -235,8 +243,8 @@ public class SimpleAsyncTaskExecutorBuilder {
PropertyMapper map = PropertyMapper.get().alwaysApplyingWhenNonNull();
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.concurrencyLimit).to(taskExecutor::setConcurrencyLimit);
map.from(this.taskDecorator).to(taskExecutor::setTaskDecorator);
map.from(this.taskTerminationTimeout).as(Duration::toMillis).to(taskExecutor::setTaskTerminationTimeout);
if (!CollectionUtils.isEmpty(this.customizers)) {

View File

@ -59,18 +59,18 @@ class SimpleAsyncTaskExecutorBuilderTests {
SimpleAsyncTaskExecutorAssert.assertThat(executor).usesVirtualThreads();
}
@Test
void concurrencyLimitShouldApply() {
SimpleAsyncTaskExecutor executor = this.builder.concurrencyLimit(1).build();
assertThat(executor.getConcurrencyLimit()).isEqualTo(1);
}
@Test
void rejectTasksWhenLimitReachedShouldApply() {
SimpleAsyncTaskExecutor executor = this.builder.rejectTasksWhenLimitReached(true).build();
assertThat(executor).extracting("rejectTasksWhenLimitReached").isEqualTo(true);
}
@Test
void concurrencyLimitShouldApply() {
SimpleAsyncTaskExecutor executor = this.builder.concurrencyLimit(1).build();
assertThat(executor.getConcurrencyLimit()).isEqualTo(1);
}
@Test
void taskDecoratorShouldApply() {
TaskDecorator taskDecorator = mock(TaskDecorator.class);