RetryExecutor:如何等待所有任务完成?

时间:2017-03-11 15:29:58

标签: java multithreading scheduledexecutorservice

我正在使用来自https://github.com/nurkiewicz/async-retry

的RetryExecutor

下面是我的代码:

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.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();

一样简单

1 个答案:

答案 0 :(得分:0)

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

相关问题