我试图找出一种使用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的包装器类,并实现应捕获getRetryableHazelcastException方法。请注意,您需要限制重试次数。如果超过该限制,则说明集群存在一些重大问题。

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.
        }
    }
}

09-28 02:26
查看更多