1
\$\begingroup\$

I writing this code to leverage the advantages of Completable future.

My doubt is call cancel() method after get() is correct approach?

 private void combineFilters(JobFilter jobFilter,
                            CompletableFuture<List<String>> measureCompletableFuture,
                            CompletableFuture<List<String>> dqTypCompletableFuture,
                            CompletableFuture<List<String>> ownerCompletableFuture,
                            CompletableFuture<List<String>> jobTypeCompletableFuture,
                            CompletableFuture<List<String>> jobStateCompletableFuture) {
    try {
        CompletableFuture.allOf(measureCompletableFuture, dqTypCompletableFuture,
                                ownerCompletableFuture, jobTypeCompletableFuture,
                                jobStateCompletableFuture)
                         .join();
        LOGGER.info("All filters DB calls completed");
        if (measureCompletableFuture.isDone()) {
            LOGGER.info("Populating measure filters results");
            jobFilter.setMeasureName(measureCompletableFuture.get());
        }
        if (dqTypCompletableFuture.isDone()) {
            LOGGER.info("Populating dqtype filters results");
            jobFilter.setDqType(dqTypCompletableFuture.get());
        }
        if (ownerCompletableFuture.isDone()) {
            LOGGER.info("Populating owner filters results");
            jobFilter.setOwner(ownerCompletableFuture.get());
        }
        if (jobTypeCompletableFuture.isDone()) {
            LOGGER.info("Populating jobtype filters results");
            jobFilter.setType(jobTypeCompletableFuture.get());
        }
        if (jobStateCompletableFuture.isDone()) {
            LOGGER.info("Populating jobstate filters results");
            jobFilter.setTriggerState(jobStateCompletableFuture.get());
        }
    } catch (InterruptedException | ExecutionException e) {
        LOGGER.error("Exception occurred while populating the filter results {}",
                     e.getMessage());
        Thread.currentThread().interrupt();
    } finally {
        LOGGER.info("Exiting from combine job filters");
        measureCompletableFuture.cancel(true);
        dqTypCompletableFuture.cancel(true);
        ownerCompletableFuture.cancel(true);
        jobTypeCompletableFuture.cancel(true);
        jobStateCompletableFuture.cancel(true);
    }
}
\$\endgroup\$
  • 1
    \$\begingroup\$ The current question title of your question is too generic to be helpful. Please edit to the site standard, which is for the title to simply state the task accomplished by the code. Please see How do I ask a good question?. \$\endgroup\$ – BCdotWEB Sep 25 '20 at 15:07

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Browse other questions tagged or ask your own question.