mirror of
https://github.com/moparisthebest/JdbcMapper
synced 2024-11-21 08:35:00 -05:00
Use lambdas where possible in QueryRunner, fix java6 compilation
This commit is contained in:
parent
133c4eb8c9
commit
5d9ff80863
@ -14,7 +14,7 @@ import static com.moparisthebest.jdbc.TryClose.tryClose;
|
|||||||
public class QueryRunner<T extends JdbcMapper> {
|
public class QueryRunner<T extends JdbcMapper> {
|
||||||
|
|
||||||
private static final int defaultRetryCount = 10;
|
private static final int defaultRetryCount = 10;
|
||||||
private static final DelayStrategy defaultDelayStrategy = exponentialBackoff(1000, 30000, 2000);
|
private static final DelayStrategy defaultDelayStrategy = exponentialBackoff(1000, 30000, 2000, 10);
|
||||||
private static final ExecutorService defaultExecutorService = Executors.newCachedThreadPool(); // todo: good or bad default?
|
private static final ExecutorService defaultExecutorService = Executors.newCachedThreadPool(); // todo: good or bad default?
|
||||||
|
|
||||||
private final Factory<T> factory;
|
private final Factory<T> factory;
|
||||||
@ -117,23 +117,30 @@ public class QueryRunner<T extends JdbcMapper> {
|
|||||||
} catch (SQLException e) {
|
} catch (SQLException e) {
|
||||||
lastException = e;
|
lastException = e;
|
||||||
try {
|
try {
|
||||||
Thread.sleep(delayStrategy.getDelay(x));
|
Thread.sleep(delayStrategy.getDelay(++x));
|
||||||
} catch (InterruptedException e2) {
|
} catch (InterruptedException e2) {
|
||||||
Thread.interrupted();
|
Thread.interrupted();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
} while (++x <= retryCount);
|
} while (x <= retryCount);
|
||||||
throw lastException;
|
throw lastException;
|
||||||
}
|
}
|
||||||
|
|
||||||
public <E> Future<E> runRetryFuture(final Runner<T, E> query) {
|
public <E> Future<E> runRetryFuture(final Runner<T, E> query) {
|
||||||
// todo: sleeps in thread, could use ScheduledExecutorService maybe?
|
// todo: sleeps in thread, could use ScheduledExecutorService maybe?
|
||||||
return executorService.submit(new Callable<E>() {
|
return executorService.submit(
|
||||||
@Override
|
//IFJAVA8_START
|
||||||
public E call() throws Exception {
|
() -> runRetry(query)
|
||||||
return runRetry(query);
|
//IFJAVA8_END
|
||||||
}
|
/*IFJAVA6_START
|
||||||
});
|
new Callable<E>() {
|
||||||
|
@Override
|
||||||
|
public E call() throws Exception {
|
||||||
|
return runRetry(query);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
IFJAVA6_END*/
|
||||||
|
);
|
||||||
}
|
}
|
||||||
|
|
||||||
//IFJAVA8_START
|
//IFJAVA8_START
|
||||||
@ -170,31 +177,74 @@ public class QueryRunner<T extends JdbcMapper> {
|
|||||||
*
|
*
|
||||||
* @author Robert Buck (buck.robert.j@gmail.com)
|
* @author Robert Buck (buck.robert.j@gmail.com)
|
||||||
*/
|
*/
|
||||||
public static DelayStrategy exponentialBackoff(final long minBackoff, final long maxBackoff, final long slotTime) {
|
public static DelayStrategy exponentialBackoff(final long minBackoff, final long maxBackoff, final long slotTime, final long maxContentionPeriods) {
|
||||||
return new DelayStrategy() {
|
return
|
||||||
|
/*IFJAVA6_START
|
||||||
|
new DelayStrategy() {
|
||||||
@Override
|
@Override
|
||||||
public long getDelay(final int attempt) {
|
public long getDelay(final int attempt) {
|
||||||
final int MAX_CONTENTION_PERIODS = 10;
|
return
|
||||||
return attempt == 0 ? 0 : Math.min(minBackoff + ThreadLocalRandom.current().nextInt(2 << Math.min(attempt, MAX_CONTENTION_PERIODS - 1)) * slotTime, maxBackoff);
|
IFJAVA6_END*/
|
||||||
|
//IFJAVA8_START
|
||||||
|
(attempt) ->
|
||||||
|
//IFJAVA8_END
|
||||||
|
attempt == 0 ? 0 : Math.min(minBackoff + ThreadLocalRandom.current().nextInt(2 << Math.min(attempt, maxContentionPeriods - 1)) * slotTime, maxBackoff);
|
||||||
|
/*IFJAVA6_START
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
IFJAVA6_END*/
|
||||||
}
|
}
|
||||||
|
|
||||||
public static DelayStrategy fixedDelay(final long delay) {
|
public static DelayStrategy fixedDelay(final long delay) {
|
||||||
return new DelayStrategy() {
|
return
|
||||||
|
/*IFJAVA6_START
|
||||||
|
new DelayStrategy() {
|
||||||
@Override
|
@Override
|
||||||
public long getDelay(final int attempt) {
|
public long getDelay(final int attempt) {
|
||||||
return delay;
|
return
|
||||||
|
IFJAVA6_END*/
|
||||||
|
//IFJAVA8_START
|
||||||
|
(attempt) ->
|
||||||
|
//IFJAVA8_END
|
||||||
|
delay;
|
||||||
|
/*IFJAVA6_START
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
IFJAVA6_END*/
|
||||||
}
|
}
|
||||||
|
|
||||||
public static DelayStrategy incrementalDelay(final long initialInterval, final long incrementalInterval) {
|
public static DelayStrategy incrementalDelay(final long initialInterval, final long incrementalInterval) {
|
||||||
return new DelayStrategy() {
|
return
|
||||||
|
/*IFJAVA6_START
|
||||||
|
new DelayStrategy() {
|
||||||
@Override
|
@Override
|
||||||
public long getDelay(final int attempt) {
|
public long getDelay(final int attempt) {
|
||||||
return initialInterval + incrementalInterval * attempt;
|
return
|
||||||
|
IFJAVA6_END*/
|
||||||
|
//IFJAVA8_START
|
||||||
|
(attempt) ->
|
||||||
|
//IFJAVA8_END
|
||||||
|
initialInterval + incrementalInterval * attempt;
|
||||||
|
/*IFJAVA6_START
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
IFJAVA6_END*/
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/*IFJAVA6_START
|
||||||
|
// terrible, I know, use java8
|
||||||
|
private static class ThreadLocalRandom {
|
||||||
|
private static final ThreadLocal<java.util.Random> randomThreadLocal = new ThreadLocal<java.util.Random>() {
|
||||||
|
@Override
|
||||||
|
protected java.util.Random initialValue() {
|
||||||
|
return new java.util.Random();
|
||||||
|
}
|
||||||
|
};
|
||||||
|
|
||||||
|
private static java.util.Random current() {
|
||||||
|
return randomThreadLocal.get();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
IFJAVA6_END*/
|
||||||
|
|
||||||
}
|
}
|
||||||
|
11
pom.xml
11
pom.xml
@ -218,6 +218,9 @@
|
|||||||
<excludes>
|
<excludes>
|
||||||
<exclude>target/**</exclude>
|
<exclude>target/**</exclude>
|
||||||
</excludes>
|
</excludes>
|
||||||
|
<regexFlags>
|
||||||
|
<regexFlag>LITERAL</regexFlag>
|
||||||
|
</regexFlags>
|
||||||
<replacements>
|
<replacements>
|
||||||
<replacement>
|
<replacement>
|
||||||
<token>//IFJAVA8_START</token>
|
<token>//IFJAVA8_START</token>
|
||||||
@ -227,6 +230,14 @@
|
|||||||
<token>//IFJAVA8_END</token>
|
<token>//IFJAVA8_END</token>
|
||||||
<value>IFJAVA8_END*/</value>
|
<value>IFJAVA8_END*/</value>
|
||||||
</replacement>
|
</replacement>
|
||||||
|
<replacement>
|
||||||
|
<token>/*IFJAVA6_START</token>
|
||||||
|
<value>//IFJAVA6_START</value>
|
||||||
|
</replacement>
|
||||||
|
<replacement>
|
||||||
|
<token>IFJAVA6_END*/</token>
|
||||||
|
<value>//IFJAVA6_END</value>
|
||||||
|
</replacement>
|
||||||
</replacements>
|
</replacements>
|
||||||
</configuration>
|
</configuration>
|
||||||
</plugin>
|
</plugin>
|
||||||
|
@ -25,8 +25,8 @@ public class QueryRunnerTest {
|
|||||||
final Person actual =
|
final Person actual =
|
||||||
//qr.run(
|
//qr.run(
|
||||||
//qr.runRetry(
|
//qr.runRetry(
|
||||||
//qr.runRetryFuture(
|
qr.runRetryFuture(
|
||||||
qr.runRetryCompletableFuture(
|
//qr.runRetryCompletableFuture(
|
||||||
new QueryRunner.Runner<QueryMapper, Person>() {
|
new QueryRunner.Runner<QueryMapper, Person>() {
|
||||||
@Override
|
@Override
|
||||||
public Person run(final QueryMapper qm) throws SQLException {
|
public Person run(final QueryMapper qm) throws SQLException {
|
||||||
|
@ -1,2 +1,4 @@
|
|||||||
#!/bin/sh
|
#!/bin/sh
|
||||||
find */src/{main,test}/java -type f -name '*.java' -print0 | xargs -0 sed -i -e 's@/\*IFJAVA8_START@//IFJAVA8_START@' -e 's@IFJAVA8_END\*/@//IFJAVA8_END@'
|
find */src/{main,test}/java -type f -name '*.java' -print0 | xargs -0 sed -i \
|
||||||
|
-e 's@/\*IFJAVA8_START@//IFJAVA8_START@' -e 's@IFJAVA8_END\*/@//IFJAVA8_END@' \
|
||||||
|
-e 's@//IFJAVA6_START@/*IFJAVA6_START@' -e 's@//IFJAVA6_END@IFJAVA6_END*/@'
|
||||||
|
Loading…
Reference in New Issue
Block a user