gpt4 book ai didi

java - 使用 IExecutorService 异步重试

转载 作者:太空宇宙 更新时间:2023-11-04 11:35:15 32 4
gpt4 key购买 nike

我正在尝试找出一种使用 Hazelcast IExecutorService 实现异步重试机制而无需递归调用的方法:

递归解决方案如下所示:

Callable task = ...

private sendToExecutor(){

Future future = submitToExecutorService(task);

((ICompletableFuture<ActionReply>) future).andThen(callback);
}

回调是一个 ExecutionCallback:

@Override
public void onResponse(ActionReply response) {
// normal stuff
}

@Override
public void onFailure(Throwable t) {

// re-send if possible
if(numRetries < max_retries){
sendToExecutor();
}
}

我正在努力寻找一个不涉及递归的好解决方案。任何帮助将不胜感激。谢谢!

最佳答案

创建一个实现 Future 的包装类,并实现应捕获 RetryableHazelcastExceptionget 方法。请注意,您需要限制重试次数。如果它超过了该限制,则意味着您的集群存在一些重大问题。

public class RetryableFuture implements Future {
//Implement other methods.

@Override
public Object get() throws InterruptedException, ExecutionException {

try{
//get operation on future object
}catch(ExecutionException e){
if(e.getCause() instanceof RetryableHazelcastException){
//Log some warnings and submit the task back to Executors
}
}catch(Exception e){
//Not all exceptions are retryable
}finally {
//Close any kind of resources.
}
}
}

关于java - 使用 IExecutorService 异步重试,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43374792/

32 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com