'Project reactor - not able to use fallback method

I'm new to reactive programming. I was playing around with fallback methods in case of an error scenario. For this, I referred to this doc https://projectreactor.io/docs/core/release/reference/index.html#_fallback_method and created a sample code.

public static void main(String[] args) {
    Flux.just("key1", "key2")
            .flatMap(k -> callExternalService(k)
                    .doOnError(e -> System.out.println("Error scenario"))
                    .onErrorResume(e -> getFromCache(k)))
            .subscribe(value -> System.out.println("value = " + value),
                    error -> System.out.println("error = " + error));
}

private static Mono<String> callExternalService(String input) {
    if(input.equals("key2")) throw new RuntimeException("Mocking the exception");
    return Mono.just(input + " - " + LocalDateTime.now());
}

private static Mono<String> getFromCache(String input) {
    return Mono.just(input + " ^^ " + LocalDateTime.now());
}

Based on whatever I referred so far, doOnError should print the message in case of the ERROR scenario and onErrorResume should fall back to the other method. But I didn't see the expected outcome->

value = key1 - 2022-05-18T15:58:36.364949

error = java.lang.RuntimeException: Mocking the exception

Please correct me if I'm missing anything.



Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source