【问题标题】:RetryExecutor : How to wait for all tasks to finish?RetryExecutor:如何等待所有任务完成?
【发布时间】:2017-03-11 15:29:57
【问题描述】:

我正在使用来自 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 提交了一些任务。

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()那么简单;

【问题讨论】:

    标签: java multithreading scheduledexecutorservice


    【解决方案1】:

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

    【讨论】:

    猜你喜欢
    • 2011-03-17
    • 2017-04-14
    • 1970-01-01
    • 2011-09-03
    • 1970-01-01
    • 1970-01-01
    • 2021-06-12
    • 2023-04-04
    • 1970-01-01
    相关资源
    最近更新 更多