@SuppressWarnings("unchecked") private CompletionStage<?> executeCompletionStage(MethodInvocation invocation, CompletionStage<?> stage, io.github.resilience4j.retry.Retry.Context context, RecoveryFunction<?> recoveryFunction) { final CompletableFuture promise = new CompletableFuture(); stage.whenComplete((v, t) -> { if (t != null) { try { context.onError((Exception) t); CompletionStage next = (CompletionStage) invocation.proceed(); CompletableFuture temp = executeCompletionStage(invocation, next, context, recoveryFunction).toCompletableFuture(); promise.complete(temp.join()); } catch (Throwable t2) { try { Object result = recoveryFunction.apply(t); promise.complete(result); } catch (Throwable t3) { promise.completeExceptionally(t3); } } } else { context.onSuccess(); promise.complete(v); } }); return promise; }
/** * 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); }; }
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); } } } } }
@SuppressWarnings("unchecked") private CompletionStage<?> executeCompletionStage(MethodInvocation invocation, CompletionStage<?> stage, io.github.resilience4j.retry.Retry.Context context, RecoveryFunction<?> recoveryFunction) { final CompletableFuture promise = new CompletableFuture(); stage.whenComplete((v, t) -> { if (t != null) { try { context.onError((Exception) t); CompletionStage next = (CompletionStage) invocation.proceed(); CompletableFuture temp = executeCompletionStage(invocation, next, context, recoveryFunction).toCompletableFuture(); promise.complete(temp.join()); } catch (Throwable t2) { try { Object result = recoveryFunction.apply(t); promise.complete(result); } catch (Throwable t3) { promise.completeExceptionally(t3); } } } else { context.onSuccess(); promise.complete(v); } }); return promise; }
@Override public void onError(Throwable t) { if (LOG.isDebugEnabled()) { LOG.info("onError"); } long r = remaining; if (r != Long.MAX_VALUE) { remaining = r - 1; } if (r == 0) { actual.onError(t); } else { try { context.onError((Exception) t); subscribeNext(); } catch (Throwable t2) { actual.onError(t2); } } }
@Override public void onNext(T t) { if (LOG.isDebugEnabled()) { LOG.info("onNext"); } context.onSuccess(); actual.onNext(t); sa.produced(1L); } @Override
/** * 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); }; }
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 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 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); }; }
/** * 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); }; }
@Override public void onError(Throwable t) { if (LOG.isDebugEnabled()) { LOG.info("onError"); } long r = remaining; if (r != Long.MAX_VALUE) { remaining = r - 1; } if (r == 0) { actual.onError(t); } else { try { context.onError((Exception) t); subscribeNext(); } catch (Throwable t2) { actual.onError(t2); } } }
@Override public void onNext(T t) { if (LOG.isDebugEnabled()) { LOG.info("onNext"); } context.onSuccess(); actual.onNext(t); sa.produced(1L); } @Override