◐ Shell
clean mode source ↗

Allow scheduling of CF completion by bbakerman · Pull Request #275 · graphql-java/java-dataloader

The completion of CF promises can now be scheduled on another thread

A step in the batch loading process is the completion of the futures that have been promised earlier via
Dataloader.load(). This can also be scheduled on another thread if need be, allowing the dispatch call
to return quicker.

You might want to schedule the completion of values on another thread if there is following work t do such as :

var cfStage1 = dataLoader.load(key);
var cfStage2 = cfStage1.thenApply(v -> doSomethingSlow(v));
//...
var dispatchCF = dataLoader.dispatch();

In the above example the .doSomethingSlow(v) call will happen inside the completion
of the cfStage1 code path since CompletableFuture by design eagerly runs dependent chained methods.

Perhaps you want this completion to happen more asynchronously so that the .dispatch() returns more
quickly and is not bound to the .doSomethingSlow(v) call.

By default, the dispatch completion is done on the current thread in a synchronous manner, which will include
any extra CompletableFuture chained methods.

This is an example of running the completion step in an asynchronous manner :

@Override
public <K> CompletionStage<?> scheduleCompletion(Runnable completeValuesRunnable, List<K> keys, BatchLoaderEnvironment environment) {
    return CompletableFuture.runAsync(completeValuesRunnable);
}