How to write while loop with completionStage which eventually returns CompletionStage without join?

734 views Asked by At

I have to write a function that returns CompletionStage<Void>. This function can query paginated data(async operation, returns data wrapped in CompletionStage) and process them and query the next set of data until it finishes processing the entire set of data. Is there a way I can use a while loop with the completion stage without using join?

public CompletionStage<Void> process(Input){
    while(allDataProcessed){
       queryDataAndProcess(This itself is async function)
    }
}
1

There are 1 answers

0
Davide D'Alto On
public CompletionStage<Void> process(Input) {
     CompletableStage<Void> loop = CompletableFuture.completedFuture(null);
     while(allDataProcessed) {
        loop = loop.thenCompose( unusued -> queryDataAndProcess(...) );
     }
     return loop;
}

The drawback of this solution is that it might create many object in memory. To avoid this issues you could use the IBM async utils:

public CompletionStage<Void> process(Input) {
    return AsyncTrampoline.asyncWhile( () -> 
        queryDataAndProcess(...).thenApply( r -> allDataProcessed )
    )
}

With Maven, you can add it to your project with:

<dependency>
    <groupId>com.ibm.async</groupId>
    <artifactId>asyncutil</artifactId>
    <version>0.1.0</version>
</dependency>

This library implements the Trampoline pattern