Start A New Workflow

Subscribe here to follow Infinitic's development. Please ⭐️ us on Github!

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 HelloWorld workflow
HelloWorld helloWorld = client.newWorkflow(HelloWorld.class);

// synchronous dispatch of a new workflow
Deferred<String> deferred = client.dispatch(helloWorld::greet, "Infinitic");
// Stub of a new HelloWorld workflow
val helloWorld : HelloWorld = client.newWorkflow(HelloWorld::class.java)

// asynchronous dispatch of a new workflow
val deferred: Deferred<String> = client.dispatch(helloWorld::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(helloWorld::greet, "Infinitic");
val future: CompletableFuture<Deferred<String>> = client.dispatchAsync(helloWorld::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 an UnknownWorkflowException 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 HelloWorld workflow
HelloWorld helloWorld = client.newWorkflow(HelloWorld.class);

// synchronous dispatch of a new workflow
String greeting = helloWorld.greet("Infinitic");
// Stub of a new HelloWorld workflow
val helloWorld : HelloWorld = client.newWorkflow(HelloWorld::class.java)

// synchronous dispatch of a new workflow
val greeting = helloWorld.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.

Edit this page on GitHub Updated at Sat, Sep 10, 2022