I'd probably wrap it in something like:
public class Retry<T, E extends Exception> {
private final Exception retryOn;
private final Callable<T> to attempt;
public Retry(Class<E> retryOn, Callable<T> toAttempt) {
this.retryOn = retryOn;
this.toAttempt = toAttempt;
}
Optional<T> attempt(int times) throws Exception {
for (int i = times; i > 0; i--) {
try {
toAttempt.call();
}
catch (Exception e) {
if (! retryOn.assigableFrom(e.getClass()) { throw e; }
}
return Optional.absent();
}
}
public static <T, E extends Exception> Optional<T> retry(int times,
Class<? extends Exception> ec, Callable<T> c) throws Exception {
return new Retry<T, E>(ec, c).attempt(times);
}
}
Though I would probably noodle with inclusion/exclusion of what to
capture as it would make using it nicer.
If using sql object stuff, it would be nice to wrap it up an an
annotation based interceptor. Maybe we should support some generic
means of wrapping sql objects (pass them to something that returns
something that is actually returned) so you could let guice or
somethign else attach behavior.
-Brian
On Wed, May 16, 2012 at 3:04 PM, Steven Schlansker