Skip to content

Commit 29a07ba

Browse files
committed
Polish contribution
See gh-45155
1 parent 597c58f commit 29a07ba

File tree

5 files changed

+61
-53
lines changed

5 files changed

+61
-53
lines changed

spring-boot-project/spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/task/TaskExecutionProperties.java

Lines changed: 12 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -77,16 +77,24 @@ public void setThreadNamePrefix(String threadNamePrefix) {
7777

7878
public static class Simple {
7979

80+
/**
81+
* Whether to reject tasks when the concurrency limit has been reached.
82+
*/
83+
private boolean rejectTasksWhenLimitReached;
84+
8085
/**
8186
* Set the maximum number of parallel accesses allowed. -1 indicates no
8287
* concurrency limit at all.
8388
*/
8489
private Integer concurrencyLimit;
8590

86-
/**
87-
* Specify whether to reject tasks when the concurrency limit has been reached.
88-
*/
89-
private boolean rejectTasksWhenLimitReached;
91+
public boolean isRejectTasksWhenLimitReached() {
92+
return this.rejectTasksWhenLimitReached;
93+
}
94+
95+
public void setRejectTasksWhenLimitReached(boolean rejectTasksWhenLimitReached) {
96+
this.rejectTasksWhenLimitReached = rejectTasksWhenLimitReached;
97+
}
9098

9199
public Integer getConcurrencyLimit() {
92100
return this.concurrencyLimit;
@@ -96,14 +104,6 @@ public void setConcurrencyLimit(Integer concurrencyLimit) {
96104
this.concurrencyLimit = concurrencyLimit;
97105
}
98106

99-
public boolean isRejectTasksWhenLimitReached() {
100-
return this.rejectTasksWhenLimitReached;
101-
}
102-
103-
public void setRejectTasksWhenLimitReached(boolean rejectTasksWhenLimitReached) {
104-
this.rejectTasksWhenLimitReached = rejectTasksWhenLimitReached;
105-
}
106-
107107
}
108108

109109
public static class Pool {

spring-boot-project/spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/task/TaskExecutorConfigurations.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -135,8 +135,8 @@ private SimpleAsyncTaskExecutorBuilder builder() {
135135
builder = builder.customizers(this.taskExecutorCustomizers.orderedStream()::iterator);
136136
builder = builder.taskDecorator(this.taskDecorator.getIfUnique());
137137
TaskExecutionProperties.Simple simple = this.properties.getSimple();
138-
builder = builder.concurrencyLimit(simple.getConcurrencyLimit());
139138
builder = builder.rejectTasksWhenLimitReached(simple.isRejectTasksWhenLimitReached());
139+
builder = builder.concurrencyLimit(simple.getConcurrencyLimit());
140140
TaskExecutionProperties.Shutdown shutdown = this.properties.getShutdown();
141141
if (shutdown.isAwaitTermination()) {
142142
builder = builder.taskTerminationTimeout(shutdown.getAwaitTerminationPeriod());

spring-boot-project/spring-boot-autoconfigure/src/test/java/org/springframework/boot/autoconfigure/task/TaskExecutionAutoConfigurationTests.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -83,13 +83,13 @@ void shouldSupplyBeans() {
8383
void simpleAsyncTaskExecutorBuilderShouldReadProperties() {
8484
this.contextRunner
8585
.withPropertyValues("spring.task.execution.thread-name-prefix=mytest-",
86-
"spring.task.execution.simple.concurrency-limit=1",
8786
"spring.task.execution.simple.reject-tasks-when-limit-reached=true",
87+
"spring.task.execution.simple.concurrency-limit=1",
8888
"spring.task.execution.shutdown.await-termination=true",
8989
"spring.task.execution.shutdown.await-termination-period=30s")
9090
.run(assertSimpleAsyncTaskExecutor((taskExecutor) -> {
91-
assertThat(taskExecutor.getConcurrencyLimit()).isEqualTo(1);
9291
assertThat(taskExecutor).hasFieldOrPropertyWithValue("rejectTasksWhenLimitReached", true);
92+
assertThat(taskExecutor.getConcurrencyLimit()).isEqualTo(1);
9393
assertThat(taskExecutor.getThreadNamePrefix()).isEqualTo("mytest-");
9494
assertThat(taskExecutor).hasFieldOrPropertyWithValue("taskTerminationTimeout", 30000L);
9595
}));

spring-boot-project/spring-boot/src/main/java/org/springframework/boot/task/SimpleAsyncTaskExecutorBuilder.java

Lines changed: 40 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -50,27 +50,27 @@ public class SimpleAsyncTaskExecutorBuilder {
5050

5151
private final String threadNamePrefix;
5252

53-
private final Integer concurrencyLimit;
54-
5553
private final boolean rejectTasksWhenLimitReached;
5654

55+
private final Integer concurrencyLimit;
56+
5757
private final TaskDecorator taskDecorator;
5858

5959
private final Set<SimpleAsyncTaskExecutorCustomizer> customizers;
6060

6161
private final Duration taskTerminationTimeout;
6262

6363
public SimpleAsyncTaskExecutorBuilder() {
64-
this(null, null, null, false, null, null, null);
64+
this(null, null, false, null, null, null, null);
6565
}
6666

67-
private SimpleAsyncTaskExecutorBuilder(Boolean virtualThreads, String threadNamePrefix, Integer concurrencyLimit,
68-
boolean rejectTasksWhenLimitReached, TaskDecorator taskDecorator,
67+
private SimpleAsyncTaskExecutorBuilder(Boolean virtualThreads, String threadNamePrefix,
68+
boolean rejectTasksWhenLimitReached, Integer concurrencyLimit, TaskDecorator taskDecorator,
6969
Set<SimpleAsyncTaskExecutorCustomizer> customizers, Duration taskTerminationTimeout) {
7070
this.virtualThreads = virtualThreads;
7171
this.threadNamePrefix = threadNamePrefix;
72-
this.concurrencyLimit = concurrencyLimit;
7372
this.rejectTasksWhenLimitReached = rejectTasksWhenLimitReached;
73+
this.concurrencyLimit = concurrencyLimit;
7474
this.taskDecorator = taskDecorator;
7575
this.customizers = customizers;
7676
this.taskTerminationTimeout = taskTerminationTimeout;
@@ -82,8 +82,9 @@ private SimpleAsyncTaskExecutorBuilder(Boolean virtualThreads, String threadName
8282
* @return a new builder instance
8383
*/
8484
public SimpleAsyncTaskExecutorBuilder threadNamePrefix(String threadNamePrefix) {
85-
return new SimpleAsyncTaskExecutorBuilder(this.virtualThreads, threadNamePrefix, this.concurrencyLimit,
86-
this.rejectTasksWhenLimitReached, this.taskDecorator, this.customizers, this.taskTerminationTimeout);
85+
return new SimpleAsyncTaskExecutorBuilder(this.virtualThreads, threadNamePrefix,
86+
this.rejectTasksWhenLimitReached, this.concurrencyLimit, this.taskDecorator, this.customizers,
87+
this.taskTerminationTimeout);
8788
}
8889

8990
/**
@@ -92,30 +93,35 @@ public SimpleAsyncTaskExecutorBuilder threadNamePrefix(String threadNamePrefix)
9293
* @return a new builder instance
9394
*/
9495
public SimpleAsyncTaskExecutorBuilder virtualThreads(Boolean virtualThreads) {
95-
return new SimpleAsyncTaskExecutorBuilder(virtualThreads, this.threadNamePrefix, this.concurrencyLimit,
96-
this.rejectTasksWhenLimitReached, this.taskDecorator, this.customizers, this.taskTerminationTimeout);
96+
return new SimpleAsyncTaskExecutorBuilder(virtualThreads, this.threadNamePrefix,
97+
this.rejectTasksWhenLimitReached, this.concurrencyLimit, this.taskDecorator, this.customizers,
98+
this.taskTerminationTimeout);
9799
}
98100

99101
/**
100-
* Set the concurrency limit.
101-
* @param concurrencyLimit the concurrency limit
102+
* Set whether to reject tasks when the concurrency limit has been reached. By default
103+
* {@code false} to block the caller until the submission can be accepted. Switch to
104+
* {@code true} for immediate rejection instead.
105+
* @param rejectTasksWhenLimitReached whether to reject tasks when the concurrency
106+
* limit has been reached
102107
* @return a new builder instance
108+
* @since 3.5.0
103109
*/
104-
public SimpleAsyncTaskExecutorBuilder concurrencyLimit(Integer concurrencyLimit) {
105-
return new SimpleAsyncTaskExecutorBuilder(this.virtualThreads, this.threadNamePrefix, concurrencyLimit,
106-
this.rejectTasksWhenLimitReached, this.taskDecorator, this.customizers, this.taskTerminationTimeout);
110+
public SimpleAsyncTaskExecutorBuilder rejectTasksWhenLimitReached(boolean rejectTasksWhenLimitReached) {
111+
return new SimpleAsyncTaskExecutorBuilder(this.virtualThreads, this.threadNamePrefix,
112+
rejectTasksWhenLimitReached, this.concurrencyLimit, this.taskDecorator, this.customizers,
113+
this.taskTerminationTimeout);
107114
}
108115

109116
/**
110-
* Specify whether to reject tasks when the concurrency limit has been reached.
111-
* @param rejectTasksWhenLimitReached whether to reject tasks when the concurrency
112-
* limit has been reached
117+
* Set the concurrency limit.
118+
* @param concurrencyLimit the concurrency limit
113119
* @return a new builder instance
114-
* @since 3.5.0
115120
*/
116-
public SimpleAsyncTaskExecutorBuilder rejectTasksWhenLimitReached(boolean rejectTasksWhenLimitReached) {
117-
return new SimpleAsyncTaskExecutorBuilder(this.virtualThreads, this.threadNamePrefix, this.concurrencyLimit,
118-
rejectTasksWhenLimitReached, this.taskDecorator, this.customizers, this.taskTerminationTimeout);
121+
public SimpleAsyncTaskExecutorBuilder concurrencyLimit(Integer concurrencyLimit) {
122+
return new SimpleAsyncTaskExecutorBuilder(this.virtualThreads, this.threadNamePrefix,
123+
this.rejectTasksWhenLimitReached, concurrencyLimit, this.taskDecorator, this.customizers,
124+
this.taskTerminationTimeout);
119125
}
120126

121127
/**
@@ -124,8 +130,9 @@ public SimpleAsyncTaskExecutorBuilder rejectTasksWhenLimitReached(boolean reject
124130
* @return a new builder instance
125131
*/
126132
public SimpleAsyncTaskExecutorBuilder taskDecorator(TaskDecorator taskDecorator) {
127-
return new SimpleAsyncTaskExecutorBuilder(this.virtualThreads, this.threadNamePrefix, this.concurrencyLimit,
128-
this.rejectTasksWhenLimitReached, taskDecorator, this.customizers, this.taskTerminationTimeout);
133+
return new SimpleAsyncTaskExecutorBuilder(this.virtualThreads, this.threadNamePrefix,
134+
this.rejectTasksWhenLimitReached, this.concurrencyLimit, taskDecorator, this.customizers,
135+
this.taskTerminationTimeout);
129136
}
130137

131138
/**
@@ -135,8 +142,9 @@ public SimpleAsyncTaskExecutorBuilder taskDecorator(TaskDecorator taskDecorator)
135142
* @since 3.2.1
136143
*/
137144
public SimpleAsyncTaskExecutorBuilder taskTerminationTimeout(Duration taskTerminationTimeout) {
138-
return new SimpleAsyncTaskExecutorBuilder(this.virtualThreads, this.threadNamePrefix, this.concurrencyLimit,
139-
this.rejectTasksWhenLimitReached, this.taskDecorator, this.customizers, taskTerminationTimeout);
145+
return new SimpleAsyncTaskExecutorBuilder(this.virtualThreads, this.threadNamePrefix,
146+
this.rejectTasksWhenLimitReached, this.concurrencyLimit, this.taskDecorator, this.customizers,
147+
taskTerminationTimeout);
140148
}
141149

142150
/**
@@ -165,8 +173,8 @@ public SimpleAsyncTaskExecutorBuilder customizers(SimpleAsyncTaskExecutorCustomi
165173
public SimpleAsyncTaskExecutorBuilder customizers(
166174
Iterable<? extends SimpleAsyncTaskExecutorCustomizer> customizers) {
167175
Assert.notNull(customizers, "'customizers' must not be null");
168-
return new SimpleAsyncTaskExecutorBuilder(this.virtualThreads, this.threadNamePrefix, this.concurrencyLimit,
169-
this.rejectTasksWhenLimitReached, this.taskDecorator, append(null, customizers),
176+
return new SimpleAsyncTaskExecutorBuilder(this.virtualThreads, this.threadNamePrefix,
177+
this.rejectTasksWhenLimitReached, this.concurrencyLimit, this.taskDecorator, append(null, customizers),
170178
this.taskTerminationTimeout);
171179
}
172180

@@ -194,9 +202,9 @@ public SimpleAsyncTaskExecutorBuilder additionalCustomizers(SimpleAsyncTaskExecu
194202
public SimpleAsyncTaskExecutorBuilder additionalCustomizers(
195203
Iterable<? extends SimpleAsyncTaskExecutorCustomizer> customizers) {
196204
Assert.notNull(customizers, "'customizers' must not be null");
197-
return new SimpleAsyncTaskExecutorBuilder(this.virtualThreads, this.threadNamePrefix, this.concurrencyLimit,
198-
this.rejectTasksWhenLimitReached, this.taskDecorator, append(this.customizers, customizers),
199-
this.taskTerminationTimeout);
205+
return new SimpleAsyncTaskExecutorBuilder(this.virtualThreads, this.threadNamePrefix,
206+
this.rejectTasksWhenLimitReached, this.concurrencyLimit, this.taskDecorator,
207+
append(this.customizers, customizers), this.taskTerminationTimeout);
200208
}
201209

202210
/**
@@ -235,8 +243,8 @@ public <T extends SimpleAsyncTaskExecutor> T configure(T taskExecutor) {
235243
PropertyMapper map = PropertyMapper.get().alwaysApplyingWhenNonNull();
236244
map.from(this.virtualThreads).to(taskExecutor::setVirtualThreads);
237245
map.from(this.threadNamePrefix).whenHasText().to(taskExecutor::setThreadNamePrefix);
238-
map.from(this.concurrencyLimit).to(taskExecutor::setConcurrencyLimit);
239246
map.from(this.rejectTasksWhenLimitReached).to(taskExecutor::setRejectTasksWhenLimitReached);
247+
map.from(this.concurrencyLimit).to(taskExecutor::setConcurrencyLimit);
240248
map.from(this.taskDecorator).to(taskExecutor::setTaskDecorator);
241249
map.from(this.taskTerminationTimeout).as(Duration::toMillis).to(taskExecutor::setTaskTerminationTimeout);
242250
if (!CollectionUtils.isEmpty(this.customizers)) {

spring-boot-project/spring-boot/src/test/java/org/springframework/boot/task/SimpleAsyncTaskExecutorBuilderTests.java

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -59,18 +59,18 @@ void virtualThreadsShouldApply() {
5959
SimpleAsyncTaskExecutorAssert.assertThat(executor).usesVirtualThreads();
6060
}
6161

62-
@Test
63-
void concurrencyLimitShouldApply() {
64-
SimpleAsyncTaskExecutor executor = this.builder.concurrencyLimit(1).build();
65-
assertThat(executor.getConcurrencyLimit()).isEqualTo(1);
66-
}
67-
6862
@Test
6963
void rejectTasksWhenLimitReachedShouldApply() {
7064
SimpleAsyncTaskExecutor executor = this.builder.rejectTasksWhenLimitReached(true).build();
7165
assertThat(executor).extracting("rejectTasksWhenLimitReached").isEqualTo(true);
7266
}
7367

68+
@Test
69+
void concurrencyLimitShouldApply() {
70+
SimpleAsyncTaskExecutor executor = this.builder.concurrencyLimit(1).build();
71+
assertThat(executor.getConcurrencyLimit()).isEqualTo(1);
72+
}
73+
7474
@Test
7575
void taskDecoratorShouldApply() {
7676
TaskDecorator taskDecorator = mock(TaskDecorator.class);

0 commit comments

Comments
 (0)