/** * Invokes tasks in a thread pool, then unwraps the resulting {@link Future Futures}. * * <p>Any task exception is wrapped in {@link IOException}. */ private <T> List<T> callTasks(Collection<Callable<T>> tasks) throws IOException { try { List<CompletionStage<T>> futures = new ArrayList<>(tasks.size()); for (Callable<T> task : tasks) { futures.add(MoreFutures.supplyAsync(task::call, executorService)); } return MoreFutures.get(MoreFutures.allAsList(futures)); } catch (ExecutionException e) { if (e.getCause() != null) { if (e.getCause() instanceof IOException) { throw (IOException) e.getCause(); } throw new IOException(e.getCause()); } throw new IOException(e); } catch (InterruptedException e) { Thread.currentThread().interrupt(); throw new IOException("executor service was interrupted"); } } }
private static void executeBatches(List<BatchRequest> batches) throws IOException { ExecutorService executor = MoreExecutors.listeningDecorator( new ThreadPoolExecutor( MAX_CONCURRENT_BATCHES, MAX_CONCURRENT_BATCHES, 0L, TimeUnit.MILLISECONDS, new LinkedBlockingQueue<>())); List<CompletionStage<Void>> futures = new ArrayList<>(); for (final BatchRequest batch : batches) { futures.add(MoreFutures.runAsync(() -> batch.execute(), executor)); } try { MoreFutures.get(MoreFutures.allAsList(futures)); } catch (InterruptedException e) { Thread.currentThread().interrupt(); throw new IOException("Interrupted while executing batch GCS request", e); } catch (ExecutionException e) { if (e.getCause() instanceof FileNotFoundException) { throw (FileNotFoundException) e.getCause(); } throw new IOException("Error executing batch GCS request", e); } finally { executor.shutdown(); } }
/** * Stages the given artifact files to the staging service. * * @return The artifact staging token returned by the service */ public String stage(String stagingSessionToken, Collection<StagedFile> files) throws IOException, InterruptedException { final Map<StagedFile, CompletionStage<ArtifactMetadata>> futures = new HashMap<>(); LOG.info("Staging {} files (token: {})", files.size(), stagingSessionToken); for (StagedFile file : files) { futures.put( file, MoreFutures.supplyAsync(new StagingCallable(stagingSessionToken, file), executorService)); } CompletionStage<StagingResult> stagingResult = MoreFutures.allAsList(futures.values()) .thenApply(ignored -> new ExtractStagingResultsCallable(futures).call()); return stageManifest(stagingSessionToken, stagingResult); }
MoreFutures.allAsList(destinationPackages); boolean finished = false; do {