Skip to content

Commit ced7c16

Browse files
committed
Polish "Add TaskDecorator support for scheduled tasks"
See gh-43190
1 parent f0c5312 commit ced7c16

File tree

6 files changed

+66
-77
lines changed

6 files changed

+66
-77
lines changed

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

+9-9
Original file line numberDiff line numberDiff line change
@@ -68,16 +68,16 @@ static class ThreadPoolTaskSchedulerBuilderConfiguration {
6868
@Bean
6969
@ConditionalOnMissingBean
7070
ThreadPoolTaskSchedulerBuilder threadPoolTaskSchedulerBuilder(TaskSchedulingProperties properties,
71-
ObjectProvider<ThreadPoolTaskSchedulerCustomizer> threadPoolTaskSchedulerCustomizers,
72-
ObjectProvider<TaskDecorator> taskDecorator) {
71+
ObjectProvider<TaskDecorator> taskDecorator,
72+
ObjectProvider<ThreadPoolTaskSchedulerCustomizer> threadPoolTaskSchedulerCustomizers) {
7373
TaskSchedulingProperties.Shutdown shutdown = properties.getShutdown();
7474
ThreadPoolTaskSchedulerBuilder builder = new ThreadPoolTaskSchedulerBuilder();
7575
builder = builder.poolSize(properties.getPool().getSize());
7676
builder = builder.awaitTermination(shutdown.isAwaitTermination());
7777
builder = builder.awaitTerminationPeriod(shutdown.getAwaitTerminationPeriod());
7878
builder = builder.threadNamePrefix(properties.getThreadNamePrefix());
79-
builder = builder.customizers(threadPoolTaskSchedulerCustomizers);
8079
builder = builder.taskDecorator(taskDecorator.getIfUnique());
80+
builder = builder.customizers(threadPoolTaskSchedulerCustomizers);
8181
return builder;
8282
}
8383

@@ -88,16 +88,16 @@ static class SimpleAsyncTaskSchedulerBuilderConfiguration {
8888

8989
private final TaskSchedulingProperties properties;
9090

91-
private final ObjectProvider<SimpleAsyncTaskSchedulerCustomizer> taskSchedulerCustomizers;
92-
9391
private final ObjectProvider<TaskDecorator> taskDecorator;
9492

93+
private final ObjectProvider<SimpleAsyncTaskSchedulerCustomizer> taskSchedulerCustomizers;
94+
9595
SimpleAsyncTaskSchedulerBuilderConfiguration(TaskSchedulingProperties properties,
96-
ObjectProvider<SimpleAsyncTaskSchedulerCustomizer> taskSchedulerCustomizers,
97-
ObjectProvider<TaskDecorator> taskDecorator) {
96+
ObjectProvider<TaskDecorator> taskDecorator,
97+
ObjectProvider<SimpleAsyncTaskSchedulerCustomizer> taskSchedulerCustomizers) {
9898
this.properties = properties;
99-
this.taskSchedulerCustomizers = taskSchedulerCustomizers;
10099
this.taskDecorator = taskDecorator;
100+
this.taskSchedulerCustomizers = taskSchedulerCustomizers;
101101
}
102102

103103
@Bean
@@ -117,14 +117,14 @@ SimpleAsyncTaskSchedulerBuilder simpleAsyncTaskSchedulerBuilderVirtualThreads()
117117
private SimpleAsyncTaskSchedulerBuilder builder() {
118118
SimpleAsyncTaskSchedulerBuilder builder = new SimpleAsyncTaskSchedulerBuilder();
119119
builder = builder.threadNamePrefix(this.properties.getThreadNamePrefix());
120+
builder = builder.taskDecorator(this.taskDecorator.getIfUnique());
120121
builder = builder.customizers(this.taskSchedulerCustomizers.orderedStream()::iterator);
121122
TaskSchedulingProperties.Simple simple = this.properties.getSimple();
122123
builder = builder.concurrencyLimit(simple.getConcurrencyLimit());
123124
TaskSchedulingProperties.Shutdown shutdown = this.properties.getShutdown();
124125
if (shutdown.isAwaitTermination()) {
125126
builder = builder.taskTerminationTimeout(shutdown.getAwaitTerminationPeriod());
126127
}
127-
builder = builder.taskDecorator(this.taskDecorator.getIfUnique());
128128
return builder;
129129
}
130130

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

+15-15
Original file line numberDiff line numberDiff line change
@@ -141,21 +141,6 @@ void simpleAsyncTaskSchedulerBuilderShouldUsePlatformThreadsByDefault() {
141141
});
142142
}
143143

