0

Spring 4.3 and Java 8 Have configured the spring

@Repository
public class EmployeeRepostiry {

 @Async
 public CompleteableFuture<StatusVO> getEmployee(String id) {
  StatusVo status = new StatusVO();
   return CompletableFuture.completedFuture(status);
 }
}

CallingService cclass:

@Service
public class EmployeeService {

 @Autowired public EmployeeRepostiry employeeReporsitory;

 public List<StatusVO> getEmployee(List<String> ids) {
  List<StatusVo> returnStatus = new ArrayList<StatusVO>();
   List<CompleteableFuture<StatusVO>> statsuss = new ArrayList<CompleteableFuture<StatusVO>>();
   for(String id: ids) {
    statsuss.add(employeeReporsitory.getEmployee(id));
   }
   try {
       CompletableFuture.allOf(statsuss.toArray(new CompletableFuture[statsuss.size()])).join();
       } catch(Exception e){
          e.printstacktracce();
          }
   Logger.debug("not coming to this line");    //Not coming here

returnStatus = statsuss.stream().map(completableStatus -> {
        ServiceActionStatusVO statusVO = null;
        try {
            statusVO = completableStatus.get();
        } catch (InterruptedException | ExecutionException e) {
            statusVO = new StatusVO();
            statusVO.setStatus(Constants.ERROR);
            statusVO.setStatusText(e.getMessage());
            LOGGER.error("Failed on the state of execution ",e);
        }
        return statusVO;

    }).collect(Collectors.toList());
   return returnStatus ;
 }
}

The async is getting executed successfully and also running in an async thread and all the ids are getting completed. but the statement is not coming after join(); it hangs after join and all the async tasks get created, I tried to catch a generic exception and nothing returned

Saheb
  • 1,392
  • 3
  • 13
  • 33
Rajar R
  • 127
  • 1
  • 1
  • 10
  • The part with `allOf(…).join()` is unnecessary since you call `get()` on each individual future just after, so maybe try without it? – Didier L Mar 30 '18 at 15:22
  • Referred as per below https://spring.io/guides/gs/async-method/. Will try removing allof &join – Rajar R Mar 31 '18 at 04:46
  • Still there is some time out happening It dies not reaches the statement after all Async task completion is it because the calling thread time out. I tried catching general exeception in calling block, but it dies not reaches there – Rajar R Apr 09 '18 at 16:06

0 Answers0