Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Spring Boot: "Scope 'request' is not active for the current thread" in Asynch method

I have next simple request method inside Spring Boot controller:

@Autowired
    private BillServ billServ;
...

@RequestMapping("/chrgcheck")
public String chrgCheck() {
  Future<Result> fut = null;
  fut = billServ.useCheck();

 while (!fut.isDone()) {
       try {
    Thread.sleep(100);
  } catch (InterruptedException e) {
    // TODO Auto-generated catch block
    e.printStackTrace();
  } 
   }
 try {
    if (fut.get().err ==0) {
      return "OK";
    } else {
      return "ERROR";
    }
  } catch (InterruptedException e) {
    // TODO Auto-generated catch block
    e.printStackTrace();
    return "ERROR";
  } catch (ExecutionException e) {
    // TODO Auto-generated catch block
    e.printStackTrace();
    return "ERROR";
}

}

It calls asynch method of service bean billServ.useCheck();

  @Service
  @Scope(value="request", proxyMode=ScopedProxyMode.TARGET_CLASS)
  public class BillServ {
  ...
    @Async
    public Future<Result> useCheck() {
        Result res = new Result();

        System.out.println("Try-1");

        check.checkMe(1);

        res.err=0;
        return new AsyncResult<Result>(res);
    }

Which calls another method of request-scoped bean check.checkMe(), because I need to work with individual bean per request:

@Component
  @Scope(value="request", proxyMode=ScopedProxyMode.TARGET_CLASS)
  public class Check {

    public void checkMe(int i) {
      System.out.println("checkMe:"+i);

    }
  }

So, when I call request "/chrgcheck", I get next error:

  java.util.concurrent.ExecutionException: org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'scopedTarget.check': Scope 'request' 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/DispatcherPortlet: In this case, use RequestContextListener or RequestContextFilter to expose the current request.
    at java.util.concurrent.FutureTask.report(FutureTask.java:122)
    at java.util.concurrent.FutureTask.get(FutureTask.java:192)
    at com.ric.web.BillingController.chrgCheck(BillingController.java:99)
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
    at java.lang.reflect.Method.invoke(Method.java:497)
    at org.springframework.web.method.support.InvocableHandlerMethod.doInvoke(InvocableHandlerMethod.java:221)
    at org.springframework.web.method.support.InvocableHandlerMethod.invokeForRequest(InvocableHandlerMethod.java:136)
    at org.springframework.web.servlet.mvc.method.annotation.ServletInvocableHandlerMethod.invokeAndHandle(ServletInvocableHandlerMethod.java:114)
    at org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerAdapter.invokeHandlerMethod(RequestMappingHandlerAdapter.java:827)
    at org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerAdapter.handleInternal(RequestMappingHandlerAdapter.java:738)
    at org.springframework.web.servlet.mvc.method.AbstractHandlerMethodAdapter.handle(AbstractHandlerMethodAdapter.java:85)
    at org.springframework.web.servlet.DispatcherServlet.doDispatch(DispatcherServlet.java:963)
    at org.springframework.web.servlet.DispatcherServlet.doService(DispatcherServlet.java:897)
    at org.springframework.web.servlet.FrameworkServlet.processRequest(FrameworkServlet.java:970)
    at org.springframework.web.servlet.FrameworkServlet.doGet(FrameworkServlet.java:861)
    at javax.servlet.http.HttpServlet.service(HttpServlet.java:622)
    at org.springframework.web.servlet.FrameworkServlet.service(FrameworkServlet.java:846)
    at javax.servlet.http.HttpServlet.service(HttpServlet.java:729)
    at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:230)
    at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:165)
    at org.apache.tomcat.websocket.server.WsFilter.doFilter(WsFilter.java:52)

Why does it happen? What I should correct in my project?

like image 447
Lev Avatar asked Sep 05 '16 06:09

Lev


1 Answers

Springs Request Scope implementation is thread bound. So if you start an new thread (that is what @Async does) then this thread is not bound to the request and therefore the spring request scope implementations fails.

like image 58
Ralph Avatar answered Nov 14 '22 22:11

Ralph