public T callFunctionWithRetry()

in src/main/java/com/aliyun/dts/subscribe/clients/common/RetryUtil.java [72:113]


    public <T> T callFunctionWithRetry(int maxRetryTimes, int freezeInternal, TimeUnit freezeTimeUnit,
                                       ThrowableFunction<T> throwableFunction,
                                       BiFunction<Throwable, Integer, Boolean> recoverableChecker,
                                       Consumer<RetryInfo> retryInfoConsumer) throws Exception {
        Throwable error = null;
        RetryInfo retryInfo = null;
        maxRetryTimes = Math.max(1, maxRetryTimes);

        for (int i = 0; i < maxRetryTimes; i++) {
            try {
                T rs = throwableFunction.call();
                if (null != retryInfo) {
                    retryInfo.endRetry();
                }
                return rs;
            } catch (Throwable e) {
                error = e;
                if (recoverableChecker.apply(e, i)) {
                    if (null == retryInfo) {
                        retryInfo = new RetryInfo(globalJobType, objectNameShouldBeRetried);
                    }
                    retryInfo.retry(e);

                    LOG.warn("call function {} with {} times failed, try to execute it again",
                            throwableFunction.toString(), retryInfo.getRetryCount(), e);
                    freezeTimeUnit.sleep(freezeInternal);

                    if (null != retryInfoConsumer) {
                        retryInfoConsumer.accept(retryInfo);
                    }
                } else {
                    break;
                }
            }
        }

        if (error instanceof Exception) {
            throw (Exception) error;
        } else {
            throw new CriticalException("common", error);
        }
    }