Skip to content

Commit 9225848

Browse files
committed
Add support for context propagation in task execution
Signed-off-by: Sandipan <bsandipan99@gmail.com>
1 parent c585b05 commit 9225848

File tree

2 files changed

+39
-4
lines changed

2 files changed

+39
-4
lines changed

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

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,11 @@ public class TaskExecutionProperties {
4444
*/
4545
private Mode mode = Mode.AUTO;
4646

47+
/**
48+
* Indicates whether to register ContextPropagatingTaskDecorator bean
49+
*/
50+
private boolean propagateContext = false;
51+
4752
/**
4853
* Prefix to use for the names of newly created threads.
4954
*/
@@ -69,6 +74,14 @@ public void setMode(Mode mode) {
6974
this.mode = mode;
7075
}
7176

77+
public boolean getPropagateContext() {
78+
return this.propagateContext;
79+
}
80+
81+
public void setPropagateContext(boolean propagateContext) {
82+
this.propagateContext = propagateContext;
83+
}
84+
7285
public String getThreadNamePrefix() {
7386
return this.threadNamePrefix;
7487
}

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

Lines changed: 26 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -75,17 +75,39 @@ static class TaskExecutorConfiguration {
7575

7676
@Bean(TaskExecutionAutoConfiguration.APPLICATION_TASK_EXECUTOR_BEAN_NAME)
7777
@ConditionalOnThreading(Threading.VIRTUAL)
78-
SimpleAsyncTaskExecutor applicationTaskExecutorVirtualThreads(SimpleAsyncTaskExecutorBuilder builder) {
79-
return builder.build();
78+
SimpleAsyncTaskExecutor applicationTaskExecutorVirtualThreads(SimpleAsyncTaskExecutorBuilder builder,
79+
ObjectProvider<TaskDecorator> decorators) {
80+
TaskDecorator decorator = decorators.getIfAvailable();
81+
SimpleAsyncTaskExecutor executor = builder.build();
82+
if (decorator != null) {
83+
executor.setTaskDecorator(decorator);
84+
}
85+
return executor;
8086
}
8187

8288
@Bean(TaskExecutionAutoConfiguration.APPLICATION_TASK_EXECUTOR_BEAN_NAME)
8389
@Lazy
8490
@ConditionalOnThreading(Threading.PLATFORM)
85-
ThreadPoolTaskExecutor applicationTaskExecutor(ThreadPoolTaskExecutorBuilder threadPoolTaskExecutorBuilder) {
86-
return threadPoolTaskExecutorBuilder.build();
91+
ThreadPoolTaskExecutor applicationTaskExecutor( ThreadPoolTaskExecutorBuilder threadPoolTaskExecutorBuilder,
92+
ObjectProvider<TaskDecorator> decorators ) {
93+
TaskDecorator decorator = decorators.getIfAvailable();
94+
ThreadPoolTaskExecutor executor = builder.build();
95+
if (decorator != null) {
96+
executor.setTaskDecorator(decorator);
97+
}
98+
return executor;
8799
}
88100

101+
@Bean
102+
@ConditionalOnProperty(
103+
prefix = "spring.task.execution",
104+
name = "propagate-context",
105+
havingValue = "true",
106+
matchIfMissing = false
107+
)
108+
TaskDecorator contextPropagatingTaskDecorator() {
109+
return new ContextPropagatingTaskDecorator();
110+
}
89111
}
90112

91113
@Configuration(proxyBeanMethods = false)

0 commit comments

Comments
 (0)