I am trying to chain tasks in CompletableFuture to execute them in parallel and get the best performance and resource utilization.
The workflow I am writing has five stages and each stage depends on the result of the previous stages.
The workflow execution is as follows:
* Stage-1: Create CustomerContext -> This will call downstream services and return Context object.
* Stage-2: Generate Candidates for the customer -> This stage generate candidates and returns a List<Candidate> object. This stage requires CustomerContext as a parameter to generate the candidates.
* Stage-3: Process Candidate -> In this stage I process the candidates and returns processed List<Candidate>. This stage requires CustomerContext and List<Candidate> object from stage-1 and stage-2
* Stage-4: Rank candidates -> In this stage I perform candidate ranking and returns ranked List<Candidate> object. This stage requires CustomerContext and List<Candidate> object from stage-1 and **stage-3**
* Stage-5: Generate Result -> In this stage I generate the final result and returns Result object by making downstream API calls., This stage requires CustomerContext and List<Candidate> object from **stage-1** and **stage-4**
I can create a result holder object to hold each stage's results. However, I am not sure if that is the best solution.
Is CompletableFuture the best solution for this use case? What would be the optimal way to chain these stages?