我正在尝试找出一种使用 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
的包装类,并实现应捕获 RetryableHazelcastException
的 get
方法。请注意,您需要限制重试次数。如果它超过了该限制,则意味着您的集群存在一些重大问题。
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/