Clients
Start New Workflows
Asynchronous dispatch
The asynchronous dispatch of a new workflow starts a new instance without waiting for its completion:
We can start a new workflow using this stub:
// Stub of a new HelloWorkflow workflow
HelloWorkflow w = client.newWorkflow(HelloWorkflow.class);
// synchronous dispatch of a new workflow
Deferred<String> deferred = client.dispatch(w::greet, "Infinitic");
// Stub of a new HelloWorkflow workflow
val w : HelloWorkflow = client.newWorkflow(HelloWorkflow::class.java)
// asynchronous dispatch of a new workflow
val deferred : Deferred<String> = client.dispatch(w::greet, "Infinitic") }
The dispatch
method returns when the ad-hoc message has been received by Pulsar. If we do not want to wait for this, we can use dispatchAsync
instead:
CompletableFuture<Deferred<String>> future =
client.dispatchAsync(w::greet, "Infinitic");
val future: CompletableFuture<Deferred<String>> =
client.dispatchAsync(w::greet, "Infinitic") }
This CompletableFuture returns when the ad-hoc message has been received by Pulsar.
The Deferred<T>
object can be used to:
retrieve the underlying workflow's
id
:String id = deferred.id;
val id: String = deferred.id
wait for the synchronous completion:
T result = deferred.await();
val result: T = deferred.await()
where
T
is the actual return type.The
await()
method blocks the current thread of the client - up to the workflow termination. It will throw anWorkflowUnknownException
if the workflow is already terminated.
Synchronous dispatch
The synchronous dispatch of a new workflow starts a new instance and waits for its completion:
We can dispatch a workflow and wait for its completion synchronously using the stub of a new workflow:
// Stub of a new HelloWorkflow workflow
HelloWorkflow w = client.newWorkflow(HelloWorkflow.class);
// synchronous dispatch of a new workflow
String greeting = w.greet("Infinitic");
// Stub of a new HelloWorkflow workflow
val w : HelloWorkflow = client.newWorkflow(HelloWorkflow::class.java)
// synchronous dispatch of a new workflow
val greeting = w.greet("Infinitic")
When dispatching a workflow, the client serializes parameters and sends them through Pulsar to the [workflow worker, which will orchestrate the workflow. Eventually, the return value will be serialized and sent back to the client through Pulsar.