/
FixedDelayPollingStrategy.java
39 lines (34 loc) · 1.28 KB
/
FixedDelayPollingStrategy.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
package io.burt.athena.polling;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.time.Clock;
import java.time.Duration;
import java.time.Instant;
import java.util.Optional;
import java.util.concurrent.ExecutionException;
import java.util.concurrent.TimeUnit;
import java.util.concurrent.TimeoutException;
public class FixedDelayPollingStrategy implements PollingStrategy {
private final Duration delay;
private final Sleeper sleeper;
private Clock clock;
FixedDelayPollingStrategy(Duration delay) {
this(delay, duration -> TimeUnit.MILLISECONDS.sleep(duration.toMillis()), Clock.systemDefaultZone());
}
FixedDelayPollingStrategy(Duration delay, Sleeper sleeper, Clock clock) {
this.delay = delay;
this.sleeper = sleeper;
this.clock = clock;
}
@Override
public ResultSet pollUntilCompleted(PollingCallback callback, Instant deadline) throws SQLException, TimeoutException, ExecutionException, InterruptedException {
while (true) {
Optional<ResultSet> resultSet = callback.poll(deadline);
if (resultSet.isPresent()) {
return resultSet.get();
} else {
sleeper.sleep(sleepDuration(delay, clock.instant(), deadline));
}
}
}
}