In an application utilizing asynchronous web services, where the server responds with an acknowledgment while commencing asynchronous processing via an AsyncTaskExecutor, it becomes crucial to enable the request scope. This is essential to access bean classes annotated with @Scope(WebApplicationContext.SCOPE_REQUEST).
Typically, in such scenarios, an exception occurs due to the request scope not being active within the SimpleAsyncTaskExecutor, which operates outside of the DispatcherServlet. To resolve this issue, consider the following solution:
Note: This approach only supports Session and Request scoped beans, not security context (e.g., Spring Security). For security context handling, separate measures should be implemented.
Example Implementation:
Executor:
<code class="java">public class ContextAwarePoolExecutor extends ThreadPoolTaskExecutor { @Override public <T> Future<T> submit(Callable<T> task) { return super.submit(new ContextAwareCallable(task, RequestContextHolder.currentRequestAttributes())); } }</code>
Callable:
<code class="java">public class ContextAwareCallable<T> implements Callable<T> { private Callable<T> task; private RequestAttributes context; @Override public T call() throws Exception { if (context != null) { RequestContextHolder.setRequestAttributes(context); } try { return task.call(); } finally { RequestContextHolder.resetRequestAttributes(); } } }</code>
Configuration:
<code class="java">@Configuration public class ExecutorConfig extends AsyncConfigurerSupport { @Override @Bean public Executor getAsyncExecutor() { return new ContextAwarePoolExecutor(); } }</code>
The above is the detailed content of How to Preserve Request Scope in Asynchronous Task Execution?. For more information, please follow other related articles on the PHP Chinese website!