Search in sources :

Example 1 with ZERO

use of java.time.Duration.ZERO in project mule by mulesoft.

the class SimpleRetryPolicy method applyPolicy.

@Override
public <T> Publisher<T> applyPolicy(Publisher<T> publisher, Predicate<Throwable> shouldRetry, Consumer<Throwable> onExhausted, Function<Throwable, Throwable> errorFunction, Scheduler retryScheduler) {
    return from(publisher).onErrorResume(e -> {
        if (shouldRetry.test(e)) {
            Retry<T> retry = (Retry<T>) onlyIf(ctx -> shouldRetry.test(unwrap(ctx.exception()))).backoff(ctx -> new BackoffDelay(frequency, ZERO, ZERO));
            if (count != RETRY_COUNT_FOREVER) {
                retry = retry.retryMax(count - 1);
            }
            reactor.core.scheduler.Scheduler reactorRetryScheduler = fromExecutorService(new ConditionalExecutorServiceDecorator(retryScheduler, s -> isTransactionActive()));
            Mono<T> retryMono = from(publisher).retryWhen(retry.withBackoffScheduler(reactorRetryScheduler)).doOnError(e2 -> onExhausted.accept(unwrap(e2))).onErrorMap(RetryExhaustedException.class, e2 -> errorFunction.apply(unwrap(e2.getCause())));
            return delay(frequency, reactorRetryScheduler).then(isTransactionActive() ? just(retryMono.block()) : retryMono);
        } else {
            e = unwrap(e);
            onExhausted.accept(e);
            return error(errorFunction.apply(e));
        }
    });
}
Also used : BackoffDelay(reactor.retry.BackoffDelay) Disposable(reactor.core.Disposable) Schedulers.fromExecutorService(reactor.core.scheduler.Schedulers.fromExecutorService) Exceptions.unwrap(org.mule.runtime.core.api.rx.Exceptions.unwrap) Thread.currentThread(java.lang.Thread.currentThread) Function(java.util.function.Function) TransactionCoordination.isTransactionActive(org.mule.runtime.core.api.transaction.TransactionCoordination.isTransactionActive) DEFAULT_RETRY_COUNT(org.mule.runtime.core.api.retry.policy.SimpleRetryPolicyTemplate.DEFAULT_RETRY_COUNT) Scheduler(org.mule.runtime.api.scheduler.Scheduler) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) Retry.onlyIf(reactor.retry.Retry.onlyIf) RetryPolicy(org.mule.runtime.core.api.retry.policy.RetryPolicy) Duration(java.time.Duration) Mono.from(reactor.core.publisher.Mono.from) Mono.just(reactor.core.publisher.Mono.just) Thread.sleep(java.lang.Thread.sleep) DEFAULT_FREQUENCY(org.mule.runtime.core.api.retry.policy.SimpleRetryPolicyTemplate.DEFAULT_FREQUENCY) Retry(reactor.retry.Retry) Mono.error(reactor.core.publisher.Mono.error) Logger(org.slf4j.Logger) ConditionalExecutorServiceDecorator(org.mule.runtime.core.internal.util.rx.ConditionalExecutorServiceDecorator) Predicate(java.util.function.Predicate) Publisher(org.reactivestreams.Publisher) Mono(reactor.core.publisher.Mono) TimeUnit(java.util.concurrent.TimeUnit) Consumer(java.util.function.Consumer) PolicyStatus(org.mule.runtime.core.api.retry.policy.PolicyStatus) RetryExhaustedException(reactor.retry.RetryExhaustedException) Mono.delay(reactor.core.publisher.Mono.delay) Schedulers.immediate(reactor.core.scheduler.Schedulers.immediate) ZERO(java.time.Duration.ZERO) LoggerFactory.getLogger(org.slf4j.LoggerFactory.getLogger) Duration.ofMillis(java.time.Duration.ofMillis) RETRY_COUNT_FOREVER(org.mule.runtime.core.api.retry.policy.SimpleRetryPolicyTemplate.RETRY_COUNT_FOREVER) DEFAULT_RETRY_COUNT(org.mule.runtime.core.api.retry.policy.SimpleRetryPolicyTemplate.DEFAULT_RETRY_COUNT) BackoffDelay(reactor.retry.BackoffDelay) ConditionalExecutorServiceDecorator(org.mule.runtime.core.internal.util.rx.ConditionalExecutorServiceDecorator) Retry(reactor.retry.Retry)

Aggregations

Thread.currentThread (java.lang.Thread.currentThread)1 Thread.sleep (java.lang.Thread.sleep)1 Duration (java.time.Duration)1 ZERO (java.time.Duration.ZERO)1 Duration.ofMillis (java.time.Duration.ofMillis)1 TimeUnit (java.util.concurrent.TimeUnit)1 AtomicInteger (java.util.concurrent.atomic.AtomicInteger)1 Consumer (java.util.function.Consumer)1 Function (java.util.function.Function)1 Predicate (java.util.function.Predicate)1 Scheduler (org.mule.runtime.api.scheduler.Scheduler)1 PolicyStatus (org.mule.runtime.core.api.retry.policy.PolicyStatus)1 RetryPolicy (org.mule.runtime.core.api.retry.policy.RetryPolicy)1 DEFAULT_FREQUENCY (org.mule.runtime.core.api.retry.policy.SimpleRetryPolicyTemplate.DEFAULT_FREQUENCY)1 DEFAULT_RETRY_COUNT (org.mule.runtime.core.api.retry.policy.SimpleRetryPolicyTemplate.DEFAULT_RETRY_COUNT)1 RETRY_COUNT_FOREVER (org.mule.runtime.core.api.retry.policy.SimpleRetryPolicyTemplate.RETRY_COUNT_FOREVER)1 Exceptions.unwrap (org.mule.runtime.core.api.rx.Exceptions.unwrap)1 TransactionCoordination.isTransactionActive (org.mule.runtime.core.api.transaction.TransactionCoordination.isTransactionActive)1 ConditionalExecutorServiceDecorator (org.mule.runtime.core.internal.util.rx.ConditionalExecutorServiceDecorator)1 Publisher (org.reactivestreams.Publisher)1