Skip to content

Async Execution

RuleSets and RuleFlows can be executed asynchronously using CompletableFuture — both implement the AsyncRunnable interface (new in 2.0.0). This enables non-blocking rule evaluation and parallel execution, leveraging the ExecutorService configured in the RuleContext.

The recommended way to run rules asynchronously is the RuleFlow async pipeline — asyncRun(...) launches steps in parallel and await / awaitAll / awaitAny coordinate them, with binding isolation and exception handling built in:

RuleFlow<RuleContext> flow = RuleFlow.builder()
.name("ParallelChecks")
.bind(price -> BigDecimal.ZERO) // declared before the step: the task can setValue it
.asyncRun(pricingRuleSet, spec -> spec
.as("pricingFuture")) // INHERITED (default) — runs on a fork of the flow's context
.asyncRun(inventoryRuleSet, spec -> spec
.as("inventoryFuture")
.withImmutableBindings()) // IMMUTABLE — read-only snapshot
.awaitAll("pricingFuture", "inventoryFuture")
.build();
flow.run(order -> myOrder);

Every async step runs under an AsyncContextMode. The default, INHERITED, is new in 2.2.0: when the step executes, the flow’s context is forked and the task, its thenRun continuation and its onException handler run on the fork. The fork shares every scope the flow had at that moment, by reference — values set on those bindings by either side are seen by both — but everything structural is private: the bindings and scopes the task adds are never seen by the flow, and the scopes the flow pushes or pops afterwards do not affect the task. A RuleSet or nested flow, which pushes scopes of its own, is therefore safe to launch asynchronously, also several at once. IMMUTABLE (withImmutableBindings()) and CUSTOM (withContext(ctx)) are unchanged.

The old default, SHARED, is removed. It ran the task on the flow’s own scope stack from another thread, so any task that pushed a scope raced with the flow; nothing in the builder selected it except the default, which INHERITED now covers. The task’s result reaches the flow through the future, or through a binding the flow declared before the step and the task sets with setValue — see Scoped Bindings for what a fork shares and what it keeps to itself.

By default, rulii uses a fixed thread pool sized to the number of available processors. This provides sensible concurrency without requiring explicit configuration.

For one-off cases, runAsync(...) is available directly on a RuleSet or RuleFlow:

RuleSet<?> ruleSet = RuleSet.builder()
.with("AsyncRules")
.rule(/* ... rules ... */)
.build();
Bindings bindings = Bindings.builder().standard();
bindings.bind("name", "Alice");
// Run asynchronously
CompletableFuture<?> future = ruleSet.runAsync(bindings);
// Wait for result
future.join();
// Or use callbacks
future.thenAccept(result -> {
System.out.println("Rules completed!");
});

(new in 2.2.0) runAsync(...) forks the given context on the calling thread, before the work is submitted, and runs the RuleSet or RuleFlow on the fork. The fork captures the caller’s scopes as they stand at that moment and shares their bindings by reference, so a caller that keeps using its context while the future runs is safe, and the result also comes back through any binding the caller declared beforehand and the rules setValue. Bindings the rules create live in the fork and are not seen by the caller — read them off the future’s result instead.

You can provide your own ExecutorService to control the thread pool size and behavior:

ExecutorService executor = Executors.newFixedThreadPool(4);
RuleContext ctx = RuleContext.builder()
.executorService(executor)
.build(bindings);
CompletableFuture<?> future = ruleSet.runAsync(ctx);
  • Parallel Rule Sets Run independent rule sets concurrently for faster evaluation
  • Non-Blocking Validation Validate inputs without blocking the calling thread