@Test public void schedulesSubsequentExecutions24HoursLater() throws Exception { final ScheduledExecutorTester mock = new ScheduledExecutorTester(); BackgroundRunner<String> instance = new BackgroundRunner<>( 16, Clock.fixed(Instant.ofEpochMilli(0), ZoneId.systemDefault()), mock ); instance.start(() -> ""); assertThat(mock.period, is(24L)); assertThat(mock.unit, is(TimeUnit.HOURS)); }
public void start(Producer<E> task) { previousResult = task.call(); final int currentHour = LocalDateTime.ofInstant(clock.instant(), clock.getZone()).getHour(); final int initialDelay; if (currentHour > hourToRunAt) { initialDelay = 24 - currentHour + hourToRunAt; } else { initialDelay = hourToRunAt - currentHour; } healthchecksExecutor.scheduleAtFixedRate(() -> previousResult = task.call(), initialDelay, 24, TimeUnit.HOURS); }
BackgroundRunner { public void start(Producer<E> task) { previousResult = task.call(); final int currentHour = LocalDateTime.ofInstant(clock.instant(), clock.getZone()).getHour(); final int initialDelay; if (currentHour > hourToRunAt) { initialDelay = 24 - currentHour + hourToRunAt; } else { initialDelay = hourToRunAt - currentHour; } healthchecksExecutor.scheduleAtFixedRate(() -> previousResult = task.call(), initialDelay, 24, TimeUnit.HOURS); } }
BackgroundRunner { public void start(Producer<E> task) { previousResult = task.call(); final int currentHour = LocalDateTime.ofInstant(clock.instant(), clock.getZone()).getHour(); final int initialDelay; if (currentHour > hourToRunAt) { initialDelay = 24 - currentHour + hourToRunAt; } else { initialDelay = hourToRunAt - currentHour; } healthchecksExecutor.scheduleAtFixedRate(() -> previousResult = task.call(), initialDelay, 24, TimeUnit.HOURS); } BackgroundRunner(int hourToRunAt, Clock clock, ScheduledExecutorService healthchecksExecutor); }
BackgroundRunner { public void start(Producer<E> task) { previousResult = task.call(); final int currentHour = LocalDateTime.ofInstant(clock.instant(), clock.getZone()).getHour(); final int initialDelay; if (currentHour > hourToRunAt) { initialDelay = 24 - currentHour + hourToRunAt; } else { initialDelay = hourToRunAt - currentHour; } healthchecksExecutor.scheduleAtFixedRate(() -> previousResult = task.call(), initialDelay, 24, TimeUnit.HOURS); } BackgroundRunner(int hourToRunAt, Clock clock, ScheduledExecutorService healthchecksExecutor); void start(Producer<E> task); Optional<E> getMostRecentResult(); }
BackgroundRunner { public void start(Producer<E> task) { previousResult = task.call(); final int currentHour = LocalDateTime.ofInstant(clock.instant(), clock.getZone()).getHour(); final int initialDelay; if (currentHour > hourToRunAt) { initialDelay = 24 - currentHour + hourToRunAt; } else { initialDelay = hourToRunAt - currentHour; } healthchecksExecutor.scheduleAtFixedRate(() -> previousResult = task.call(), initialDelay, 24, TimeUnit.HOURS); } BackgroundRunner(int hourToRunAt, Clock clock, ScheduledExecutorService healthchecksExecutor); void start(Producer<E> task); Optional<E> getMostRecentResult();  }