RetrySubscriber(Subscriber<? super T> actual, long count, SubscriptionArbiter sa, Publisher<? extends T> source, Retry retry) { this.actual = actual; this.sa = sa; this.source = source; this.context = retry.context(); this.remaining = count; }
@Override public Upstream<T> apply(Upstream<? extends T> upstream) throws Exception { return down -> { Retry.Context context = retry.context(); Downstream<T> downstream = new Downstream<T>() {
/** * Creates a retryable runnable. * * @param retry the retry context * @param runnable the original runnable * * @return a retryable runnable */ static Runnable decorateRunnable(Retry retry, Runnable runnable){ return () -> { Retry.Context context = retry.context(); do try { runnable.run(); context.onSuccess(); break; } catch (RuntimeException runtimeException) { context.onRuntimeError(runtimeException); } while (true); }; }
/** * Creates a retryable supplier. * * @param retry the retry context * @param supplier the original function * @param <T> the type of results supplied by this supplier * * @return a retryable function */ static <T> Supplier<T> decorateSupplier(Retry retry, Supplier<T> supplier){ return () -> { @SuppressWarnings("unchecked") Retry.Context<T> context = retry.context(); do try { T result = supplier.get(); final boolean validationOfResult = context.onResult(result); if (!validationOfResult) { context.onSuccess(); return result; } } catch (RuntimeException runtimeException) { context.onRuntimeError(runtimeException); } while (true); }; }
/** * Creates a retryable callable. * * @param retry the retry context * @param supplier the original function * @param <T> the type of results supplied by this supplier * * @return a retryable function */ static <T> Callable<T> decorateCallable(Retry retry, Callable<T> supplier){ return () -> { @SuppressWarnings("unchecked") Retry.Context<T> context = retry.context(); do try { T result = supplier.call(); final boolean validationOfResult = context.onResult(result); if (!validationOfResult) { context.onSuccess(); return result; } } catch (RuntimeException runtimeException) { context.onRuntimeError(runtimeException); } while (true); }; }
/** * Creates a retryable function. * * @param retry the retry context * @param function the original function * @param <T> the type of the input to the function * @param <R> the result type of the function * * @return a retryable function */ static <T, R> Function<T, R> decorateFunction(Retry retry, Function<T, R> function){ return (T t) -> { @SuppressWarnings("unchecked") Retry.Context<R> context = retry.context(); do try { R result = function.apply(t); final boolean validationOfResult = context.onResult(result); if (!validationOfResult) { context.onSuccess(); return result; } } catch (RuntimeException runtimeException) { context.onRuntimeError(runtimeException); } while (true); }; }
/** * Creates a retryable runnable. * * @param retry the retry context * @param runnable the original runnable * * @return a retryable runnable */ static CheckedRunnable decorateCheckedRunnable(Retry retry, CheckedRunnable runnable){ return () -> { Retry.Context context = retry.context(); do try { runnable.run(); context.onSuccess(); break; } catch (Exception exception) { context.onError(exception); } while (true); }; }
private Object proceed(MethodInvocation invocation, io.github.resilience4j.retry.Retry retry, RecoveryFunction<?> recoveryFunction) throws Throwable { io.github.resilience4j.retry.Retry.Context context = retry.context(); try { Object result = invocation.proceed(); context.onSuccess(); return result; } catch (Exception e) { // exception thrown, we know a direct value was attempted to be returned Object result; context.onError(e); while (true) { try { result = invocation.proceed(); context.onSuccess(); return result; } catch (Exception e1) { try { context.onError(e1); } catch (Throwable t) { return recoveryFunction.apply(t); } } } } }
/** * Creates a retryable supplier. * * @param retry the retry context * @param supplier the original function * @param <T> the type of results supplied by this supplier * * @return a retryable function */ static <T> CheckedFunction0<T> decorateCheckedSupplier(Retry retry, CheckedFunction0<T> supplier){ return () -> { @SuppressWarnings("unchecked") Retry.Context<T> context = retry.context(); do try { T result = supplier.apply(); final boolean validationOfResult = context.onResult(result); if (!validationOfResult) { context.onSuccess(); return result; } } catch (Exception exception) { context.onError(exception); } while (true); }; }
/** * Creates a retryable function. * * @param retry the retry context * @param function the original function * @param <T> the type of the input to the function * @param <R> the result type of the function * * @return a retryable function */ static <T, R> CheckedFunction1<T, R> decorateCheckedFunction(Retry retry, CheckedFunction1<T, R> function){ return (T t) -> { @SuppressWarnings("unchecked") Retry.Context<R> context = retry.context(); do try { R result = function.apply(t); final boolean validationOfResult = context.onResult(result); if (!validationOfResult) { context.onSuccess(); return result; } } catch (Exception exception) { context.onError(exception); } while (true); }; }
return executeCompletionStage(invocation, stage, retry.context(), recoveryFunction);
RetrySubscriber(Subscriber<? super T> actual, long count, SubscriptionArbiter sa, Publisher<? extends T> source, Retry retry) { this.actual = actual; this.sa = sa; this.source = source; this.context = retry.context(); this.remaining = count; }
@Override public Upstream<T> apply(Upstream<? extends T> upstream) throws Exception { return down -> { Retry.Context context = retry.context(); Downstream<T> downstream = new Downstream<T>() {
private Object proceed(MethodInvocation invocation, io.github.resilience4j.retry.Retry retry, RecoveryFunction<?> recoveryFunction) throws Throwable { io.github.resilience4j.retry.Retry.Context context = retry.context(); try { Object result = invocation.proceed(); context.onSuccess(); return result; } catch (Exception e) { // exception thrown, we know a direct value was attempted to be returned Object result; context.onError(e); while (true) { try { result = invocation.proceed(); context.onSuccess(); return result; } catch (Exception e1) { try { context.onError(e1); } catch (Throwable t) { return recoveryFunction.apply(t); } } } } }
return executeCompletionStage(invocation, stage, retry.context(), recoveryFunction);