Class ResourceWaiter<T>

java.lang.Object
com.eqixiac.equinix.core.waiter.ResourceWaiter<T>
Type Parameters:
T - the resource type

public final class ResourceWaiter<T> extends Object
A small, generic poller for asynchronous Equinix resources: re-fetch a resource on an interval until it reaches a desired state, fails, or a timeout elapses. Most provisioning operations are asynchronous (a created connection starts PROVISIONING and later becomes PROVISIONED); this removes the hand-rolled sleep/poll loops callers would otherwise write.

Usage

Connection conn = fabric.connections().define(ConnectionType.EVPL_VC)... .create();

Connection ready = ResourceWaiter.forResource(() -> fabric.connections().getByUuid(conn.getUuid()))
        .until(c -> c.getState() == ConnectionState.PROVISIONED)
        .failWhen(c -> c.getState() == ConnectionState.FAILED)
        .timeout(Duration.ofMinutes(10))
        .pollInterval(Duration.ofSeconds(15))
        .await();

await() returns the resource once the success condition holds, throws WaiterFailedException if the failure condition is reached first, and WaiterTimeoutException if neither happens before the timeout.

Author:
ianjones
  • Method Details

    • forResource

      public static <T> ResourceWaiter<T> forResource(Supplier<T> fetch)
      Begins a waiter that re-fetches the resource via the given supplier (e.g. () -> fabric.connections().getByUuid(uuid)).
      Type Parameters:
      T - the resource type
      Parameters:
      fetch - supplies the current resource state on each poll
      Returns:
      a new waiter to configure
    • until

      public ResourceWaiter<T> until(Predicate<T> successCondition)
      The success condition; await() returns once it holds. Required.
      Parameters:
      successCondition - predicate over the fetched resource
      Returns:
      this waiter
    • failWhen

      public ResourceWaiter<T> failWhen(Predicate<T> failureCondition)
      An optional terminal failure condition; if it holds, await() throws WaiterFailedException immediately rather than polling to timeout.
      Parameters:
      failureCondition - predicate over the fetched resource
      Returns:
      this waiter
    • timeout

      public ResourceWaiter<T> timeout(Duration timeout)
      Maximum total time to wait (default 5 minutes). The final sleep is clamped to the remaining budget, so await() never overruns the timeout by a full poll interval.
      Parameters:
      timeout - the overall timeout; must not be negative
      Returns:
      this waiter
    • pollInterval

      public ResourceWaiter<T> pollInterval(Duration pollInterval)
      Delay between polls (default 5 seconds). Use Duration.ZERO to poll without sleeping.
      Parameters:
      pollInterval - the inter-poll delay; must not be negative
      Returns:
      this waiter
    • clock

      public ResourceWaiter<T> clock(Clock clock)
      The clock the timeout is measured on (default: a monotonic clock). Only differences between readings are used.
      Parameters:
      clock - the time source (must not be null)
      Returns:
      this waiter
    • await

      public T await()
      Polls until the success condition holds (returning the resource), the failure condition holds (WaiterFailedException), or the timeout elapses (WaiterTimeoutException). Each sleep is clamped to the remaining timeout budget, so the total wait never exceeds the configured timeout by more than one final fetch.

      If the polling thread is interrupted while sleeping, a plain WaiterException is thrown and the thread's interrupt flag is restored.

      Returns:
      the resource once the success condition is met