Source:
protected CompletableFuture<?> transmit(final RecordProvider packet, final InetSocketAddress remoteAddress) { LOG.trace("Got packet: {}", packet); final CompletableFuture<CompletableFuture[]> futureOfFutures = CompletableFuture.supplyAsync(() -> { return packet.getRecords().map(record -> { final CompletableFuture<AsyncDispatcher.DispatchStatus> future = new CompletableFuture<>(); final Timer.Context timerContext = recordEnrichmentTimer.time(); final RecordEnricher recordEnricher = new RecordEnricher(dnsResolver, getDnsLookupsEnabled()); recordEnricher.enrich(record).whenComplete((enrichment, ex) -> { timerContext.close(); if (ex != null) { future.completeExceptionally(ex); return; } final Runnable dispatch = () -> { byte[] flowMessage = FlowMessageBuilderFactory.buildAndSerialize(this.protocol, record, enrichment); final TelemetryMessage msg = new TelemetryMessage(remoteAddress, ByteBuffer.wrap(flowMessage)); dispatcher.send(msg).whenComplete((b, exx) -> { if (exx != null) { future.completeExceptionally(exx); return; } future.complete(b); }); recordsDispatched.mark(); }; if (Boolean.TRUE.equals(isParserThread.get())) { dispatch.run(); } else { executor.execute(dispatch); } }); return future; }).toArray(CompletableFuture[]::new); }, executor); final CompletableFuture<Void> future = new CompletableFuture<>(); futureOfFutures.whenComplete((futures,ex) -> { if (ex != null) { LOG.warn("Error preparing records for dispatch.", ex); future.completeExceptionally(ex); return; } CompletableFuture.allOf(futures).whenComplete((any,exx) -> { if (exx != null) { LOG.warn("One or more of the records were not successfully dispatched.", exx); future.completeExceptionally(exx); return; } future.complete(any); }); }); return future; }

Target:
protected CompletableFuture<?> transmit(final RecordProvider packet, final InetSocketAddress remoteAddress) { LOG.trace("Got packet: {}", packet); final CompletableFuture<CompletableFuture[]> futureOfFutures = CompletableFuture.supplyAsync(() -> { return packet.getRecords().map(record -> { final CompletableFuture<AsyncDispatcher.DispatchStatus> future = new CompletableFuture<>(); final Timer.Context timerContext = recordEnrichmentTimer.time(); final RecordEnricher recordEnricher = new RecordEnricher(dnsResolver, getDnsLookupsEnabled()); recordEnricher.enrich(record).whenComplete((enrichment, ex) -> { timerContext.close(); if (ex != null) { future.completeExceptionally(ex); return; } final Runnable dispatch = () -> { byte[] flowMessage = buildMessage(record, enrichment); final TelemetryMessage msg = new TelemetryMessage(remoteAddress, ByteBuffer.wrap(flowMessage)); dispatcher.send(msg).whenComplete((b, exx) -> { if (exx != null) { future.completeExceptionally(exx); return; } future.complete(b); }); recordsDispatched.mark(); }; if (Boolean.TRUE.equals(isParserThread.get())) { dispatch.run(); } else { executor.execute(dispatch); } }); return future; }).toArray(CompletableFuture[]::new); }, executor); final CompletableFuture<Void> future = new CompletableFuture<>(); futureOfFutures.whenComplete((futures,ex) -> { if (ex != null) { LOG.warn("Error preparing records for dispatch.", ex); future.completeExceptionally(ex); return; } CompletableFuture.allOf(futures).whenComplete((any,exx) -> { if (exx != null) { LOG.warn("One or more of the records were not successfully dispatched.", exx); future.completeExceptionally(exx); return; } future.complete(any); }); }); return future; }