144-
@Test
145-
void simpleAsyncTaskSchedulerBuilderShouldApplyCustomizers() {
146-
SimpleAsyncTaskSchedulerCustomizer customizer = (scheduler) -> {
147-
};
148-
this.contextRunner.withBean(SimpleAsyncTaskSchedulerCustomizer.class, () -> customizer)
149-
.withUserConfiguration(SchedulingConfiguration.class)
150-
.run((context) -> {
151-
assertThat(context).hasSingleBean(SimpleAsyncTaskSchedulerBuilder.class);
152-
SimpleAsyncTaskSchedulerBuilder builder = context.getBean(SimpleAsyncTaskSchedulerBuilder.class);
153-
assertThat(builder).extracting("customizers")
154-
.asInstanceOf(InstanceOfAssertFactories.collection(SimpleAsyncTaskSchedulerCustomizer.class))
155-
.containsExactly(customizer);
156-
});
157-
}
158-
159144
@Test
160145
void simpleAsyncTaskSchedulerBuilderShouldApplyTaskDecorator() {
161146
this.contextRunner.withUserConfiguration(SchedulingConfiguration.class, TaskDecoratorConfig.class)
@@ -180,6 +165,21 @@ void threadPoolTaskSchedulerBuilderShouldApplyTaskDecorator() {
180165
});
181166
}
182167

