Source:
private void triggerNextJobAsync(String location, Queue<DiscoveryJob> jobs, AtomicInteger jobIndexTracker, int totalNumberOfJobs, int taskId, CompletableFuture<Void> future) { final DiscoveryJob job = jobs.poll(); if (job == null) { future.complete(null); return; } final PingSweepRequestBuilder builder = locationAwarePingClient.sweep() .withLocation(job.getLocation()) .withPacketsPerSecond(job.getPacketsPerSecond()); for(IPPollRange range : job.getRanges()) { try { InetAddress begin = InetAddress.getByAddress(range.getAddressRange().getBegin()); InetAddress end = InetAddress.getByAddress(range.getAddressRange().getEnd()); builder.withRange(begin, end, range.getRetries(), range.getTimeout(), TimeUnit.MILLISECONDS); } catch (UnknownHostException e) { LOG.error("Failed to retrieve addresses from range: {}. The range will be skipped.", e); } } final int jobIndex = jobIndexTracker.incrementAndGet(); LOG.debug("Starting job of at location (on task #{}).", jobIndex, totalNumberOfJobs, location, taskId); builder.execute().whenComplete((summary, ex) -> { Logging.withPrefix(Discovery.getLoggingCategory(), new Runnable() { @Override public void run() { if (summary != null) { LOG.debug("Job of at location (on task #{}) completed succesfully.", jobIndex, totalNumberOfJobs, location, taskId); CompletableFuture<List<DiscoveryResult>> resultsFuture = performDetection(job.getLocation(), summary); resultsFuture.whenComplete((results, throwable) -> { final Log eventLog = toNewSuspectEvents(job, results); if (eventLog.getEvents() != null && eventLog.getEvents().getEventCount() >= 1) { eventForwarder.sendNow(eventLog); } triggerNextJobAsync(location, jobs, jobIndexTracker, totalNumberOfJobs, taskId, future); }); } else { LOG.error("An error occurred while processing job of at location (on task #{})." + " No newSuspect events will be generated.", jobIndex, totalNumberOfJobs, location, taskId, ex); triggerNextJobAsync(location, jobs, jobIndexTracker, totalNumberOfJobs, taskId, future); } } }); }); }

Target:
private void triggerNextJobAsync(String location, Queue<DiscoveryJob> jobs, AtomicInteger jobIndexTracker, int totalNumberOfJobs, int taskId, CompletableFuture<Void> future) { final DiscoveryJob job = jobs.poll(); if (job == null) { future.complete(null); return; } final PingSweepRequestBuilder builder = locationAwarePingClient.sweep() .withLocation(job.getLocation()) .withPacketsPerSecond(job.getPacketsPerSecond()); for(IPPollRange range : job.getRanges()) { try { InetAddress begin = InetAddress.getByAddress(range.getAddressRange().getBegin()); InetAddress end = InetAddress.getByAddress(range.getAddressRange().getEnd()); builder.withRange(begin, end, range.getRetries(), range.getTimeout(), TimeUnit.MILLISECONDS); } catch (UnknownHostException e) { LOG.error("Failed to retrieve addresses from range: {}. The range will be skipped.", e); } } final int jobIndex = jobIndexTracker.incrementAndGet(); LOG.debug("Starting job of at location (on task #{}).", jobIndex, totalNumberOfJobs, location, taskId); builder.execute().whenComplete((summary, ex) -> { Logging.withPrefix(Discovery.getLoggingCategory(), new Runnable() { @Override public void run() { if (summary != null) { CompletableFuture<List<DiscoveryResult>> resultsFuture = performDetection(job.getLocation(), summary, job.getConfig()); resultsFuture.whenComplete((results, throwable) -> { LOG.debug("Job of at location (on task #{}) completed succesfully.", jobIndex, totalNumberOfJobs, location, taskId); final Log eventLog = toNewSuspectEvents(job, results); if (eventLog.getEvents() != null && eventLog.getEvents().getEventCount() >= 1) { eventForwarder.sendNow(eventLog); } triggerNextJobAsync(location, jobs, jobIndexTracker, totalNumberOfJobs, taskId, future); }); } else { LOG.error("An error occurred while processing job of at location (on task #{})." + " No newSuspect events will be generated.", jobIndex, totalNumberOfJobs, location, taskId, ex); triggerNextJobAsync(location, jobs, jobIndexTracker, totalNumberOfJobs, taskId, future); } } }); }); }