All Downloads are FREE. Search and download functionalities are using the official Maven repository.

pl.allegro.tech.hermes.client.HermesClientTermination Maven / Gradle / Ivy

The newest version!
package pl.allegro.tech.hermes.client;

import java.util.concurrent.CompletableFuture;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import java.util.function.BooleanSupplier;

class HermesClientTermination {

  private final ExecutorService executorService = Executors.newSingleThreadExecutor();
  private final long pollInterval;

  HermesClientTermination(long pollInterval) {
    this.pollInterval = pollInterval;
  }

  CompletableFuture observe(BooleanSupplier condition) {
    final CompletableFuture result = new CompletableFuture<>();

    executorService.submit(
        () -> {
          try {
            while (!condition.getAsBoolean()) {
              Thread.sleep(pollInterval);
            }
            result.complete(null);
          } catch (InterruptedException e) {
            result.completeExceptionally(e);
            Thread.currentThread().interrupt();
          } finally {
            executorService.shutdown();
          }
        });

    return result;
  }
}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy