Package org.jboss.elemento.flow
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:
- A stack for passing data between tasks using
FlowContext.push(Object)andFlowContext.pop() - A map for named data storage using
FlowContext.set(String, Object)andFlowContext.get(String) - Progress tracking via
Progress - Status information about execution success, failure, or timeout
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:
-
ClassDescriptionAn interface to execute a list of asynchronous tasks in parallel or sequentially, or to execute a single task repeatedly as long as certain conditions are met.FlowCallback<C extends FlowContext>A callback for the outcome of the execution of asynchronous tasks.General purpose context to be used as a common data structure when executing a list of asynchronous tasks in parallel, in sequence or when executing a task repeatedly while a condition evaluates to
true.Enum for the execution status of a flow method.ParallelTasks<C extends FlowContext>A task implementation that executes a list of asynchronous tasks in parallel.Interface to reflect the progress when executing a list of asynchronous tasks in parallel, in sequence or repeatedly as long as certain conditions are met.Promisable<C extends FlowContext>An interface to use the promise when executing asynchronous tasks.Repeat<C extends FlowContext>An interface to control the repeated execution of an asynchronous task.RepeatTask<C extends FlowContext>A task implementation that executes a task as long as certain conditions are met.Sequence<C extends FlowContext>SequentialTasks<C extends FlowContext>A task implementation that executes a list of asynchronous tasks in order.Subscription<C extends FlowContext>An interface to subscribe to the outcome of the execution of asynchronous tasks.Task<C extends FlowContext>Interface for the execution of an asynchronous task.