Class RequestAttributesTaskDecorator
java.lang.Object
com.fayupable.logged.spring.security.RequestAttributesTaskDecorator
- All Implemented Interfaces:
org.springframework.core.task.TaskDecorator
public final class RequestAttributesTaskDecorator
extends Object
implements org.springframework.core.task.TaskDecorator
TaskDecorator that carries the submitting thread's Spring Web
RequestAttributes into whatever thread Spring's @Async
infrastructure actually runs the task on.
The Spring Web/Spring counterpart to
com.fayupable.logged.spring.aspect.FlowContextTaskDecorator: that
class carries this library's own call-chain tracking across an
@Async boundary, this one carries the current HTTP request across
the same boundary. Combine both on the same executor if both are needed:
@Bean
public TaskExecutor taskExecutor() {
ThreadPoolTaskExecutor executor = new ThreadPoolTaskExecutor();
executor.setTaskDecorator(runnable ->
new FlowContextTaskDecorator().decorate(
new RequestAttributesTaskDecorator().decorate(runnable)));
executor.initialize();
return executor;
}
Without this registration, this library's IP-based caller identity
resolution silently falls back to "unknown" for any nested
@Logged call made from within an @Async method dispatched
through the decorated executor.
-
Constructor Summary
Constructors -
Method Summary
Modifier and TypeMethodDescriptionCaptures the calling thread's currentRequestAttributesand returns a wrapped task that restores them before runningrunnableand restores the executor thread's own previous request attributes again afterward, regardless of whetherrunnablecompletes normally or throws.
-
Constructor Details
-
RequestAttributesTaskDecorator
public RequestAttributesTaskDecorator()
-
-
Method Details
-
decorate
Captures the calling thread's currentRequestAttributesand returns a wrapped task that restores them before runningrunnableand restores the executor thread's own previous request attributes again afterward, regardless of whetherrunnablecompletes normally or throws.- Specified by:
decoratein interfaceorg.springframework.core.task.TaskDecorator- Parameters:
runnable- the task Spring's@Asyncinfrastructure is about to hand off to an executor thread- Returns:
- a task carrying the calling thread's request attributes
-