20 Mayıs 2021 Perşembe

SpringWebFlux Flux.onErrorResume metodu

Giriş
Açıklaması şöyle
Project Reactor provides several operators to manage errors effectively within reactive streams:

1. onErrorResume and onErrorReturn: These operators allow you to provide fallback values or alternative streams in case of an error. This can help prevent the entire stream from failing and provide a more graceful degradation.
2. doOnError: This operator lets you execute specific actions when an error occurs, such as logging the error or cleaning up resources. It doesn't interfere with the error propagation itself.
3. retry and retryWhen: These operators enable you to automatically retry an operation a specified number of times or based on a certain condition. This can be helpful for transient errors.
Kısaca onErrorResume exception'ı loglar ve varsayılan bir sonuç döndürür

Örnek
Elimizde şöyle bir kod olsun
Flux<String> stringFlux = Flux.just("A", "B", "C")
  .log()
  .concatWith(Flux.error(new RuntimeException("Exception Occurred")))
  .concatWith(Flux.just("D"))
  .onErrorResume((e) -> {    // this block gets executed
    System.out.println("Exception is : " + e);
    return Flux.just("default", "default1");
});

stringFlux.subscribe(System.out::println);
Çıktı olarak A,B,C,"default","default1" alırız.


Hiç yorum yok:

Yorum Gönder