2017-03-11 4 views
0

J'utilise RetryExecutor de: https://github.com/nurkiewicz/async-retryRetryExecutor: Comment attendre que toutes les tâches se terminent?

ci-dessous id mon code:

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

J'ai soumis quelques tâches à 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); 
      } 
     }); 

Maintenant, comment puis-je attendre que toutes les tâches soumises soient terminées? Je veux attendre que toutes les tentatives soient terminées (le cas échéant).

ne pense pas que ce sera aussi simple que executorService.shutdown();

Répondre

0

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

+1

S'il vous plaît ajouter une explication à votre réponse: [? Comment puis-je écrire une bonne réponse] (https://stackoverflow.com/help/how-to-answer) –