0

I have a scenario in my springboot application, where I submit tasks into a threadpool for async execution.Now some of the methods inside child execution is part of aspect point advice with @AfterReturn. I observe that even if processing is done asnyc, my main thread keeps executing the point cut advice from child thread and my service does not return a value until, all child thread finished execution. Any pointer how to make the advice run on the executing thread itself? So in short, controller method does not return response until dao method execution and its corresponding point cut is executed.

@Controller
@RequestMapping(value = "/api")
public class SampleController  {

@Autowired
SampleService service;
    @RequestMapping(value = "/action", method = RequestMethod.POST, produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
    @ResponseBody
    public String action(@RequestBody String request){
     service.action(request);
     return "Success";
    }

}



@Service
public class SampleService{
@Autowired
SampleDao dao;

@Async("threadPoolExecutor")
public void action(String request){
 dao.action(request);
}

}


@Repository
public class SampleDao{
 public void action(String request){
 //do some db things
 }

}


@Aspect
@Component
public class SampleAspect{
@AfterReturning(
            pointcut = "execution( * com.sample.*.*.SampleDao.action(..))",
            returning = "result")
    public void audit(JoinPoint joinPoint, Object result)  {
       //dosome thing
    }



}
  • Please clarify why would you like to return a value from an aspect method? – mtrycz Mar 06 '20 at 09:03
  • Hi...I was talking about my main http thread returning the response.Its like this=> Controller method a=>. @Async method b()=> method c()....so on.Now method c() is part of afterReturn pointcut.Method a does not send response until method c() is finished.Basically my main thread is blocked due to aspect.I want the aspect to be run in the thread method c() is running or someway to send the response back to client as soon as the task is submitted to executorpool. – Arghya Pattanayak Mar 06 '20 at 09:11
  • Please post a minimal code sample. It's very hard for me to be sure I correctly understand what you're doing. – daniu Mar 06 '20 at 09:13
  • I have added a sample code.My controller method does not return response until dao method execution is complete in child thread and its advice is executed in main thread.So i am not benefitting for async execution in terms of response time of my api. – Arghya Pattanayak Mar 06 '20 at 09:28
  • The only reason I could think of is that you missed `@EnableAsync` annotation. – R.G Mar 06 '20 at 15:07
  • Its on a Config file that I did not give here.Task is executing on child thread thats not a problem. – Arghya Pattanayak Mar 06 '20 at 18:02

1 Answers1

0

The @Async on the service method does not mean that it will be submitted to the executor service and then return immediately, but that you can can have several incoming calls to the endpoint which will then be handled concurrently (this is the case per default anyway afaik, @Async is pretty much a marker only).

You can read this guide to see how this can be done properly.

The gist is that your service needs to create (and optionally return) some sort of Future (in the case of the post, CompletableFuture, as in

@Async
void serviceMethod(String request) {
    CompletableFuture.submit(() -> dao.action(request));
}

It sounds like you do want to wait for the result to arrive though, so while this will work, I expect you'll run into problems later.

daniu
  • 14,137
  • 4
  • 32
  • 53
  • Thanks for the explanation!But I have debugged and found if I disable aspect, I get response immediately and my main thread does not wait for child thread execution to be complete.I need a success response as soon as task is submitted.So my main problem is not async.Rather aspect is blocking request completion. – Arghya Pattanayak Mar 06 '20 at 09:45