CompletableFuture Chain (Monad) I wrote an application that has a modular structure. Each module communicates with different remote servers. Time for execution of a task in each module varies. So communication should be asynchron. 

Plus some restrictions like flexibility, scalability and so on are put.

So i decided to use CompletableFutures to build a composition of functions (monade), that modules execute. 

Each module has its own FixedThreadPool und a public function, that create and sends back a CompletableFuture. Something like that:

private final ExecutorService executorService = Executors.newFixedThreadPool(10);

public CompletableFuture&lt;String&gt; function1(final String rawReport, final UUID id) {
     LOG.info("SEND a report for " + id);
     return CompletableFuture.supplyAsync(() -&gt; run(rawReport, id), this.executorService);
}

private String run(final String rawReport, final UUID id) {
     // do something
}


The whole process through all modules is handled by ModuleChainHandler:

 public CompletableFuture&lt;Result&gt; handler(final RunnerJob job) {
        // @formatter:off
        return this.scanExecuterRegistry.function1(job).
                thenCompose(bArr -&gt; this.module2.function2(bArr, job)).
                thenCompose(metrics -&gt; this.module3.function3(metrics, job)).
                handle((vReport, throwable) -&gt; {
                    if (vReport != null) {    
                        this.module4.function4(vReport, job);                        
                        return builder().setResult(vReport).build();
                    } else {                            
                        return builder().setErrorMessage(throwable.getMessage()).build();
                    }
                });
        // @formatter:on
    }


The BIGGEST problem (at the beginning) was to add an error handling. Because you cant throw an exception directly out of a future. I had to create an object with two state (result and error). If a completable future completes exceptionally, will be one action executed and the result object has an error massage and an empty result. 

There is no so much real examples in internet except some poor tutorials. So just interesting, if you do it in the same way.
One thing that i'd like to propose is to replace CompletableFuture with home-brewed builder class that wraps CompletableFuture but will have more 'easy to read' interface. It would be something like this:

class FutureBuilder {

/**
 * Wrapped 'completable future' instance
 */
CompletableFuture future;

...
public FutureBuilder execute(Function&lt;...&gt; task) {...}
public FutureBuilder whenComplete(Consumer&lt;Report&gt; handler) {...}
public FutureBuilder whenFail(BiComsumer&lt;Report, Throwable&gt; handler) {...}
...

/**
 * This method should be called at the end to meet interface requirements
 */
public CompletableFuture build() {
      return future;
}
...
}


So, it may be used in next way:

public CompletableFuture&lt;Result&gt; handler(final RunnerJob job) {
   return new FutureBuilder ()
              .execute(bArr -&gt; this.module2.function2(bArr, job))
              .execute(metrics -&gt; this.module3.function3(metrics, job))
              .whenComplete(r-&gt; uilder().setResult(r).build())
              .whenFail((r, e) -&gt; { 
                   this.module4.function4(r, job);
                   return builder().setErrorMessage(e.getMessage()).build();
              })
              .build(); 
}


I hope that is you are looking for.