Skip to content

Commit

Permalink
Complex command invocation structures #131
Browse files Browse the repository at this point in the history
* doesn't look like CompletableFuture buys us anything, as we do all the combining
  of tasks ourselves. Using regular ExecutorService/Future
  • Loading branch information
andrus committed Nov 18, 2017
1 parent 3fd7c41 commit f844016
Showing 1 changed file with 7 additions and 10 deletions.
17 changes: 7 additions & 10 deletions bootique/src/main/java/io/bootique/command/MultiCommand.java
Expand Up @@ -8,10 +8,10 @@
import java.util.ArrayList; import java.util.ArrayList;
import java.util.Collection; import java.util.Collection;
import java.util.List; import java.util.List;
import java.util.concurrent.CompletableFuture; import java.util.concurrent.Callable;
import java.util.concurrent.ExecutionException; import java.util.concurrent.ExecutionException;
import java.util.concurrent.ExecutorService; import java.util.concurrent.ExecutorService;
import java.util.function.Supplier; import java.util.concurrent.Future;


/** /**
* A composite command made of the main command and auxiliary commands run before the main command or in parallel with it. * A composite command made of the main command and auxiliary commands run before the main command or in parallel with it.
Expand Down Expand Up @@ -62,7 +62,7 @@ public CommandOutcome run(Cli cli) {
return mainCommand.run(cli); return mainCommand.run(cli);
} }


private CommandOutcome waitForOutcome(CompletableFuture<CommandOutcome> invocation) { private CommandOutcome waitForOutcome(Future<CommandOutcome> invocation) {
try { try {
return invocation.get(); return invocation.get();
} catch (InterruptedException e) { } catch (InterruptedException e) {
Expand All @@ -75,19 +75,16 @@ private CommandOutcome waitForOutcome(CompletableFuture<CommandOutcome> invocati
} }
} }


private Collection<CompletableFuture<CommandOutcome>> invokeInParallel(Collection<CommandWithArgs> cmdWithArgs) { private Collection<Future<CommandOutcome>> invokeInParallel(Collection<CommandWithArgs> cmdWithArgs) {
ExecutorService executor = getExecutor(); ExecutorService executor = getExecutor();
List<CompletableFuture<CommandOutcome>> outcomes = new ArrayList<>(cmdWithArgs.size());


cmdWithArgs.forEach(cwa -> { List<Future<CommandOutcome>> outcomes = new ArrayList<>(cmdWithArgs.size());
Supplier<CommandOutcome> invocation = toInvocation(cwa); cmdWithArgs.forEach(cwa -> outcomes.add(executor.submit(toInvocation(cwa))));
outcomes.add(CompletableFuture.supplyAsync(invocation, executor));
});


return outcomes; return outcomes;
} }


private Supplier<CommandOutcome> toInvocation(CommandWithArgs cmdWithArgs) { private Callable<CommandOutcome> toInvocation(CommandWithArgs cmdWithArgs) {


CommandManager commandManager = getCommandManager(); CommandManager commandManager = getCommandManager();
String commandName = cmdWithArgs.getName(commandManager); String commandName = cmdWithArgs.getName(commandManager);
Expand Down

0 comments on commit f844016

Please sign in to comment.