Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to log if a completableFuture fails?

i have this method:

default <U> CompletableFuture<U> submit(Supplier<U> supplier) {
    return CompletableFuture.supplyAsync(supplier, .....getThreadPool());
}

it is used widely by different classes, but currently if a computation fails there is no default logging. My first approach is:

.exceptionally(throwable ->
                .....handleThrowable(throwable, runnable);
)

But this method is intended for recovery, since i have to return something. What if i just want to log it?

like image 428
Leander Avatar asked Mar 24 '15 20:03

Leander


1 Answers

This will handle the exception and return the original result, so the downstream code will also have a chance to do something with the exception. Is that what you intend?

default <U> CompletableFuture<U> submit(Supplier<U> supplier) {
    return CompletableFuture.supplyAsync(supplier, ....)
            .whenComplete((u, ex) -> {
                if (ex != null) {
                    handleThrowable(ex);
                }
            });
}
like image 192
Misha Avatar answered Oct 05 '22 23:10

Misha