Skip to content

Commit

Permalink
Initial refactoring for Java 8 requirement
Browse files Browse the repository at this point in the history
  • Loading branch information
jhalterman committed Dec 25, 2018
1 parent 5d919d5 commit 70e544d
Show file tree
Hide file tree
Showing 24 changed files with 856 additions and 735 deletions.
515 changes: 515 additions & 0 deletions README-1x.md

Large diffs are not rendered by default.

30 changes: 4 additions & 26 deletions README.md
Expand Up @@ -10,7 +10,7 @@

## Introduction

Failsafe is a lightweight, zero-dependency library for handling failures. It was designed to be as easy to use as possible, with a concise API for handling everyday use cases and the flexibility to handle everything else. Failsafe features:
Failsafe is a lightweight, zero-dependency library for handling failures for Java 8+. It was designed to be as easy to use as possible, with a concise API for handling everyday use cases and the flexibility to handle everything else. Failsafe features:

* [Retries](#retries)
* [Circuit breakers](#circuit-breakers)
Expand All @@ -21,8 +21,6 @@ Failsafe is a lightweight, zero-dependency library for handling failures. It was
* [CompletableFuture](#completablefuture-integration) and [functional interface](#functional-interface-integration) integration
* [Execution tracking](#execution-tracking)

Supports Java 6+ though the documentation uses lambdas for simplicity.

## Setup

Add the latest [Failsafe Maven dependency][maven] to your project.
Expand Down Expand Up @@ -50,16 +48,6 @@ Failsafe.with(retryPolicy).run(() -> connect());
Connection connection = Failsafe.with(retryPolicy).get(() -> connect());
```

Java 6 and 7 are also supported:

```java
Connection connection = Failsafe.with(retryPolicy).get(new Callable<Connection>() {
public Connection call() {
return connect();
}
});
```

#### Retry Policies

Failsafe's [retry policies][RetryPolicy] provide flexibility in allowing you to express when retries should be performed.
Expand Down Expand Up @@ -255,6 +243,7 @@ breaker.close();

if (breaker.allowsExecution()) {
try {
breaker.preExecute();
doSomething();
breaker.recordSuccess();
} catch (Exception e) {
Expand Down Expand Up @@ -353,17 +342,6 @@ Failsafe.with(retryPolicy)
.get(this::connect);
```

Java 6 and 7 users can extend the [Listeners] class and override individual event handlers:

```java
Failsafe.with(retryPolicy)
.with(new Listeners<Connection>() {
public void onRetry(Connection cxn, Throwable failure, ExecutionContext ctx) {
log.warn("Failure #{}. Retrying.", ctx.getExecutions());
}
}).get(() -> connect());
```

[CircuitBreaker] related event listeners can also be registered:

```java
Expand All @@ -389,7 +367,7 @@ Failsafe can also perform asynchronous executions and retries on 3rd party sched

#### CompletableFuture Integration

Java 8 users can use Failsafe to retry [CompletableFuture] or [CompletionStage] calls:
Failsafe can be used to retry [CompletableFuture] or [CompletionStage] calls:

```java
Failsafe.with(retryPolicy)
Expand All @@ -401,7 +379,7 @@ Failsafe.with(retryPolicy)

#### Functional Interface Integration

Failsafe can be used to create retryable Java 8 functional interfaces:
Failsafe can be used to create retryable functional interfaces:

```java
Function<String, Connection> connect = address -> Failsafe.with(retryPolicy).get(() -> connect(address));
Expand Down
25 changes: 2 additions & 23 deletions pom.xml
Expand Up @@ -14,11 +14,6 @@
<name>Failsafe</name>
<url>http://github.com/jhalterman/failsafe/</url>

<properties>
<devCompileVersion>1.8</devCompileVersion>
<releaseCompileVersion>1.6</releaseCompileVersion>
</properties>

<licenses>
<license>
<name>Apache License, Version 2.0</name>
Expand Down Expand Up @@ -89,8 +84,8 @@
<artifactId>maven-compiler-plugin</artifactId>
<version>3.2</version>
<configuration>
<source>${devCompileVersion}</source>
<target>${devCompileVersion}</target>
<source>1.8</source>
<target>1.8</target>
</configuration>
</plugin>
<plugin>
Expand Down Expand Up @@ -188,22 +183,6 @@
<id>release</id>
<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<version>3.2</version>
<executions>
<execution>
<id>default-compile</id>
<configuration>
<compilerArguments>
<source>${releaseCompileVersion}</source>
<target>${releaseCompileVersion}</target>
</compilerArguments>
</configuration>
</execution>
</executions>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-source-plugin</artifactId>
Expand Down
19 changes: 5 additions & 14 deletions src/main/java/net/jodah/failsafe/AbstractExecution.java
Expand Up @@ -25,9 +25,8 @@
import java.util.concurrent.TimeUnit;

public abstract class AbstractExecution extends ExecutionContext {
final FailsafeConfig<Object, ?> config;
final EventHandler<Object> eventHandler;
final Callable<Object> callable;
Callable<Object> callable;

// Internally mutable state
volatile Object lastResult;
Expand All @@ -41,20 +40,11 @@ public abstract class AbstractExecution extends ExecutionContext {
volatile boolean completed;
volatile boolean success;

/**
* Creates a new standalone AbstractExecution for the {@code config}.
*/
AbstractExecution(FailsafeConfig<Object, ?> config) {
this(null, config);
}

/**
* Creates a new AbstractExecution for the {@code callable} and {@code config}.
*/
AbstractExecution(Callable<Object> callable, FailsafeConfig<Object, ?> config) {
AbstractExecution(FailsafeConfig<Object, ?> config) {
super(new Duration(System.nanoTime(), TimeUnit.NANOSECONDS));
this.config = config;
this.callable = callable;
eventHandler = config.eventHandler;

PolicyExecutor next = null;
Expand All @@ -76,8 +66,9 @@ public abstract class AbstractExecution extends ExecutionContext {
head = next;
}

void addPolicy(FailsafePolicy policy) {
head = buildPolicyExecutor(policy, head);
@SuppressWarnings("unchecked")
void inject(Callable<?> callable) {
this.callable = (Callable<Object>) callable;
}

private PolicyExecutor buildPolicyExecutor(FailsafePolicy policy, PolicyExecutor next) {
Expand Down
10 changes: 7 additions & 3 deletions src/main/java/net/jodah/failsafe/AsyncExecution.java
Expand Up @@ -33,13 +33,17 @@ public final class AsyncExecution extends AbstractExecution {
private volatile boolean retryCalled;

@SuppressWarnings("unchecked")
<T> AsyncExecution(Callable<T> callable, Scheduler scheduler, FailsafeFuture<T> future,
FailsafeConfig<Object, ?> config) {
super((Callable<Object>) callable, config);
<T> AsyncExecution(Scheduler scheduler, FailsafeFuture<T> future, FailsafeConfig<?, ?> config) {
super((FailsafeConfig<Object, ?>) config);
this.scheduler = scheduler;
this.future = (FailsafeFuture<Object>) future;
}

<T> AsyncExecution(Callable<T> callable, Scheduler scheduler, FailsafeFuture<T> future, FailsafeConfig<?, ?> config) {
this(scheduler, future, config);
inject(callable);
}

/**
* Completes the execution and the associated {@code FutureResult}.
*
Expand Down

0 comments on commit 70e544d

Please sign in to comment.