public CompletableFuture<List<Task>> describe() { return new PaginatedResponseIterator<>(ListTasksResponse::nextToken, this::list) .stream() .map(ListTasksResponse::taskArns) .filter(arns -> !arns.isEmpty()) .map(this::describeTasks) .collect(CompletableFutures.joinList()) .thenApply(this::extractTasksFromResponses); }
public CompletableFuture<List<ContainerInstance>> describe() { return new PaginatedResponseIterator<>(ListContainerInstancesResponse::nextToken, this::list) .stream() .map(ListContainerInstancesResponse::containerInstanceArns) .filter(arns -> !arns.isEmpty()) .map(this::describeContainerInstances) .collect(CompletableFutures.joinList()) .thenApply(this::extractContainerInstancesFromResponses); }
@Override @SneakyThrows // TODO add checked exception handling public Void handleRequest(CloudWatchEvent<Map> input, Context context) { log.debug("Reconciler request: {}", input); ListClustersResponse r = dataService.listClusters(ListClustersRequest.builder().build()); List<Cluster> clusters = r.getClusters(); clusters .stream() .map(c -> stateFunction.triggerAsync(new ManagerInput(c))) .collect(CompletableFutures.joinList()) .join(); return null; } }
@Override @SneakyThrows // TODO add checked exception handling public ManagerOutput handleRequest(ManagerInput input, Context context) { log.debug("Manager request: {}", input); ListEnvironmentsResponse r = data.listEnvironments( ListEnvironmentsRequest.builder().cluster(input.getCluster()).build()); List<EnvironmentId> environments = r.getEnvironmentIds(); ClusterSnapshot state = ecs.snapshotState(input.getCluster().getClusterName()); Stream<CompletableFuture<SchedulerOutput>> pendingRequests = environments .stream() .map(environmentId -> scheduler.callAsync(new SchedulerInput(state, environmentId))); List<SchedulerOutput> outputs = pendingRequests.collect(CompletableFutures.joinList()).join(); return new ManagerOutput(input.getCluster(), outputs); } }
actions.stream().map(a -> a.execute(ecs)).collect(CompletableFutures.joinList()).join();
/** * Returns a new {@link CompletableFuture} which completes to a list of values of those input * stages that succeeded. The list of results is in the same order as the input stages. For failed * stages, the defaultValueMapper will be called, and the value returned from that function will * be put in the resulting list. * * <p>If no stages are provided, returns a future holding an empty list. * * @param stages the stages to combine. * @param defaultValueMapper a function that will be called when a future completes exceptionally * to provide a default value to place in the resulting list * @param <T> the common type of all of the input stages, that determines the type of the * output future * @return a future that completes to a list of the results of the supplied stages * @throws NullPointerException if the stages list or any of its elements are {@code null} */ public static <T> CompletableFuture<List<T>> successfulAsList( List<? extends CompletionStage<T>> stages, Function<Throwable, ? extends T> defaultValueMapper) { return stages.stream() .map(f -> f.exceptionally(defaultValueMapper)) .collect(joinList()); }
/** * Returns a new {@link CompletableFuture} which completes to a list of values of those input * stages that succeeded. The list of results is in the same order as the input stages. For failed * stages, the defaultValueMapper will be called, and the value returned from that function will * be put in the resulting list. * * <p>If no stages are provided, returns a future holding an empty list. * * @param stages the stages to combine. * @param defaultValueMapper a function that will be called when a future completes exceptionally * to provide a default value to place in the resulting list * @param <T> the common type of all of the input stages, that determines the type of the * output future * @return a future that completes to a list of the results of the supplied stages * @throws NullPointerException if the stages list or any of its elements are {@code null} */ public static <T> CompletableFuture<List<T>> successfulAsList( List<? extends CompletionStage<T>> stages, Function<Throwable, ? extends T> defaultValueMapper) { return stages.stream() .map(f -> f.exceptionally(defaultValueMapper)) .collect(joinList()); }