Class Worker
- All Implemented Interfaces:
AutoCloseable
The worker opens outbound pull streams to Dex. It dispatches
invocations on a bounded JVM executor.
User methods are ordinary blocking Java methods; slow external I/O occupies a handler thread and
should respect the configured Step or RPC timeout. Call start() on a dedicated application
thread because it blocks until the worker stops. A worker can be started only once. The supplied
BlobCache is borrowed and remains the caller's responsibility.
Worker worker = new Worker(registry, blobCache, WorkerOptions.newBuilder()
.serverAddress("dex:8801")
.build());
Runtime.getRuntime().addShutdownHook(new Thread(worker::stop));
worker.start();
-
Constructor Summary
Constructors -
Method Summary
Modifier and TypeMethodDescriptionvoidclose()Stops the worker and permanently closes its lifecycle.Returns an independent snapshot for pull Worker readiness probes.longReturns cumulative Step declines caused by reserved RPC capacity for this Worker.voidstart()Starts the worker pull streams and blocks until termination.voidstop()Gracefully stops the pull streams, handler executor, and server channel.voidwaitPullRegistered(Duration timeout) Waits until every pull queue has received a Registered acknowledgement at least once.
-
Constructor Details
-
Worker
Creates a worker using local development defaults.- Parameters:
registry- the nonnull registry of Flow implementationsblobCache- the nonnull cache used to hydrate blob-backed invocation values- Throws:
IllegalArgumentException- if either argument isnull
-
Worker
Creates a worker with explicit polling, connection, and serialization options.- Parameters:
registry- the nonnull registry of Flow implementationsblobCache- the nonnull cache used to hydrate blob-backed invocation valuesoptions- the nonnull worker options- Throws:
IllegalArgumentException- if any argument isnullor the pull Worker ID conflicts with a registered Flow type
-
-
Method Details
-
start
public void start()Starts the worker pull streams and blocks until termination.Only a newly created worker may start. It negotiates the Server protocol before syncing Attribute indexes and opening the pull streams. If the waiting thread is interrupted, the worker stops and restores the thread's interruption status before returning.
- Throws:
PullRegistrationError- if an uncovered permanent stream rejection stops the Worker with stopOnPermanentStreamRejection enabled, before or after readinessIllegalStateException- if the worker was already started or startup fails
-
waitPullRegistered
Waits until every pull queue has received a Registered acknowledgement at least once.Start the Worker on another thread. Multiple callers may wait independently, before or after start. Success latches even after stop; use getPullRegistrationState for later probes. Before readiness, a permanent rejection fails this wait only if that queue has no other registered stream. Startup failures are propagated to waiters. Interrupting this wait leaves the Worker running.
Thread workerThread = new Thread(worker::start); workerThread.start(); worker.waitPullRegistered(Duration.ofSeconds(30)); boolean isReady = worker.getPullRegistrationState().isReady();- Parameters:
timeout- the nonnull, nonnegative maximum wait duration- Throws:
InterruptedException- if the waiting thread is interruptedTimeoutException- if the timeout expires before readinessPullRegistrationError- if a queue is permanently rejected before readinessIllegalArgumentException- if timeout is null or negativeIllegalStateException- if startup fails or the Worker stops before readiness
-
getPullReservedRpcDeclines
public long getPullReservedRpcDeclines()Returns cumulative Step declines caused by reserved RPC capacity for this Worker.The counter counts decline events. Dex may decline the same Step dispatch repeatedly while retrying it, so read the counter as a rate. Full-capacity rejections and unknown names are excluded. The count never resets, remains available after stop, and is safe to read concurrently with handlers. Export this value to a monitoring counter:
long declines = worker.getPullReservedRpcDeclines();- Returns:
- the cumulative reserve-decline count
-
getPullRegistrationState
Returns an independent snapshot for pull Worker readiness probes.After initial readiness, transient failures, reconnects, and stop keep readiness true. A permanent rejection makes it false only while that queue has no registered stream. Re-registration clears the error. Safe concurrently with start, stop, and registration waits. See waitPullRegistered for a startup example.
- Returns:
- immutable queue state
-
stop
public void stop()Gracefully stops the pull streams, handler executor, and server channel.The method is idempotent. The Worker drains for the configured stop timeout, then cancels remaining handlers and waits for them to return. A handler ignoring cancellation can block Stop. Interruption status is preserved.
-
close
public void close()Stops the worker and permanently closes its lifecycle.The application owns any supplied
Credentialand the borrowedBlobCache.- Specified by:
closein interfaceAutoCloseable
-