The rare async case

Tools that fan out to many downstream services in parallel. You want to await all before returning.

Advertisement

AsyncTool interface

public interface AsyncTool extends Tool {
  CompletableFuture callAsync(Map args);
  default Object call(Map args) { return callAsync(args).join(); }
}






















































































































































Advertisement

Parallel calls

public CompletableFuture callAsync(Map args) {
  var f1 = CompletableFuture.supplyAsync(() -> serviceA.fetch());
  var f2 = CompletableFuture.supplyAsync(() -> serviceB.fetch());
  return f1.thenCombine(f2, (a, b) -> Map.of("a", a, "b", b));
}


        
    




















































































































































Advertisement
Disclaimer  ·  Privacy  ·  Contact