List of usage examples for com.google.common.util.concurrent ForwardingService ForwardingService
ForwardingService
From source file:com.griddynamics.jagger.master.AbstractDistributor.java
@Override public Service distribute(final ExecutorService executor, final String sessionId, final String taskId, final Multimap<NodeType, NodeId> availableNodes, final Coordinator coordinator, final T task, final DistributionListener listener) { Set<Qualifier<?>> qualifiers = getQualifiers(); final Map<NodeId, RemoteExecutor> remotes = Maps.newHashMap(); for (NodeId nodeId : availableNodes.get(NodeType.KERNEL)) { boolean canRunTheCommand = coordinator.canExecuteCommands(nodeId, qualifiers); if (canRunTheCommand) { remotes.put(nodeId, coordinator.getExecutor(nodeId)); } else {// w w w .j av a 2 s . c o m log.debug("command type {} are not supported by kernel {}", qualifiers, nodeId); } } if (remotes.isEmpty()) { throw new NodeNotFound("Nodes not found to distribute the task"); } final Service service = performDistribution(executor, sessionId, taskId, task, remotes, availableNodes, coordinator); return new ForwardingService() { @Override public ListenableFuture<State> start() { ListenableFuture<Nothing> runListener = Futures .makeListenable(executor.submit(new Callable<Nothing>() { @Override public Nothing call() { listener.onDistributionStarted(sessionId, taskId, task, remotes.keySet()); return Nothing.INSTANCE; } })); return Futures.chain(runListener, new Function<Nothing, ListenableFuture<State>>() { @Override public ListenableFuture<State> apply(Nothing input) { return doStart(); } }); } private ListenableFuture<State> doStart() { return super.start(); } @Override protected Service delegate() { return service; } @Override public ListenableFuture<State> stop() { ListenableFuture<State> stop = super.stop(); return Futures.chain(stop, new Function<State, ListenableFuture<State>>() { @Override public ListenableFuture<State> apply(final State input) { final SettableFuture<State> result = SettableFuture.create(); executor.execute(new Runnable() { @Override public void run() { try { listener.onTaskDistributionCompleted(sessionId, taskId, task); } finally { result.set(input); } } }); return result; } }); } }; }