org.testifyproject.failsafe.SyncFailsafe Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of core Show documentation
Show all versions of core Show documentation
A module that provides core Testify SPI and implementation classes
The newest version!
/*
* Copyright 2016 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License
*/
package org.testifyproject.failsafe;
import java.util.concurrent.Callable;
import java.util.concurrent.ScheduledExecutorService;
import org.testifyproject.failsafe.Functions.ContextualCallableWrapper;
import org.testifyproject.failsafe.function.CheckedRunnable;
import org.testifyproject.failsafe.function.ContextualCallable;
import org.testifyproject.failsafe.function.ContextualRunnable;
import org.testifyproject.failsafe.internal.util.Assert;
import org.testifyproject.failsafe.util.concurrent.Scheduler;
import org.testifyproject.failsafe.util.concurrent.Schedulers;
/**
* Performs synchronous executions with failures handled according to a configured {@link #with(RetryPolicy) retry
* policy}, {@link #with(CircuitBreaker) circuit breaker} and
* {@link #withFallback(org.testifyproject.failsafe.function.BiFunction) fallback}.
*
* @author Jonathan Halterman
* @param listener result type
*/
public class SyncFailsafe extends FailsafeConfig> {
SyncFailsafe(CircuitBreaker circuitBreaker) {
this.circuitBreaker = circuitBreaker;
}
SyncFailsafe(RetryPolicy retryPolicy) {
this.retryPolicy = retryPolicy;
}
/**
* Executes the {@code callable} until a successful result is returned or the configured {@link RetryPolicy} is
* exceeded.
*
* @throws NullPointerException if the {@code callable} is null
* @throws FailsafeException if the {@code callable} fails with a checked Exception or if interrupted while waiting to
* perform a retry.
* @throws CircuitBreakerOpenException if a configured circuit is open.
*/
public T get(Callable callable) {
return call(Assert.notNull(callable, "callable"));
}
/**
* Executes the {@code callable} until a successful result is returned or the configured {@link RetryPolicy} is
* exceeded.
*
* @throws NullPointerException if the {@code callable} is null
* @throws FailsafeException if the {@code callable} fails with a checked Exception or if interrupted while waiting to
* perform a retry.
* @throws CircuitBreakerOpenException if a configured circuit is open.
*/
public T get(ContextualCallable callable) {
return call(Functions.callableOf(callable));
}
/**
* Executes the {@code runnable} until successful or until the configured {@link RetryPolicy} is exceeded.
*
* @throws NullPointerException if the {@code runnable} is null
* @throws FailsafeException if the {@code callable} fails with a checked Exception or if interrupted while waiting to
* perform a retry.
* @throws CircuitBreakerOpenException if a configured circuit is open.
*/
public void run(CheckedRunnable runnable) {
call(Functions.callableOf(runnable));
}
/**
* Executes the {@code runnable} until successful or until the configured {@link RetryPolicy} is exceeded.
*
* @throws NullPointerException if the {@code runnable} is null
* @throws FailsafeException if the {@code runnable} fails with a checked Exception or if interrupted while waiting to
* perform a retry.
* @throws CircuitBreakerOpenException if a configured circuit is open.
*/
public void run(ContextualRunnable runnable) {
call(Functions.callableOf(runnable));
}
/**
* Creates and returns a new AsyncFailsafe instance that will perform executions and retries asynchronously via the
* {@code executor}.
*
* @throws NullPointerException if {@code executor} is null
*/
public AsyncFailsafe with(ScheduledExecutorService executor) {
return new AsyncFailsafe(this, Schedulers.of(executor));
}
/**
* Creates and returns a new AsyncFailsafe instance that will perform executions and retries asynchronously via the
* {@code scheduler}.
*
* @throws NullPointerException if {@code scheduler} is null
*/
public AsyncFailsafe with(Scheduler scheduler) {
return new AsyncFailsafe(this, Assert.notNull(scheduler, "scheduler"));
}
/**
* Calls the {@code callable} synchronously, performing retries according to the {@code retryPolicy}.
*
* @throws FailsafeException if the {@code callable} fails with a checked Exception or if interrupted while waiting to
* perform a retry.
* @throws CircuitBreakerOpenException if a configured circuit breaker is open
*/
@SuppressWarnings("unchecked")
private T call(Callable callable) {
Execution execution = new Execution((FailsafeConfig