@Override public StagingResult call() { Set<ArtifactMetadata> metadata = new HashSet<>(); Map<StagedFile, Throwable> failures = new HashMap<>(); for (Entry<StagedFile, CompletionStage<ArtifactMetadata>> stagedFileResult : futures.entrySet()) { try { metadata.add(MoreFutures.get(stagedFileResult.getValue())); } catch (ExecutionException ee) { failures.put(stagedFileResult.getKey(), ee.getCause()); } catch (InterruptedException ie) { throw new AssertionError( "This should never happen. " + "All of the futures are complete by construction", ie); } } if (failures.isEmpty()) { return StagingResult.success(metadata); } else { return StagingResult.failure(failures); } } }
/** * 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(); } }
public DataflowPackage stageToFile( byte[] bytes, String target, String stagingPath, CreateOptions createOptions) { try { return MoreFutures.get( stagePackage( PackageAttributes.forBytesToStage(bytes, target, stagingPath), DEFAULT_SLEEPER, createOptions)) .getPackageAttributes() .getDestination(); } catch (InterruptedException e) { Thread.currentThread().interrupt(); throw new RuntimeException("Interrupted while staging pipeline", e); } catch (ExecutionException e) { throw new RuntimeException("Error while staging pipeline", e.getCause()); } }
@Test public void runAsyncSuccess() throws Exception { AtomicInteger result = new AtomicInteger(0); CompletionStage<Void> sideEffectFuture = MoreFutures.runAsync( () -> { result.set(42); }); MoreFutures.get(sideEffectFuture); assertThat(result.get(), equalTo(42)); }
@Test public void supplyAsyncSuccess() throws Exception { CompletionStage<Integer> future = MoreFutures.supplyAsync(() -> 42); assertThat(MoreFutures.get(future), equalTo(42)); }
throws InterruptedException { try { StagingResult stagingResult = MoreFutures.get(stagingFuture); if (stagingResult.isSuccess()) { LOG.info(
@Test public void supplyAsyncFailure() throws Exception { final String testMessage = "this is just a test"; CompletionStage<Long> future = MoreFutures.supplyAsync( () -> { throw new IllegalStateException(testMessage); }); thrown.expect(ExecutionException.class); thrown.expectCause(isA(IllegalStateException.class)); thrown.expectMessage(testMessage); MoreFutures.get(future); }
@Test public void runAsyncFailure() throws Exception { final String testMessage = "this is just a test"; CompletionStage<Void> sideEffectFuture = MoreFutures.runAsync( () -> { throw new IllegalStateException(testMessage); }); thrown.expect(ExecutionException.class); thrown.expectCause(isA(IllegalStateException.class)); thrown.expectMessage(testMessage); MoreFutures.get(sideEffectFuture); } }