0

Using @Async annotation I want to call a method in a different thread that has access to Session and Request scoped classes.

However when the ApplicationContext tries to get the bean the following exception is generated:

Caused by: org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'scopedTarget.sessionInfoClass': Scope 'session' is not active for the current thread;

I had tried extending ApplicationContextAware class to hold the main thread context.

Also I had tried the suggested solution from this question How to enable request scope in async task executor

Source coude is in Github

https://github.com/saavedrah/spring-threadSample

saavedrah
  • 183
  • 14

1 Answers1

1

I have created a pull request for your repo that solves the issue.

Basically, I extended this solution also for Runnable case.

To verify it, run the ThreadSampleApplication class then hit http://localhost:8080/testAsync

  • Thank you Eugen, I checked using the Url and it worked, I will mark this as answered. Additionally do you know how to fix the RunnableServiceTest ? – saavedrah Oct 03 '19 at 10:13
  • 1
    You need to use an `WebClient` to hit the same URL. For a very straightforward example see https://dzone.com/articles/spring-boot-webclient-and-its-testing –  Oct 03 '19 at 10:31