Here is my code to work with @Async with CompletableFuture in Spring Boot:
@RestController
@EnableAsync
public class DemoController {
@Autowired
UtilService service;
@GetMapping("/test/{count}")
public void test(@PathVariable int count) throws InterruptedException, ExecutionException {
List<CompletableFuture<String>> list = new ArrayList<>();
long start = System.currentTimeMillis();
for(int i=0; i< count; i++) {
//CompletableFuture<String> res = calculate(i);
CompletableFuture<String> res = service.calculate(i);
list.add(res);
}
List<String> res = list.stream().map(com -> com.join()).collect(Collectors.toList());
res.forEach(System.out:: println);
System.out.println("Elapsed time: " + (System.currentTimeMillis() - start));
}
@Async
public CompletableFuture<String> calculate(int counter) throws InterruptedException{
Thread.sleep(1000L);
System.out.println("------Util------counter->" + counter);
return CompletableFuture.completedFuture("Async -" + counter);
}
}
@Service
public class UtilService {
@Async
public CompletableFuture<String> calculate(int counter) throws InterruptedException{
Thread.sleep(1000L);
System.out.println("------Util------counter->" + counter);
return CompletableFuture.completedFuture("Async -" + counter);
}
}
When i keep the async method calculate() in the same contrller class, code is not executing in asynchronus way. it is running as a synchronus method. But when i move the async method calculate() to a different component. It is working fine.
I didnt understand why there is behavour change. if both methods (calling, caller) are in same class, code is running synchronusly. if both methods (calling, caller) are in different classes, code is running in asynchronusly.
Could you any one explain me is there any reason behind.