小编Rah*_*and的帖子

当函数在 y 时间间隔内抛出异常时重试函数调用 n 次

我想在一段时间内失败时重试我的函数调用。这样做的最佳方法是什么。这会正常工作吗。

CompletableFuture.runAsync(() -> {
    for (int i = 0; i < 3; i++) {
        try {
            dndService.initateDNDRequest(transactionId, circle, category, "PREPAID");       
            break;
        } catch (Exception e) {
            try {
                TimeUnit.SECONDS.sleep(10);//wait for few minutes while next attempt
            } catch (InterruptedException e1) {
                LOGGER.error("Error while retrying request for DND.");
            }
            LOGGER.error("Request retry for DND count"+i);
        }
    }
}, executor);
Run Code Online (Sandbox Code Playgroud)

java schedule scheduled-tasks java-8 retry-logic

2
推荐指数
1
解决办法
1500
查看次数

标签 统计

java ×1

java-8 ×1

retry-logic ×1

schedule ×1

scheduled-tasks ×1