+ if (timeout.isPresent()) {
+ while (remainingMessages.get() != 0) {
+ try {
+ final var passedTime = System.currentTimeMillis() - lastMessage.get();
+ final var sleepTimeRemaining = timeout.get().toMillis() - passedTime;
+ if (sleepTimeRemaining < 0) {
+ break;
+ }
+ Thread.sleep(sleepTimeRemaining);
+ } catch (InterruptedException ignored) {
+ }
+ }
+ } else {