I try to use @Async
de spring, And in my service I use a bean with scope session
, always I got the bellow error:
org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'scopedTarget.classSession': Scope 'session' is not active for the current thread; consider defining a scoped proxy for this bean if you intend to refer to it from a singleton; nested exception is java.lang.IllegalStateException: No session found and request already completed - cannot create new session!
As is mentioned, No session found and request already completed. I implemented AsyncConfigurer in order to overide the ThreadPoolTaskExecutor: bellow my code
Controller:
@Autowired
MyService myService;
@RequestMapping(value = "/doIt", method = RequestMethod.PUT)
public HttpEntity initiateCurrent(..){
myService.do();
...
}
MyService
@Autowired
ClassWithScopeSession classSession;
@Async("taskExecutor")
public void do(){
....
...
classSession.doService();
}
//Overide the ThreadPoolTaskExecutor
public class ContextAwareCallable<T> implements Callable<T> {
private Callable<T> task;
private RequestAttributes context;
@Autowired
private ApplicationContext appContext;
public ContextAwareCallable(Callable<T> task, RequestAttributes context) {
this.task = task;
this.context = context;
}
@Override
public T call() throws Exception {
if (context != null) {
RequestContextHolder.setRequestAttributes(context);
}
try {
return task.call();
} finally {
RequestContextHolder.resetRequestAttributes();
}
}
}
public class ContextAwarePoolExecutor extends ThreadPoolTaskExecutor {
@Override
public <T> Future<T> submit(Callable<T> task) {
return super.submit(new ContextAwareCallable(task, RequestContextHolder.currentRequestAttributes()));
}
@Override
public <T> ListenableFuture<T> submitListenable(Callable<T> task) {
return super.submitListenable(new ContextAwareCallable(task, RequestContextHolder.currentRequestAttributes()));
}
}
@Configuration
@EnableAsync
public class ExecutorConfig implements AsyncConfigurer {
@Override
@Bean(name="taskExecutor")
public Executor getAsyncExecutor() {
return new ContextAwarePoolExecutor();
}
@Override
public AsyncUncaughtExceptionHandler getAsyncUncaughtExceptionHandler() {
return null;
}
}
I followed this response