/** * @param eventHandler the {@link EventHandler} for which the actions are to be executed * @param msg the {@link Message} that triggered the event * @return a {@link CompletableFuture} holding a list of {@link EventExecution}s for the {@link Action}s executed in the event handler */ private CompletableFuture<List<EventExecution>> executeActionsForEventHandler(EventHandler eventHandler, Message msg) { List<CompletableFuture<EventExecution>> futuresList = new ArrayList<>(); int i = 0; for (Action action : eventHandler.getActions()) { String id = msg.getId() + "_" + i++; EventExecution eventExecution = new EventExecution(id, msg.getId()); eventExecution.setCreated(System.currentTimeMillis()); eventExecution.setEvent(eventHandler.getEvent()); eventExecution.setName(eventHandler.getName()); eventExecution.setAction(action.getAction()); eventExecution.setStatus(Status.IN_PROGRESS); if (executionService.addEventExecution(eventExecution)) { futuresList.add(CompletableFuture.supplyAsync(() -> execute(eventExecution, action, getPayloadObject(msg.getPayload())), executorService)); } else { logger.warn("Duplicate delivery/execution of message: {}", msg.getId()); } } return CompletableFutures.allAsList(futuresList); }
private <T> CompletionStage<MemcacheStatus> sendToAll(final Request<T> request) { List<CompletionStage<MemcacheStatus>> futures = clients .stream() .map(client -> (CompletionStage<MemcacheStatus>) client.send(request)) .collect(Collectors.toList()); return CompletableFutures.allAsList(futures) .thenApply( ts -> ts.stream() .filter(status -> status != MemcacheStatus.OK) .findFirst() .orElse(MemcacheStatus.OK)); }
@Override public void listRepositories(String projectName, AsyncMethodCallback resultHandler) { handle(allAsList(projectManager.get(projectName).repos().list().entrySet().stream() .map(e -> convert(e.getKey(), e.getValue())) .collect(toList())), resultHandler); }
private CompletionStage<Response<ByteString>> haltActiveBackfillInstances(Backfill backfill, Client client) { return CompletableFutures.allAsList( retrieveBackfillStatuses(backfill).stream() .filter(BackfillResource::isActiveState) .map(RunStateData::workflowInstance) .map(workflowInstance -> haltActiveBackfillInstance(workflowInstance, client)) .collect(toList())) .handle((result, throwable) -> { if (throwable != null || result.contains(Boolean.FALSE)) { return Response.forStatus( Status.INTERNAL_SERVER_ERROR .withReasonPhrase( "some active instances cannot be halted, however no new ones will be triggered")); } else { return Response.ok(); } }); }
private CompletionStage<List<GetResult<V>>> multiget(List<byte[]> keys, int ttl) { final int size = keys.size(); if (size == 0) { return CompletableFuture.completedFuture(Collections.<GetResult<V>>emptyList()); } final List<List<byte[]>> keyPartition = Lists.partition(keys, MemcacheEncoder.MAX_MULTIGET_SIZE); final List<CompletionStage<List<GetResult<byte[]>>>> futureList = new ArrayList<>(keyPartition.size()); for (final List<byte[]> part : keyPartition) { MultigetRequest request = MultigetRequest.create(part, ttl); futureList.add(rawMemcacheClient.send(request)); } final CompletionStage<List<GetResult<byte[]>>> future = CompletableFutures.allAsList(futureList).thenApply(Utils.flatten()); metrics.measureMultigetFuture(future); return transformerUtil.decodeList(future); }
private CompletionStage<List<GetResult<V>>> multiget(List<byte[]> keys, boolean withCas) { final int size = keys.size(); if (size == 0) { return CompletableFuture.completedFuture(Collections.<GetResult<V>>emptyList()); } final List<List<byte[]>> keyPartition = Lists.partition(keys, MemcacheEncoder.MAX_MULTIGET_SIZE); final List<CompletionStage<List<GetResult<byte[]>>>> futureList = new ArrayList<>(keyPartition.size()); for (final List<byte[]> part : keyPartition) { MultigetRequest request = MultigetRequest.create(part, withCas); futureList.add(rawMemcacheClient.send(request)); } final CompletionStage<List<GetResult<byte[]>>> future = ((CompletionStage<List<List<GetResult<byte[]>>>>) CompletableFutures.allAsList(futureList)) .thenApply(Utils.flatten()); metrics.measureMultigetFuture(future); return transformerUtil.decodeList(future); }
.collect(Collectors.toList()); return CompletableFutures.allAsList(predictions); };
private CompletableFuture<List<Chunk>> getChunksForRadius(int radius) { // Get current player's position in chunk coordinates. int chunkX = getPosition().getFloorX() >> 4; int chunkZ = getPosition().getFloorZ() >> 4; // Now get and send chunk data. Set<Vector2i> chunksForRadius = new HashSet<>(); List<CompletableFuture<Chunk>> completableFutures = new ArrayList<>(); for (int x = -radius; x <= radius; x++) { for (int z = -radius; z <= radius; z++) { int newChunkX = chunkX + x, newChunkZ = chunkZ + z; Vector2i chunkCoords = new Vector2i(newChunkX, newChunkZ); chunksForRadius.add(chunkCoords); if (!sentChunks.add(chunkCoords)) { // Already sent, don't need to resend. continue; } completableFutures.add(getLevel().getChunk(newChunkX, newChunkZ)); } } sentChunks.retainAll(chunksForRadius); return CompletableFutures.allAsList(completableFutures); }
.collect(Collectors.toList()); return CompletableFutures.allAsList(predictions); };
CompletableFutures.allAsList(entryFutures).handle((entries, cause) -> { if (cause != null) { future.completeExceptionally(Exceptions.peel(cause));
/** * @param eventHandler the {@link EventHandler} for which the actions are to be executed * @param msg the {@link Message} that triggered the event * @return a {@link CompletableFuture} holding a list of {@link EventExecution}s for the {@link Action}s executed in the event handler */ private CompletableFuture<List<EventExecution>> executeActionsForEventHandler(EventHandler eventHandler, Message msg) { List<CompletableFuture<EventExecution>> futuresList = new ArrayList<>(); int i = 0; for (Action action : eventHandler.getActions()) { String id = msg.getId() + "_" + i++; EventExecution eventExecution = new EventExecution(id, msg.getId()); eventExecution.setCreated(System.currentTimeMillis()); eventExecution.setEvent(eventHandler.getEvent()); eventExecution.setName(eventHandler.getName()); eventExecution.setAction(action.getAction()); eventExecution.setStatus(Status.IN_PROGRESS); if (executionService.addEventExecution(eventExecution)) { futuresList.add(CompletableFuture.supplyAsync(() -> execute(eventExecution, action, getPayloadObject(msg.getPayload())), executorService)); } else { logger.warn("Duplicate delivery/execution of message: {}", msg.getId()); } } return CompletableFutures.allAsList(futuresList); }