Guava-Retrying 请求重试机制

使用Guava-Retrying,请求重试

public static Object callMethod(String url, String method, Object[] args, Class[] clzs){
        Callable<Object> task = ()->{
            System.out.println("远程调用");
            if(isDebug) {
                log.debug("远程调用方法地址, ip:{},  method:{}", url, method);
            }
            Object[] objs = getRPCSvcClient(url + CAS_PATH, TIME_OUT_MILL_SECONDS)
                    .invokeBlocking(new QName(NS_ESBMNG, method), args, clzs);
            if(isDebug) {
                log.debug("远程调用方法结束:" + Arrays.toString(objs));
            }
            return objs == null ? null: objs[0];
        };

        Retryer<Object> retryer = RetryerBuilder.<Object>newBuilder()
                        .retryIfException()
                        .withWaitStrategy(WaitStrategies.fixedWait(3, TimeUnit.SECONDS))
                        .withStopStrategy(StopStrategies.stopAfterAttempt(3)).build();
        Object object = null;
        try{
            object = retryer.call(task);
        }catch (Exception e){
            log.error("重试三次后,远程调用失败:"+e.getMessage(),e);
            OprResult oprResult = new OprResult();
            oprResult.setRspCode("8888");
            oprResult.setRspMessage(e.getMessage());
            return oprResult;
        }
        return object;
    }

进行代码重试

待完善。。。

原文地址:https://www.cnblogs.com/lidedong/p/14700193.html