168+
@Test
169+
void simpleAsyncTaskSchedulerBuilderShouldApplyCustomizers() {
170+
SimpleAsyncTaskSchedulerCustomizer customizer = (scheduler) -> {
171+
};
172+
this.contextRunner.withBean(SimpleAsyncTaskSchedulerCustomizer.class, () -> customizer)
173+
.withUserConfiguration(SchedulingConfiguration.class)
174+
.run((context) -> {
175+
assertThat(context).hasSingleBean(SimpleAsyncTaskSchedulerBuilder.class);
176+
SimpleAsyncTaskSchedulerBuilder builder = context.getBean(SimpleAsyncTaskSchedulerBuilder.class);
177+
assertThat(builder).extracting("customizers")
178+
.asInstanceOf(InstanceOfAssertFactories.collection(SimpleAsyncTaskSchedulerCustomizer.class))
179+
.containsExactly(customizer);
180+
});
181+
}
182+
183183
@Test
184184
void enableSchedulingWithNoTaskExecutorAppliesCustomizers() {
185185
this.contextRunner.withPropertyValues("spring.task.scheduling.thread-name-prefix=scheduling-test-")

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

+11-16
Original file line numberDiff line numberDiff line change
@@ -54,11 +54,6 @@ public class SimpleAsyncTaskSchedulerBuilder {
5454

5555
private final Set<SimpleAsyncTaskSchedulerCustomizer> customizers;
5656

57-
/**
58-
* Constructs a new {@code SimpleAsyncTaskSchedulerBuilder} with default settings.
59-
* Initializes a builder instance with all fields set to {@code null}, allowing for
60-
* further customization through its fluent API methods.
61-
*/
6257
public SimpleAsyncTaskSchedulerBuilder() {
6358
this(null, null, null, null, null, null);
6459
}
@@ -115,6 +110,17 @@ public SimpleAsyncTaskSchedulerBuilder taskTerminationTimeout(Duration taskTermi
115110
taskTerminationTimeout, this.taskDecorator, this.customizers);
116111
}
117112

113+
/**
114+
* Set the task decorator to be used by the {@link SimpleAsyncTaskScheduler}.
115+
* @param taskDecorator the task decorator to set
116+
* @return a new builder instance
117+
* @since 3.5.0
118+
*/
119+
public SimpleAsyncTaskSchedulerBuilder taskDecorator(TaskDecorator taskDecorator) {
120+
return new SimpleAsyncTaskSchedulerBuilder(this.threadNamePrefix, this.concurrencyLimit, this.virtualThreads,
121+
this.taskTerminationTimeout, taskDecorator, this.customizers);
122+
}
123+
118124
/**
119125
* Set the {@link SimpleAsyncTaskSchedulerCustomizer customizers} that should be
120126
* applied to the {@link SimpleAsyncTaskScheduler}. Customizers are applied in the
@@ -173,17 +179,6 @@ public SimpleAsyncTaskSchedulerBuilder additionalCustomizers(
173179
this.taskTerminationTimeout, this.taskDecorator, append(this.customizers, customizers));
174180
}
175181

176-
/**
177-
* Set the task decorator to be used by the {@link SimpleAsyncTaskScheduler}.
178-
* @param taskDecorator the task decorator to set
179-
* @return a new builder instance
180-
* @since 3.5.0
181-
*/
182-
public SimpleAsyncTaskSchedulerBuilder taskDecorator(TaskDecorator taskDecorator) {
183-
return new SimpleAsyncTaskSchedulerBuilder(this.threadNamePrefix, this.concurrencyLimit, this.virtualThreads,
184-
this.taskTerminationTimeout, taskDecorator, this.customizers);
185-
}
186-
187182
/**
188183
* Build a new {@link SimpleAsyncTaskScheduler} instance and configure it using this
189184
* builder.

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

+11-17
Original file line numberDiff line numberDiff line change
@@ -53,12 +53,6 @@ public class ThreadPoolTaskSchedulerBuilder {
5353

5454
private final Set<ThreadPoolTaskSchedulerCustomizer> customizers;
5555

56-
/**
57-
* Default constructor for creating a new instance of
58-
* {@code ThreadPoolTaskSchedulerBuilder}. Initializes a builder instance with all
59-
* fields set to {@code null}, allowing for further customization through its fluent
60-
* API methods.
61-
*/
6256
public ThreadPoolTaskSchedulerBuilder() {
6357
this(null, null, null, null, null, null);
6458
}
@@ -79,18 +73,18 @@ public ThreadPoolTaskSchedulerBuilder() {
7973
@Deprecated(since = "3.5.0", forRemoval = true)
8074
public ThreadPoolTaskSchedulerBuilder(Integer poolSize, Boolean awaitTermination, Duration awaitTerminationPeriod,
8175
String threadNamePrefix, Set<ThreadPoolTaskSchedulerCustomizer> taskSchedulerCustomizers) {
82-
this(poolSize, awaitTermination, awaitTerminationPeriod, threadNamePrefix, taskSchedulerCustomizers, null);
76+
this(poolSize, awaitTermination, awaitTerminationPeriod, threadNamePrefix, null, taskSchedulerCustomizers);
8377
}
8478

8579
private ThreadPoolTaskSchedulerBuilder(Integer poolSize, Boolean awaitTermination, Duration awaitTerminationPeriod,
86-
String threadNamePrefix, Set<ThreadPoolTaskSchedulerCustomizer> taskSchedulerCustomizers,
87-
TaskDecorator taskDecorator) {
80+
String threadNamePrefix, TaskDecorator taskDecorator,
81+
Set<ThreadPoolTaskSchedulerCustomizer> taskSchedulerCustomizers) {
8882
this.poolSize = poolSize;
8983
this.awaitTermination = awaitTermination;
9084
this.awaitTerminationPeriod = awaitTerminationPeriod;
9185
this.threadNamePrefix = threadNamePrefix;
92-
this.customizers = taskSchedulerCustomizers;
9386
this.taskDecorator = taskDecorator;
87+
this.customizers = taskSchedulerCustomizers;
9488
}
9589

9690
/**
@@ -100,7 +94,7 @@ private ThreadPoolTaskSchedulerBuilder(Integer poolSize, Boolean awaitTerminatio
10094
*/
10195
public ThreadPoolTaskSchedulerBuilder poolSize(int poolSize) {
10296
return new ThreadPoolTaskSchedulerBuilder(poolSize, this.awaitTermination, this.awaitTerminationPeriod,
103-
this.threadNamePrefix, this.customizers, this.taskDecorator);
97+
this.threadNamePrefix, this.taskDecorator, this.customizers);
10498
}
10599

106100
/**
@@ -113,7 +107,7 @@ public ThreadPoolTaskSchedulerBuilder poolSize(int poolSize) {
113107
*/
114108
public ThreadPoolTaskSchedulerBuilder awaitTermination(boolean awaitTermination) {
115109
return new ThreadPoolTaskSchedulerBuilder(this.poolSize, awaitTermination, this.awaitTerminationPeriod,
116-
this.threadNamePrefix, this.customizers, this.taskDecorator);
110+
this.threadNamePrefix, this.taskDecorator, this.customizers);
117111
}
118112

