The following code executes 3 methods sequentially using CompletableFuture.
ExecutorService executor = Executors.newSingleThreadExecutor();
// Various handlers
ModelHandler modelHandler = new ModelHandler();
PermissionHandler permissionHandler = new PermissionHandler();
RemoteClient remoteClient = new RemoteClient();
Runnable getFacadeMetadata = () -> modelHandler.getMetadata("myMethod");
Runnable isUserAuthorized = () -> permissionHandler.isUserAuthorized("user");
Runnable invoke = () -> remoteClient.invoke();
CompletableFuture.runAsync(getMetadata, executor)
.thenRunAsync(isUserAuthorized, executor)
.thenRunAsync(invoke, executor);
Currently they don't return values and supply them as parameters to the next calls.
How should the CompletableFuture chain be written so that the first two methods can return data to be consumed by later methods?
Example:
String metadata = modelHandler.getMetadata("myMethod");
boolean authorized = permissionHandler.isUserAuthorized("user", metadata); // metadata passed
remoteClient.invoke(metadata, authorized); // metadata and authorized passed