2017-03-11 74 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) –