Collectives™ on Stack Overflow

Find centralized, trusted content and collaborate around the technologies you use most.

Learn more about Collectives

Teams

Q&A for work

Connect and share knowledge within a single location that is structured and easy to search.

Learn more about Teams

I need to make an asynchronous call to a service which is injected by Spring using the autowire annotation. I'm doing something like this

@Component
Public class nameListener implements ApplicationListener<ContextRefreshEvent>{
@Autowire
protected ServiceName serviceName;
@Override
@Transactional
public onApplicationEvent(ContextRefreshEvent event){
  log(asyncMethod.get().longValue());
@Async
public CompletableFuture<Long> asyncMethod(){
 CompletableFuture<Long> result = CompletableFuture.supplyAsync(()-> serviceName.methodName());
  return result;

serviceBean configuration:

@Configuration
public class serviceClassConfiguration{
@Autowired
protected serviceFactory;
@Bean
public ServiceType serviceName();
   //this only creates the type with the attributes it need
   return serviceFactory.createServiceName();

The Error I'm getting says the following:

org.springframework.beans.factory.BeanCreationException;
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 thread-bound request found;
Are you referring to request attributes outside of an actual web request,
or processing a request outside of the originally receiving thread&;
If you are actually operating within a web request and still receive this
message, your code is probably running outside of DispatcherServlet;
In this case, use RequestContextListener or RequestContextFilter to
expose the current request

If I understand the problem correctly I think that Spring injects a proxy when does dependency injection so the serviceName bean is not available in the scope of the asyncCall, is that correct?

I tried creating a new class only for the asyncMethod using prototype scope so it can be created any time is been call, and added as a bean on the listenerclass but this didnt work also.

Which workarounds could I use to manage this situation?

Best regards

Scope session is not active for the current thread identifies the problem. The cause of this appears to be in the configured scope of your ServiceName class. Consider including configuration details. – DwB May 24, 2018 at 17:06

CompletableFuture.supplyAsync will run your method in a pooled thread.

That thread is completely does not received @Transaction around.

So you have to create and common/rollback your transaction manually

Thanks for contributing an answer to Stack Overflow!

  • Please be sure to answer the question. Provide details and share your research!

But avoid

  • Asking for help, clarification, or responding to other answers.
  • Making statements based on opinion; back them up with references or personal experience.

To learn more, see our tips on writing great answers.