java - 使用 IExecutorService 异步重试

标签 java asynchronous hazelcast

我正在尝试找出一种使用 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/

相关文章:

javascript - 如何在调用异步函数后在 javascript/jquery 代码中创建暂停

java - 异步Java : How to return this without blocking?

java - 升级到 Spring 5.0.0.RELEASE - 无法实例化 hazelcast

java - Hazelcast:分布式执行和本地成员信息

java - ConcurrentHashMap中的并发修改

java - 什么时候需要担心线程安全?

java - OpenGL + LWJGL - glColor4d() 没有正确着色形状

java - 使用 Spring Boot 1.3.3-RELEASE 启用 CORS

django - 如何将 Django post_save 信号作为后台进程运行?

java - 请求 hazelcast jcache 提供程序时 HazelcastClientCachingProvider 类未找到异常