119113
/**
@@ -127,7 +121,7 @@ public ThreadPoolTaskSchedulerBuilder awaitTermination(boolean awaitTermination)
127121
*/
128122
public ThreadPoolTaskSchedulerBuilder awaitTerminationPeriod(Duration awaitTerminationPeriod) {
129123
return new ThreadPoolTaskSchedulerBuilder(this.poolSize, this.awaitTermination, awaitTerminationPeriod,
130-
this.threadNamePrefix, this.customizers, this.taskDecorator);
124+
this.threadNamePrefix, this.taskDecorator, this.customizers);
131125
}
132126

133127
/**
@@ -137,7 +131,7 @@ public ThreadPoolTaskSchedulerBuilder awaitTerminationPeriod(Duration awaitTermi
137131
*/
138132
public ThreadPoolTaskSchedulerBuilder threadNamePrefix(String threadNamePrefix) {
139133
return new ThreadPoolTaskSchedulerBuilder(this.poolSize, this.awaitTermination, this.awaitTerminationPeriod,
140-
threadNamePrefix, this.customizers, this.taskDecorator);
134+
threadNamePrefix, this.taskDecorator, this.customizers);
141135
}
142136

143137
/**
@@ -148,7 +142,7 @@ public ThreadPoolTaskSchedulerBuilder threadNamePrefix(String threadNamePrefix)
148142
*/
149143
public ThreadPoolTaskSchedulerBuilder taskDecorator(TaskDecorator taskDecorator) {
150144
return new ThreadPoolTaskSchedulerBuilder(this.poolSize, this.awaitTermination, this.awaitTerminationPeriod,
151-
this.threadNamePrefix, this.customizers, taskDecorator);
145+
this.threadNamePrefix, taskDecorator, this.customizers);
152146
}
153147

