Class ResourceWaiter<T>
java.lang.Object
com.eqixiac.equinix.core.waiter.ResourceWaiter<T>
- Type Parameters:
T- the resource type
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 Summary
Modifier and TypeMethodDescriptionawait()Polls until the success condition holds (returning the resource), the failure condition holds (WaiterFailedException), or the timeout elapses (WaiterTimeoutException).The clock the timeout is measured on (default: a monotonic clock).An optional terminal failure condition; if it holds,await()throwsWaiterFailedExceptionimmediately rather than polling to timeout.static <T> ResourceWaiter<T> forResource(Supplier<T> fetch) Begins a waiter that re-fetches the resource via the given supplier (e.g.pollInterval(Duration pollInterval) Delay between polls (default 5 seconds).Maximum total time to wait (default 5 minutes).The success condition;await()returns once it holds.
-
Method Details
-
forResource
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
The success condition;await()returns once it holds. Required.- Parameters:
successCondition- predicate over the fetched resource- Returns:
- this waiter
-
failWhen
An optional terminal failure condition; if it holds,await()throwsWaiterFailedExceptionimmediately rather than polling to timeout.- Parameters:
failureCondition- predicate over the fetched resource- Returns:
- this waiter
-
timeout
Maximum total time to wait (default 5 minutes). The final sleep is clamped to the remaining budget, soawait()never overruns the timeout by a full poll interval.- Parameters:
timeout- the overall timeout; must not be negative- Returns:
- this waiter
-
pollInterval
Delay between polls (default 5 seconds). UseDuration.ZEROto poll without sleeping.- Parameters:
pollInterval- the inter-poll delay; must not be negative- Returns:
- this waiter
-
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 benull)- Returns:
- this waiter
-
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
WaiterExceptionis thrown and the thread's interrupt flag is restored.- Returns:
- the resource once the success condition is met
-