Skip to content

Commit e61ab7e

Browse files
committed
Polish "Add support for multiple TaskDecorator beans"
See gh-45302
1 parent fff816d commit e61ab7e

File tree

3 files changed

+69
-20
lines changed

3 files changed

+69
-20
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
/*
2+
* Copyright 2012-present the original author or authors.
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* https://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
17+
package org.springframework.boot.autoconfigure.task;
18+
19+
import org.springframework.core.Ordered;
20+
import org.springframework.core.task.TaskDecorator;
21+
22+
/**
23+
* {@link TaskDecorator} that is {@link Ordered ordered}.
24+
*
25+
* @author Andy Wilkinson
26+
*/
27+
class OrderedTaskDecorator implements TaskDecorator, Ordered {
28+
29+
private final int order;
30+
31+
OrderedTaskDecorator() {
32+
this(0);
33+
}
34+
35+
OrderedTaskDecorator(int order) {
36+
this.order = order;
37+
}
38+
39+
@Override
40+
public int getOrder() {
41+
return this.order;
42+
}
43+
44+
@Override
45+
public Runnable decorate(Runnable runnable) {
46+
return runnable;
47+
}
48+
49+
}

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

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -128,7 +128,7 @@ void threadPoolTaskExecutorBuilderWhenHasCustomBuilderShouldUseCustomBuilder() {
128128

129129
@Test
130130
void threadPoolTaskExecutorBuilderShouldUseTaskDecorator() {
131-
this.contextRunner.withBean(TaskDecorator.class, this::createTaskDecorator).run((context) -> {
131+
this.contextRunner.withBean(TaskDecorator.class, OrderedTaskDecorator::new).run((context) -> {
132132
assertThat(context).hasSingleBean(ThreadPoolTaskExecutorBuilder.class);
133133
ThreadPoolTaskExecutor executor = context.getBean(ThreadPoolTaskExecutorBuilder.class).build();
134134
assertThat(executor).extracting("taskDecorator").isSameAs(context.getBean(TaskDecorator.class));
@@ -137,8 +137,9 @@ void threadPoolTaskExecutorBuilderShouldUseTaskDecorator() {
137137

138138
@Test
139139
void threadPoolTaskExecutorBuilderShouldUseCompositeTaskDecorator() {
140-
this.contextRunner.withBean("taskDecorator1", TaskDecorator.class, this::createTaskDecorator)
141-
.withBean("taskDecorator2", TaskDecorator.class, this::createTaskDecorator)
140+
this.contextRunner.withBean("taskDecorator1", TaskDecorator.class, () -> new OrderedTaskDecorator(1))
141+
.withBean("taskDecorator2", TaskDecorator.class, () -> new OrderedTaskDecorator(3))
142+
.withBean("taskDecorator3", TaskDecorator.class, () -> new OrderedTaskDecorator(2))
142143
.run((context) -> {
143144
assertThat(context).hasSingleBean(ThreadPoolTaskExecutorBuilder.class);
144145
ThreadPoolTaskExecutor executor = context.getBean(ThreadPoolTaskExecutorBuilder.class).build();
@@ -147,6 +148,7 @@ void threadPoolTaskExecutorBuilderShouldUseCompositeTaskDecorator() {
147148
.extracting("taskDecorators")
148149
.asInstanceOf(InstanceOfAssertFactories.list(TaskDecorator.class))
149150
.containsExactly(context.getBean("taskDecorator1", TaskDecorator.class),
151+
context.getBean("taskDecorator3", TaskDecorator.class),
150152
context.getBean("taskDecorator2", TaskDecorator.class));
151153
});
152154
}
@@ -201,7 +203,7 @@ void whenVirtualThreadsAreAvailableButNotEnabledThenThreadPoolTaskExecutorIsAuto
201203
@EnabledForJreRange(min = JRE.JAVA_21)
202204
void whenTaskDecoratorIsDefinedThenSimpleAsyncTaskExecutorWithVirtualThreadsUsesIt() {
203205
this.contextRunner.withPropertyValues("spring.threads.virtual.enabled=true")
204-
.withBean(TaskDecorator.class, this::createTaskDecorator)
206+
.withBean(TaskDecorator.class, OrderedTaskDecorator::new)
205207
.run((context) -> {
206208
SimpleAsyncTaskExecutor executor = context.getBean(SimpleAsyncTaskExecutor.class);
207209
assertThat(executor).extracting("taskDecorator").isSameAs(context.getBean(TaskDecorator.class));
@@ -212,15 +214,17 @@ void whenTaskDecoratorIsDefinedThenSimpleAsyncTaskExecutorWithVirtualThreadsUses
212214
@EnabledForJreRange(min = JRE.JAVA_21)
213215
void whenTaskDecoratorsAreDefinedThenSimpleAsyncTaskExecutorWithVirtualThreadsUsesThem() {
214216
this.contextRunner.withPropertyValues("spring.threads.virtual.enabled=true")
215-
.withBean("taskDecorator1", TaskDecorator.class, this::createTaskDecorator)
216-
.withBean("taskDecorator2", TaskDecorator.class, this::createTaskDecorator)
217+
.withBean("taskDecorator1", TaskDecorator.class, () -> new OrderedTaskDecorator(1))
218+
.withBean("taskDecorator2", TaskDecorator.class, () -> new OrderedTaskDecorator(3))
219+
.withBean("taskDecorator3", TaskDecorator.class, () -> new OrderedTaskDecorator(2))
217220
.run((context) -> {
218221
SimpleAsyncTaskExecutor executor = context.getBean(SimpleAsyncTaskExecutor.class);
219222
assertThat(executor).extracting("taskDecorator")
220223
.isInstanceOf(CompositeTaskDecorator.class)
221224
.extracting("taskDecorators")
222225
.asInstanceOf(InstanceOfAssertFactories.list(TaskDecorator.class))
223226
.containsExactly(context.getBean("taskDecorator1", TaskDecorator.class),
227+
context.getBean("taskDecorator3", TaskDecorator.class),
224228
context.getBean("taskDecorator2", TaskDecorator.class));
225229
});
226230
}
@@ -485,10 +489,6 @@ void shouldNotAliasApplicationTaskExecutorWhenBootstrapExecutorAliasIsDefined()
485489
});
486490
}
487491

488-
private TaskDecorator createTaskDecorator() {
489-
return (runnable) -> runnable;
490-
}
491-
492492
private Executor createCustomAsyncExecutor(String threadNamePrefix) {
493493
SimpleAsyncTaskExecutor executor = new SimpleAsyncTaskExecutor();
494494
executor.setThreadNamePrefix(threadNamePrefix);

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

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -145,7 +145,7 @@ void simpleAsyncTaskSchedulerBuilderShouldUsePlatformThreadsByDefault() {
145145
@Test
146146
void simpleAsyncTaskSchedulerBuilderShouldApplyTaskDecorator() {
147147
this.contextRunner.withUserConfiguration(SchedulingConfiguration.class)
148-
.withBean(TaskDecorator.class, this::createTaskDecorator)
148+
.withBean(TaskDecorator.class, OrderedTaskDecorator::new)
149149
.run((context) -> {
150150
assertThat(context).hasSingleBean(SimpleAsyncTaskSchedulerBuilder.class);
151151
assertThat(context).hasSingleBean(TaskDecorator.class);
@@ -158,8 +158,9 @@ void simpleAsyncTaskSchedulerBuilderShouldApplyTaskDecorator() {
158158
@Test
159159
void simpleAsyncTaskSchedulerBuilderShouldApplyCompositeTaskDecorator() {
160160
this.contextRunner.withUserConfiguration(SchedulingConfiguration.class)
161-
.withBean("taskDecorator1", TaskDecorator.class, this::createTaskDecorator)
162-
.withBean("taskDecorator2", TaskDecorator.class, this::createTaskDecorator)
161+
.withBean("taskDecorator1", TaskDecorator.class, () -> new OrderedTaskDecorator(1))
162+
.withBean("taskDecorator2", TaskDecorator.class, () -> new OrderedTaskDecorator(3))
163+
.withBean("taskDecorator3", TaskDecorator.class, () -> new OrderedTaskDecorator(2))
163164
.run((context) -> {
164165
assertThat(context).hasSingleBean(SimpleAsyncTaskSchedulerBuilder.class);
165166
SimpleAsyncTaskScheduler scheduler = context.getBean(SimpleAsyncTaskSchedulerBuilder.class).build();
@@ -168,14 +169,15 @@ void simpleAsyncTaskSchedulerBuilderShouldApplyCompositeTaskDecorator() {
168169
.extracting("taskDecorators")
169170
.asInstanceOf(InstanceOfAssertFactories.list(TaskDecorator.class))
170171
.containsExactly(context.getBean("taskDecorator1", TaskDecorator.class),
172+
context.getBean("taskDecorator3", TaskDecorator.class),
171173
context.getBean("taskDecorator2", TaskDecorator.class));
172174
});
173175
}
174176

175177
@Test
176178
void threadPoolTaskSchedulerBuilderShouldApplyTaskDecorator() {
177179
this.contextRunner.withUserConfiguration(SchedulingConfiguration.class)
178-
.withBean(TaskDecorator.class, this::createTaskDecorator)
180+
.withBean(TaskDecorator.class, OrderedTaskDecorator::new)
179181
.run((context) -> {
180182
assertThat(context).hasSingleBean(ThreadPoolTaskSchedulerBuilder.class);
181183
assertThat(context).hasSingleBean(TaskDecorator.class);
@@ -188,8 +190,9 @@ void threadPoolTaskSchedulerBuilderShouldApplyTaskDecorator() {
188190
@Test
189191
void threadPoolTaskSchedulerBuilderShouldApplyCompositeTaskDecorator() {
190192
this.contextRunner.withUserConfiguration(SchedulingConfiguration.class)
191-
.withBean("taskDecorator1", TaskDecorator.class, this::createTaskDecorator)
192-
.withBean("taskDecorator2", TaskDecorator.class, this::createTaskDecorator)
193+
.withBean("taskDecorator1", TaskDecorator.class, () -> new OrderedTaskDecorator(1))
194+
.withBean("taskDecorator2", TaskDecorator.class, () -> new OrderedTaskDecorator(3))
195+
.withBean("taskDecorator3", TaskDecorator.class, () -> new OrderedTaskDecorator(2))
193196
.run((context) -> {
194197
assertThat(context).hasSingleBean(ThreadPoolTaskSchedulerBuilder.class);
195198
ThreadPoolTaskScheduler scheduler = context.getBean(ThreadPoolTaskSchedulerBuilder.class).build();
@@ -198,6 +201,7 @@ void threadPoolTaskSchedulerBuilderShouldApplyCompositeTaskDecorator() {
198201
.extracting("taskDecorators")
199202
.asInstanceOf(InstanceOfAssertFactories.list(TaskDecorator.class))
200203
.containsExactly(context.getBean("taskDecorator1", TaskDecorator.class),
204+
context.getBean("taskDecorator3", TaskDecorator.class),
201205
context.getBean("taskDecorator2", TaskDecorator.class));
202206
});
203207
}
@@ -271,10 +275,6 @@ void enableSchedulingWithLazyInitializationInvokeScheduledMethods() {
271275
});
272276
}
273277

274-
private TaskDecorator createTaskDecorator() {
275-
return (runnable) -> runnable;
276-
}
277-
278278
@Configuration(proxyBeanMethods = false)
279279
@EnableScheduling
280280
static class SchedulingConfiguration {

0 commit comments

Comments
 (0)