How do I orchestrate async tasks

I don’t have a Java background so maybe what I’m asking is more of a Java question than an Ignition question.

How do I handle a collection of async calls? I have a bunch of API calls to make and I want to use system.net.httpClient().getAsync every x minutes. The individual calls are unrelated other than the desired frequency.

How do I orchestrate the calls? I’d like to know when everything has completed.

C# Task.WhenAll() is what I have in mind. Don’t know the equivalent concept in Ignition / Java.

Reference: Task.WhenAll Method (System.Threading.Tasks) | Microsoft Learn

It's possible that this could be of use to you:

The key is that the promise is a thin wrapper around a Java CompletableFuture. Which implements the CompletionStage interface, that has numerous ways to combine results with further actions. The promise wrapper exposes the most import one: .whenComplete(), that takes a function of two arguments. This is the BiConsumer<> async interface. You should create a class implementing BiConsumer with the tracking/counting/accounting logic you wish, and attach it to each promise.

The most equivalent would probably be CompletableFuture.allOf, using the underlying CompletableFuture on the Promise as Phil already called out.

The downside to .allOf() is that you can't construct it incrementally. A well-designed consumer class could accept more promises while earlier ones are running and completing. Such a class can also process the results as it goes.

1 Like

I recently needed to create some scripting to execute a task x times in parallel, but block the script from moving on until they completed.

Here is the script incase there's anything useful for you in it.

1 Like