Laisser CompletableFuture exceptionnellement () gérer une exception supplyAsync ()

Nov 20 2020

La question est assez simple: je recherche une manière élégante d'utiliser CompletableFuture#exceptionallyavec CompletableFuture#supplyAsync. C'est ce qui ne fonctionne pas:

private void doesNotCompile() {
    CompletableFuture<String> sad = CompletableFuture
            .supplyAsync(() -> throwSomething())
            .exceptionally(Throwable::getMessage);
}

private String throwSomething() throws Exception {
    throw new Exception();
}

Je pensais que l'idée derrière exceptionally()était précisément de gérer les cas où un Exceptionest jeté. Pourtant, si je fais cela, cela fonctionne:

private void compiles() {
    CompletableFuture<String> thisIsFine = CompletableFuture.supplyAsync(() -> {
        try {
            throwSomething();
            return "";
        } catch (Exception e) {
            throw new RuntimeException(e);
        }
    }).exceptionally(Throwable::getMessage);
}

Je pourrais travailler avec ça, mais cela a l'air horrible et rend les choses plus difficiles à entretenir. N'y a-t-il pas un moyen de garder cela propre qui ne nécessite pas de transformer tout Exceptionen RuntimeException?

Réponses

2 Eugene Nov 21 2020 at 02:21

Ce n'est peut-être pas une bibliothèque très populaire, mais nous l'utilisons (et de temps en temps, j'y travaille aussi; mineur cependant) en interne: NoException . C'est vraiment, vraiment bien écrit à mon goût. Ce n'est pas la seule chose dont il dispose, mais couvre définitivement votre cas d'utilisation:

Voici un exemple:

import com.machinezoo.noexception.Exceptions;
import java.util.concurrent.CompletableFuture;

public class SO64937499 {

    public static void main(String[] args) {
        CompletableFuture<String> sad = CompletableFuture
            .supplyAsync(Exceptions.sneak().supplier(SO64937499::throwSomething))
            .exceptionally(Throwable::getMessage);
    }

    private static String throwSomething() throws Exception {
        throw new Exception();
    }
}

Ou vous pouvez les créer vous-même:

final class CheckedSupplier<T> implements Supplier<T> {

    private final SupplierThatThrows<T> supplier;

    CheckedSupplier(SupplierThatThrows<T> supplier) {
        this.supplier = supplier;
    }

    @Override
    public T get() {
        try {
            return supplier.get();
        } catch (Throwable exception) {
            throw new RuntimeException(exception);
        }
    }
}



@FunctionalInterface
interface SupplierThatThrows<T> {

    T get() throws Throwable;
}

Et l'utilisation:

 CompletableFuture<String> sad = CompletableFuture
        .supplyAsync(new CheckedSupplier<>(SO64937499::throwSomething))
        .exceptionally(Throwable::getMessage);