154148
/**
@@ -180,7 +174,7 @@ public ThreadPoolTaskSchedulerBuilder customizers(
180174
Iterable<? extends ThreadPoolTaskSchedulerCustomizer> customizers) {
181175
Assert.notNull(customizers, "Customizers must not be null");
182176
return new ThreadPoolTaskSchedulerBuilder(this.poolSize, this.awaitTermination, this.awaitTerminationPeriod,
183-
this.threadNamePrefix, append(null, customizers), this.taskDecorator);
177+
this.threadNamePrefix, this.taskDecorator, append(null, customizers));
184178
}
185179

186180
/**
@@ -210,7 +204,7 @@ public ThreadPoolTaskSchedulerBuilder additionalCustomizers(
210204
Iterable<? extends ThreadPoolTaskSchedulerCustomizer> customizers) {
211205
Assert.notNull(customizers, "Customizers must not be null");
212206
return new ThreadPoolTaskSchedulerBuilder(this.poolSize, this.awaitTermination, this.awaitTerminationPeriod,
213-
this.threadNamePrefix, append(this.customizers, customizers), this.taskDecorator);
207+
this.threadNamePrefix, this.taskDecorator, append(this.customizers, customizers));
214208
}
215209

216210
/**

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

+13-13
Original file line numberDiff line numberDiff line change
@@ -62,6 +62,19 @@ void virtualThreadsShouldApply() {
6262
assertThat(scheduler).extracting("virtualThreadDelegate").isNotNull();
6363
}
6464

65+
@Test
66+
void taskTerminationTimeoutShouldApply() {
67+
SimpleAsyncTaskScheduler scheduler = this.builder.taskTerminationTimeout(Duration.ofSeconds(1)).build();
68+
assertThat(scheduler).extracting("taskTerminationTimeout").isEqualTo(1000L);
69+
}
70+
71+
@Test
72+
void taskDecoratorShouldApply() {
73+
TaskDecorator taskDecorator = mock(TaskDecorator.class);
74+
SimpleAsyncTaskScheduler scheduler = this.builder.taskDecorator(taskDecorator).build();
75+
assertThat(scheduler).extracting("taskDecorator").isSameAs(taskDecorator);
76+
}
77+
6578
@Test
6679
void customizersWhenCustomizersAreNullShouldThrowException() {
6780
assertThatIllegalArgumentException()
@@ -129,17 +142,4 @@ void additionalCustomizersShouldAddToExisting() {
129142
then(customizer2).should().customize(scheduler);
130143
}
131144

132-
@Test
133-
void taskTerminationTimeoutShouldApply() {
134-
SimpleAsyncTaskScheduler scheduler = this.builder.taskTerminationTimeout(Duration.ofSeconds(1)).build();
135-
assertThat(scheduler).extracting("taskTerminationTimeout").isEqualTo(1000L);
136-
}
137-
138-
@Test
139-
void taskDecoratorShouldApply() {
140-
TaskDecorator taskDecorator = mock(TaskDecorator.class);
141-
SimpleAsyncTaskScheduler scheduler = this.builder.taskDecorator(taskDecorator).build();
142-
assertThat(scheduler).extracting("taskDecorator").isSameAs(taskDecorator);
143-
}
144-
145145
}

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

+7-7
Original file line numberDiff line numberDiff line change
@@ -65,6 +65,13 @@ void threadNamePrefixShouldApply() {
6565
assertThat(scheduler.getThreadNamePrefix()).isEqualTo("test-");
6666
}
6767

68+
@Test
69+
void taskDecoratorShouldApply() {
70+
TaskDecorator taskDecorator = mock(TaskDecorator.class);
71+
ThreadPoolTaskScheduler scheduler = this.builder.taskDecorator(taskDecorator).build();
72+
assertThat(scheduler).extracting("taskDecorator").isSameAs(taskDecorator);
73+
}
74+
6875
@Test
6976
void customizersWhenCustomizersAreNullShouldThrowException() {
7077
assertThatIllegalArgumentException()
@@ -132,11 +139,4 @@ void additionalCustomizersShouldAddToExisting() {
132139
then(customizer2).should().customize(scheduler);
133140
}
134141

135-
@Test
136-
void taskDecoratorShouldApply() {
137-
TaskDecorator taskDecorator = mock(TaskDecorator.class);
138-
ThreadPoolTaskScheduler scheduler = this.builder.taskDecorator(taskDecorator).build();
139-
assertThat(scheduler).extracting("taskDecorator").isSameAs(taskDecorator);
140-
}
141-
142142
}

0 commit comments

Comments
 (0)