2017-03-11 41 views
0

我使用RetryExecutor来自:https://github.com/nurkiewicz/async-retryRetryExecutor:如何等待所有任务完成?

下面ID我的代码:

ScheduledExecutorService executorService = Executors.newScheduledThreadPool(10); 
RetryExecutor retryExecutor = new AsyncRetryExecutor(executorService) 
      .retryOn(IOException.class) 
      .withExponentialBackoff(500, 2) 
      .withMaxDelay(5_000) // 5 seconds 
      .withUniformJitter() 
      .withMaxRetries(5); 

我已经提交了几个任务retryExecutor。

retryExecutor.getWithRetry(ctx -> { 
       if(ctx.getRetryCount()==0) 
        System.out.println("Starting download from : " + url); 
       else 
        System.out.println("Retrying ("+ctx.getRetryCount()+") dowload from : "+url); 
       return downloadFile(url); 
      } 
     ).whenComplete((result, error) -> { 
      if(result!=null && result){ 
       System.out.println("Successfully downloaded!"); 
      }else{ 
       System.out.println("Download failed. Error : "+error); 
      } 
     }); 

现在,我该如何等待所有提交的任务完成? 我想等到所有重试完成(如果有)。

不认为它会像executorService.shutdown()那样简单;

回答

0

CompletableFuture<DownloadResult> downloadPromise = retryExecutor.getWithRetry(...) .whenComplete(...); DownloadResult downloadResult = downloadPromise.get()

+1

请添加解释你的答案:我如何写一个很好的答案(https://stackoverflow.com/help/how-to-answer) –