First of all I went through all the posts(such as [https://stackoverflow.com/questions/21286675/scope-session-is-not-active-for-the-current-thread-illegalstateexception-no][1] ) related to above topic in Stack overflow and couldn't find a solution yet.
In my case, I have an OAuth2RestTemplate
with request scope OAuth2ClientContext
and when I use the oauth2 rest template in a new thread (an asyn call) I get this error. I don't need that to be bound to a request, all I want is to send a request to another micro service and that request will be sent by a scheduler.
@Component
@Scope(value = "session", proxyMode = ScopedProxyMode.TARGET_CLASS)
Class Scheduler{
@Qualifier("clientRestTemplate")
@Autowired
private OAuth2RestTemplate clientRestTemplate;
public void runTask(TaskScheduleDto schedule) {
userRestTemplate.exchange(builder.build().encode().toUri(), HttpMethod.POST,
new HttpEntity<>(""), Void.class);
}
}