Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,11 @@ public class TaskExecutionProperties {
*/
private Mode mode = Mode.AUTO;

/**
* Whether to propagate the current context to task executions.
*/
private boolean propagateContext = false;

/**
* Prefix to use for the names of newly created threads.
*/
Expand All @@ -69,6 +74,14 @@ public void setMode(Mode mode) {
this.mode = mode;
}

public boolean getPropagateContext() {
return this.propagateContext;
}

public void setPropagateContext(boolean propagateContext) {
this.propagateContext = propagateContext;
}

public String getThreadNamePrefix() {
return this.threadNamePrefix;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -86,6 +86,11 @@ ThreadPoolTaskExecutor applicationTaskExecutor(ThreadPoolTaskExecutorBuilder thr
return threadPoolTaskExecutorBuilder.build();
}

@Bean
@ConditionalOnProperty(prefix = "spring.task.execution.propagate-context", havingValue = "true")
TaskDecorator contextPropagatingTaskDecorator() {
return new ContextPropagatingTaskDecorator();
}
}

@Configuration(proxyBeanMethods = false)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -92,13 +92,15 @@ void simpleAsyncTaskExecutorBuilderShouldReadProperties() {
"spring.task.execution.simple.reject-tasks-when-limit-reached=true",
"spring.task.execution.simple.concurrency-limit=1",
"spring.task.execution.shutdown.await-termination=true",
"spring.task.execution.shutdown.await-termination-period=30s")
"spring.task.execution.shutdown.await-termination-period=30s",
"spring.task.execution.propagate-context=true")
.run(assertSimpleAsyncTaskExecutor((taskExecutor) -> {
assertThat(taskExecutor).hasFieldOrPropertyWithValue("cancelRemainingTasksOnClose", true);
assertThat(taskExecutor).hasFieldOrPropertyWithValue("rejectTasksWhenLimitReached", true);
assertThat(taskExecutor.getConcurrencyLimit()).isEqualTo(1);
assertThat(taskExecutor.getThreadNamePrefix()).isEqualTo("mytest-");
assertThat(taskExecutor).hasFieldOrPropertyWithValue("taskTerminationTimeout", 30000L);
assertThat(taskExecutor).hasFieldOrPropertyWithValue("propagateContext", true);
}));
}

Expand Down Expand Up @@ -253,6 +255,16 @@ void simpleAsyncTaskExecutorBuilderUsesVirtualThreadsWhenEnabled() {
});
}

@Test
void asyncTaskExecutorShouldApplyContextPropagatingDecoratorWhenEnabled() {
this.contextRunner.withPropertyValues("spring.task.execution.propagate-context=true")
.run((context) -> {
assertThat(context).hasSingleBean(ContextPropagatingTaskDecorator.class);
assertThat(context.getBean(ContextPropagatingTaskDecorator.class))
.isSameAs(context.getBean("contextPropagatingTaskDecorator"));
});
}

@Test
void taskExecutorWhenHasCustomTaskExecutorShouldBackOff() {
this.contextRunner.withBean("customTaskExecutor", Executor.class, SyncTaskExecutor::new).run((context) -> {
Expand Down