mirror of
https://github.com/spring-projects/spring-boot.git
synced 2024-09-03 04:26:12 +08:00
Alias auto-configured TaskExecutor
to make async tasks use it
See gh-15748
This commit is contained in:
parent
9e0b1c297d
commit
d0859f1569
@ -36,6 +36,7 @@ import org.springframework.scheduling.concurrent.ThreadPoolTaskExecutor;
|
|||||||
* {@link EnableAutoConfiguration Auto-configuration} for {@link TaskExecutor}.
|
* {@link EnableAutoConfiguration Auto-configuration} for {@link TaskExecutor}.
|
||||||
*
|
*
|
||||||
* @author Stephane Nicoll
|
* @author Stephane Nicoll
|
||||||
|
* @author Camille Vienot
|
||||||
* @since 2.1.0
|
* @since 2.1.0
|
||||||
*/
|
*/
|
||||||
@ConditionalOnClass(ThreadPoolTaskExecutor.class)
|
@ConditionalOnClass(ThreadPoolTaskExecutor.class)
|
||||||
@ -48,6 +49,8 @@ public class TaskExecutionAutoConfiguration {
|
|||||||
*/
|
*/
|
||||||
public static final String APPLICATION_TASK_EXECUTOR_BEAN_NAME = "applicationTaskExecutor";
|
public static final String APPLICATION_TASK_EXECUTOR_BEAN_NAME = "applicationTaskExecutor";
|
||||||
|
|
||||||
|
public static final String ASYNC_TASK_EXECUTOR_BEAN_NAME = "taskExecutor";
|
||||||
|
|
||||||
private final TaskExecutionProperties properties;
|
private final TaskExecutionProperties properties;
|
||||||
|
|
||||||
private final ObjectProvider<TaskExecutorCustomizer> taskExecutorCustomizers;
|
private final ObjectProvider<TaskExecutorCustomizer> taskExecutorCustomizers;
|
||||||
@ -79,7 +82,7 @@ public class TaskExecutionAutoConfiguration {
|
|||||||
}
|
}
|
||||||
|
|
||||||
@Lazy
|
@Lazy
|
||||||
@Bean(name = APPLICATION_TASK_EXECUTOR_BEAN_NAME)
|
@Bean(name = { APPLICATION_TASK_EXECUTOR_BEAN_NAME, ASYNC_TASK_EXECUTOR_BEAN_NAME })
|
||||||
@ConditionalOnMissingBean(Executor.class)
|
@ConditionalOnMissingBean(Executor.class)
|
||||||
public ThreadPoolTaskExecutor applicationTaskExecutor(TaskExecutorBuilder builder) {
|
public ThreadPoolTaskExecutor applicationTaskExecutor(TaskExecutorBuilder builder) {
|
||||||
return builder.build();
|
return builder.build();
|
||||||
|
@ -38,6 +38,7 @@ import org.springframework.core.task.TaskExecutor;
|
|||||||
import org.springframework.scheduling.annotation.Async;
|
import org.springframework.scheduling.annotation.Async;
|
||||||
import org.springframework.scheduling.annotation.AsyncResult;
|
import org.springframework.scheduling.annotation.AsyncResult;
|
||||||
import org.springframework.scheduling.annotation.EnableAsync;
|
import org.springframework.scheduling.annotation.EnableAsync;
|
||||||
|
import org.springframework.scheduling.annotation.EnableScheduling;
|
||||||
import org.springframework.scheduling.concurrent.ThreadPoolTaskExecutor;
|
import org.springframework.scheduling.concurrent.ThreadPoolTaskExecutor;
|
||||||
import org.springframework.test.util.ReflectionTestUtils;
|
import org.springframework.test.util.ReflectionTestUtils;
|
||||||
|
|
||||||
@ -49,6 +50,7 @@ import static org.mockito.Mockito.verify;
|
|||||||
* Tests for {@link TaskExecutionAutoConfiguration}.
|
* Tests for {@link TaskExecutionAutoConfiguration}.
|
||||||
*
|
*
|
||||||
* @author Stephane Nicoll
|
* @author Stephane Nicoll
|
||||||
|
* @author Camille Vienot
|
||||||
*/
|
*/
|
||||||
public class TaskExecutionAutoConfigurationTests {
|
public class TaskExecutionAutoConfigurationTests {
|
||||||
|
|
||||||
@ -152,6 +154,21 @@ public class TaskExecutionAutoConfigurationTests {
|
|||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void enableAsyncUsesAutoConfiguredOneByDefaultEvenThoughSchedulingIsConfigured() {
|
||||||
|
this.contextRunner
|
||||||
|
.withPropertyValues("spring.task.execution.thread-name-prefix=task-test-")
|
||||||
|
.withConfiguration(
|
||||||
|
AutoConfigurations.of(TaskSchedulingAutoConfiguration.class))
|
||||||
|
.withUserConfiguration(AsyncConfiguration.class,
|
||||||
|
SchedulingConfiguration.class, TestBean.class)
|
||||||
|
.run((context) -> {
|
||||||
|
TestBean bean = context.getBean(TestBean.class);
|
||||||
|
String text = bean.echo("something").get();
|
||||||
|
assertThat(text).contains("task-test-").contains("something");
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
||||||
private ContextConsumer<AssertableApplicationContext> assertTaskExecutor(
|
private ContextConsumer<AssertableApplicationContext> assertTaskExecutor(
|
||||||
Consumer<ThreadPoolTaskExecutor> taskExecutor) {
|
Consumer<ThreadPoolTaskExecutor> taskExecutor) {
|
||||||
return (context) -> {
|
return (context) -> {
|
||||||
@ -209,6 +226,12 @@ public class TaskExecutionAutoConfigurationTests {
|
|||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@Configuration
|
||||||
|
@EnableScheduling
|
||||||
|
static class SchedulingConfiguration {
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
static class TestBean {
|
static class TestBean {
|
||||||
|
|
||||||
@Async
|
@Async
|
||||||
|
Loading…
Reference in New Issue
Block a user