@Override public void run() { try { if (extendDeadline.getAndSet(false)) { int newDeadlineSec = computeDeadlineSeconds(); messageDeadlineSeconds.set(newDeadlineSec); extendDeadlines(); // Don't bother cancelling this when we stop. It'd just set an atomic boolean. systemExecutor.schedule( setExtendDeadline, newDeadlineSec - ackExpirationPadding.getSeconds(), TimeUnit.SECONDS); } processOutstandingAckOperations(); } catch (Throwable t) { // Catch everything so that one run failing doesn't prevent subsequent runs. logger.log(Level.WARNING, "failed to run periodic job", t); } } },
@Test public void testDeadlineAdjustment() throws Exception { assertThat(dispatcher.computeDeadlineSeconds()).isEqualTo(10); dispatcher.processReceivedMessages(Collections.singletonList(TEST_MESSAGE), NOOP_RUNNABLE); clock.advance(42, TimeUnit.SECONDS); consumers.take().ack(); assertThat(dispatcher.computeDeadlineSeconds()).isEqualTo(42); } }