Package org.jboss.elemento.flow


package org.jboss.elemento.flow
Provides an API for executing asynchronous tasks in parallel, sequentially, or repeatedly based on conditions.

Overview

The flow package enables coordinated execution of multiple asynchronous tasks that share a common context. Tasks can be executed in parallel, sequentially, or repeatedly while conditions are met. The context provides a stack and map for data sharing between tasks, along with progress tracking.

Parallel Execution

Execute multiple tasks simultaneously using Flow.parallel(FlowContext, java.util.List):

import static org.jboss.elemento.flow.Flow.parallel;

// datetime format is "2022-03-31T11:03:39.348365+02:00"
Task<FlowContext> currentTime = context -> fetch("https://worldtimeapi.org/api/timezone/Europe/Berlin")
        .then(Response::json)
        .then(json -> Promise.resolve(Js.<JsPropertyMap<String>>cast(json).get("datetime").substring(11, 23)))
        .then(context::resolve);

double ms = 500 + new Random().nextInt(2000);
Task<FlowContext> delay = context -> new Promise<>((res, __) -> setTimeout(___ -> res.onInvoke(context), ms));

// execute the two tasks in parallel
parallel(new FlowContext(), List.of(currentTime, delay))
        .subscribe(context -> console.log("Current time: " + context.pop("n/a")));

Sequential Execution

Execute tasks one after another using Flow.sequential(FlowContext, java.util.List):

import static org.jboss.elemento.flow.Flow.sequential;

Task<FlowContext> currentTime = context -> fetch("https://worldtimeapi.org/api/timezone/Europe/Berlin")
        .then(Response::json)
        .then(json -> Promise.resolve(Js.<JsPropertyMap<String>>cast(json).get("datetime").substring(11, 23)))
        .then(context::resolve);

double ms = 500 + new Random().nextInt(2_000);
Task<FlowContext> delay = context -> new Promise<>((res, __) -> setTimeout(___ -> res.onInvoke(context), ms));

// execute the two tasks in sequence and cancel after 1_000 ms
sequential(new FlowContext(), List.of(currentTime, delay))
        .timeout(1_000)
        .subscribe(context -> console.log("Current time: " + context.pop("n/a")));

Repeated Execution

Execute a task repeatedly while a condition is met using Flow.repeat(FlowContext, Task):

import static org.jboss.elemento.flow.Flow.repeat;

Task<FlowContext> currentTime = context -> fetch("https://worldtimeapi.org/api/timezone/Europe/Berlin")
        .then(Response::json)
        .then(json -> Promise.resolve(Js.<JsPropertyMap<String>>cast(json).get("datetime").substring(11, 23)))
        .then(context::resolve);

// fetch the current time until the milliseconds end with "0" and cancel after 5 iterations
repeat(new FlowContext(), currentTime)
        .while_(context -> !context.pop("").endsWith("0"))
        .iterations(5)
        .subscribe(context -> console.log("Current time: " + context.pop("n/a")));

Flow Context

The FlowContext serves as a shared data structure between tasks, providing:

Tasks can use convenience methods to resolve or reject promises:

Task<FlowContext> task = context -> {
    // Push value and resolve
    return context.resolve("result");

    // Store value and resolve
    // return context.resolve("key", "value");

    // Reject with error
    // return context.reject("error message");
};

Control Options

Both Sequence (for parallel/sequential) and Repeat provide control options:

  • Fail fast vs. fail last - Control whether execution stops on first failure or continues
  • Timeout - Set maximum execution time (sequential and repeat only)
  • Interval - Set delay between iterations (repeat only)
  • Iterations - Set maximum number of iterations (repeat only)
  • While predicate - Continue while condition is true (repeat only)

Subscription

Subscribe to execution results using Subscription.subscribe(FlowCallback):

Flow.parallel(context, tasks)
        .subscribe(ctx -> {
            if (ctx.isSuccessful()) {
                console.log("Success: " + ctx.pop());
            } else if (ctx.failure()) {
                console.log("Failed: " + ctx.failureReason());
            } else if (ctx.isTimeout()) {
                console.log("Timeout!");
            }
        });
See Also: