This repository has been archived by the owner on Mar 30, 2019. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 80
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[#3] Merging various retry policies to allow deterministic RetryPolic…
…y irrespective to order of declaration
- Loading branch information
1 parent
9a47f69
commit 3c091c2
Showing
13 changed files
with
125 additions
and
291 deletions.
There are no files selected for viewing
28 changes: 0 additions & 28 deletions
28
src/main/java/com/blogspot/nurkiewicz/asyncretry/policy/MaxRetriesPolicy.java
This file was deleted.
Oops, something went wrong.
16 changes: 0 additions & 16 deletions
16
src/main/java/com/blogspot/nurkiewicz/asyncretry/policy/RetryInfinitelyPolicy.java
This file was deleted.
Oops, something went wrong.
92 changes: 77 additions & 15 deletions
92
src/main/java/com/blogspot/nurkiewicz/asyncretry/policy/RetryPolicy.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,38 +1,100 @@ | ||
package com.blogspot.nurkiewicz.asyncretry.policy; | ||
|
||
import com.blogspot.nurkiewicz.asyncretry.RetryContext; | ||
import com.blogspot.nurkiewicz.asyncretry.policy.exception.AbortPredicateRetryPolicy; | ||
import com.blogspot.nurkiewicz.asyncretry.policy.exception.ExceptionClassRetryPolicy; | ||
|
||
import java.util.Arrays; | ||
import java.util.Collections; | ||
import java.util.HashSet; | ||
import java.util.Set; | ||
import java.util.function.Predicate; | ||
|
||
/** | ||
* @author Tomasz Nurkiewicz | ||
* @since 7/16/13, 6:05 PM | ||
*/ | ||
public interface RetryPolicy { | ||
public class RetryPolicy { | ||
|
||
public static final RetryPolicy DEFAULT = new RetryInfinitelyPolicy(); | ||
public static final RetryPolicy DEFAULT = new RetryPolicy(); | ||
|
||
boolean shouldContinue(RetryContext context); | ||
private final int maxRetries; | ||
private final Set<Class<? extends Throwable>> retryOn; | ||
private final Set<Class<? extends Throwable>> abortOn; | ||
private final Predicate<Throwable> retryPredicate; | ||
private final Predicate<Throwable> abortPredicate; | ||
|
||
default RetryPolicy retryOn(Class<? extends Throwable>... retryOnThrowables) { | ||
return ExceptionClassRetryPolicy.retryOn(this, retryOnThrowables); | ||
public RetryPolicy retryOn(Class<? extends Throwable>... retryOnThrowables) { | ||
return new RetryPolicy(maxRetries, setPlusElems(retryOn, retryOnThrowables), abortOn, retryPredicate, abortPredicate); | ||
} | ||
|
||
default RetryPolicy abortOn(Class<? extends Throwable>... retryOnThrowables) { | ||
return ExceptionClassRetryPolicy.abortOn(this, retryOnThrowables); | ||
public RetryPolicy abortOn(Class<? extends Throwable>... abortOnThrowables) { | ||
return new RetryPolicy(maxRetries, retryOn, setPlusElems(abortOn, abortOnThrowables), retryPredicate, abortPredicate); | ||
} | ||
|
||
default RetryPolicy abortIf(Predicate<Throwable> retryPredicate) { | ||
return new AbortPredicateRetryPolicy(this, retryPredicate); | ||
public RetryPolicy abortIf(Predicate<Throwable> abortPredicate) { | ||
return new RetryPolicy(maxRetries, retryOn, abortOn, retryPredicate, this.abortPredicate.or(abortPredicate)); | ||
} | ||
|
||
default RetryPolicy dontRetry() { | ||
return new MaxRetriesPolicy(this, 0); | ||
public RetryPolicy retryIf(Predicate<Throwable> retryPredicate) { | ||
return new RetryPolicy(maxRetries, retryOn, abortOn, this.retryPredicate.or(retryPredicate), abortPredicate); | ||
} | ||
|
||
default RetryPolicy withMaxRetries(int times) { | ||
return new MaxRetriesPolicy(this, times); | ||
public RetryPolicy dontRetry() { | ||
return new RetryPolicy(0, retryOn, abortOn, retryPredicate, abortPredicate); | ||
} | ||
|
||
public RetryPolicy withMaxRetries(int times) { | ||
return new RetryPolicy(times, retryOn, abortOn, retryPredicate, abortPredicate); | ||
} | ||
|
||
public RetryPolicy(int maxRetries, Set<Class<? extends Throwable>> retryOn, Set<Class<? extends Throwable>> abortOn, Predicate<Throwable> retryPredicate, Predicate<Throwable> abortPredicate) { | ||
this.maxRetries = maxRetries; | ||
this.retryOn = retryOn; | ||
this.abortOn = abortOn; | ||
this.retryPredicate = retryPredicate; | ||
this.abortPredicate = abortPredicate; | ||
} | ||
|
||
public RetryPolicy() { | ||
this(Integer.MAX_VALUE, Collections.emptySet(), Collections.emptySet(), th -> false, th -> false); | ||
} | ||
|
||
public boolean shouldContinue(RetryContext context) { | ||
if (tooManyRetries(context)) { | ||
return false; | ||
} | ||
if (abortPredicate.test(context.getLastThrowable())) { | ||
return false; | ||
} | ||
if (retryPredicate.test(context.getLastThrowable())) { | ||
return true; | ||
} | ||
return exceptionClassRetryable(context); | ||
} | ||
|
||
private boolean tooManyRetries(RetryContext context) { | ||
return context.getRetryCount() > maxRetries; | ||
} | ||
|
||
private boolean exceptionClassRetryable(RetryContext context) { | ||
if (context.getLastThrowable() == null) { | ||
return false; | ||
} | ||
final Class<? extends Throwable> e = context.getLastThrowable().getClass(); | ||
if (abortOn.isEmpty()) { | ||
return matches(e, retryOn); | ||
} else { | ||
return !matches(e, abortOn) && matches(e, retryOn); | ||
} | ||
} | ||
|
||
private static boolean matches(Class<? extends Throwable> throwable, Set<Class<? extends Throwable>> set) { | ||
return set.isEmpty() || set.stream().anyMatch(c -> c.isAssignableFrom(throwable)); | ||
} | ||
|
||
private static <T> Set<T> setPlusElems(Set<T> initial, T... newElement) { | ||
final HashSet<T> copy = new HashSet<>(initial); | ||
copy.addAll(Arrays.asList(newElement)); | ||
return Collections.unmodifiableSet(copy); | ||
} | ||
|
||
} |
17 changes: 0 additions & 17 deletions
17
src/main/java/com/blogspot/nurkiewicz/asyncretry/policy/RetryPolicyWrapper.java
This file was deleted.
Oops, something went wrong.
28 changes: 0 additions & 28 deletions
28
...n/java/com/blogspot/nurkiewicz/asyncretry/policy/exception/AbortPredicateRetryPolicy.java
This file was deleted.
Oops, something went wrong.
85 changes: 0 additions & 85 deletions
85
...n/java/com/blogspot/nurkiewicz/asyncretry/policy/exception/ExceptionClassRetryPolicy.java
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.