每当我的函数调用在一段时间内失败时,我想重试它。 最好的方法是什么? 这可以正常工作吗?
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);
最佳答案
您不应将执行程序的工作线程置于 sleep
状态。
安排新尝试的一个解决方案是
Executor executor; // … the actual executor
ScheduledExecutorService ses = Executors.newSingleThreadScheduledExecutor();
Executor afterTenSeconds
= r -> ses.schedule(() -> executor.execute(r), 10, TimeUnit.SECONDS);
Runnable primaryAction
= () -> dndService.initateDNDRequest(transactionId, circle, category, "PREPAID");
CompletableFuture<Void> cf = CompletableFuture.runAsync(primaryAction, executor);
for(int i = 0; i < 3; i++) {
cf = cf.handle((v,t) -> t == null? CompletableFuture.completedFuture(v):
CompletableFuture.runAsync(primaryAction, afterTenSeconds))
.thenCompose(Function.identity());
}
然后,handle
操作将安排一次新的尝试,在失败情况下超时(十秒)后执行。 thenCompose(Function.identity())
是必要的,因为没有单一方法可以组合 handle
和 compose
语义。
请注意,从 Java 9 开始,您可以像下面这样简单地创建延迟执行器
Executor afterTenSeconds = CompletableFuture.delayedExecutor(10,TimeUnit.SECONDS,executor);
无需自己处理ScheduledExecutorService
。
关于java - 当函数在 y 时间间隔内抛出异常时,重试函数调用 n 次,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50679825/