diff --git a/common/src/main/java/com/moparisthebest/jdbc/QueryRunner.java b/common/src/main/java/com/moparisthebest/jdbc/QueryRunner.java index 87a5445..a9d852c 100644 --- a/common/src/main/java/com/moparisthebest/jdbc/QueryRunner.java +++ b/common/src/main/java/com/moparisthebest/jdbc/QueryRunner.java @@ -14,7 +14,7 @@ import static com.moparisthebest.jdbc.TryClose.tryClose; public class QueryRunner { private static final int defaultRetryCount = 10; - private static final DelayStrategy defaultDelayStrategy = new ExponentialBackoff(); + private static final DelayStrategy defaultDelayStrategy = exponentialBackoff(1000, 30000, 2000); private static final ExecutorService defaultExecutorService = Executors.newCachedThreadPool(); // todo: good or bad default? private final Factory factory; @@ -136,6 +136,21 @@ public class QueryRunner { }); } + //IFJAVA8_START + + public CompletableFuture runRetryCompletableFuture(final Runner query) { + // todo: sleeps in thread, could use ScheduledExecutorService maybe? + return CompletableFuture.supplyAsync(() -> { + try { + return runRetry(query); + } catch (SQLException e) { + throw new RuntimeException(e); + } + }, executorService); + } + + //IFJAVA8_END + public static interface Runner { E run(T dao) throws SQLException; } @@ -148,18 +163,6 @@ public class QueryRunner { return defaultDelayStrategy; } - public static DelayStrategy exponentialBackoff(long minBackoff, long maxBackoff, long slotTime) { - return new ExponentialBackoff(minBackoff, maxBackoff, slotTime); - } - - public static DelayStrategy fixedDelay(long delay) { - return new FixedDelay(delay); - } - - public static DelayStrategy incrementalDelay(long initialInterval, long incrementalInterval) { - return new IncrementalDelay(initialInterval, incrementalInterval); - } - /** * Implements truncated binary exponential backoff to calculate retry delay per * IEEE 802.3-2008 Section 1. There will be at most ten (10) contention periods @@ -167,60 +170,31 @@ public class QueryRunner { * * @author Robert Buck (buck.robert.j@gmail.com) */ - private static class ExponentialBackoff implements DelayStrategy { - - public static final long DEFAULT_MIN_BACKOFF = TimeUnit.MILLISECONDS.convert(1, TimeUnit.SECONDS); - public static final long DEFAULT_MAX_BACKOFF = TimeUnit.MILLISECONDS.convert(30, TimeUnit.SECONDS); - public static final long DEFAULT_SLOT_TIME = TimeUnit.MILLISECONDS.convert(2, TimeUnit.SECONDS); - - private final long minBackoff; - private final long maxBackoff; - private final long slotTime; - - private ExponentialBackoff() { - this(DEFAULT_MIN_BACKOFF, DEFAULT_MAX_BACKOFF, DEFAULT_SLOT_TIME); - } - - private ExponentialBackoff(long minBackoff, long maxBackoff, long slotTime) { - this.minBackoff = minBackoff; - this.maxBackoff = maxBackoff; - this.slotTime = slotTime; - } - - @Override - public long getDelay(final int retryCount) { - final int MAX_CONTENTION_PERIODS = 10; - return retryCount == 0 ? 0 : Math.min(minBackoff + ThreadLocalRandom.current().nextInt(2 << Math.min(retryCount, MAX_CONTENTION_PERIODS - 1)) * slotTime, maxBackoff); - } + public static DelayStrategy exponentialBackoff(final long minBackoff, final long maxBackoff, final long slotTime) { + return new DelayStrategy() { + @Override + public long getDelay(final int attempt) { + final int MAX_CONTENTION_PERIODS = 10; + return attempt == 0 ? 0 : Math.min(minBackoff + ThreadLocalRandom.current().nextInt(2 << Math.min(attempt, MAX_CONTENTION_PERIODS - 1)) * slotTime, maxBackoff); + } + }; } - private static class FixedDelay implements DelayStrategy { - private final long delay; - - private FixedDelay(final long delay) { - this.delay = delay; - } - - @Override - public long getDelay(final int attempt) { - return delay; - } + public static DelayStrategy fixedDelay(final long delay) { + return new DelayStrategy() { + @Override + public long getDelay(final int attempt) { + return delay; + } + }; } - private static class IncrementalDelay implements DelayStrategy { - - private final long initialInterval; - private final long incrementalInterval; - - private IncrementalDelay(long initialInterval, long incrementalInterval) { - this.initialInterval = initialInterval; - this.incrementalInterval = incrementalInterval; - } - - @Override - public long getDelay(final int attempt) { - return initialInterval + incrementalInterval * attempt; - } + public static DelayStrategy incrementalDelay(final long initialInterval, final long incrementalInterval) { + return new DelayStrategy() { + @Override + public long getDelay(final int attempt) { + return initialInterval + incrementalInterval * attempt; + } + }; } - } diff --git a/querymapper/src/test/java/com/moparisthebest/jdbc/QueryRunnerTest.java b/querymapper/src/test/java/com/moparisthebest/jdbc/QueryRunnerTest.java index 5b086bf..8f93022 100644 --- a/querymapper/src/test/java/com/moparisthebest/jdbc/QueryRunnerTest.java +++ b/querymapper/src/test/java/com/moparisthebest/jdbc/QueryRunnerTest.java @@ -25,7 +25,8 @@ public class QueryRunnerTest { final Person actual = //qr.run( //qr.runRetry( - qr.runRetryFuture( + //qr.runRetryFuture( + qr.runRetryCompletableFuture( new QueryRunner.Runner() { @Override public Person run(final QueryMapper qm) throws SQLException { @@ -37,6 +38,8 @@ public class QueryRunnerTest { } }) .get() + //.join() + //.thenAccept(actual -> Assert.assertEquals(expected, actual)) ; /* System.out.println("expected: " + expected);