6 Haziran 2023 Salı

SpringWebFlux Flux Hot Publisher

Giriş
Açıklaması şöyle
A hot publisher in Spring WebFlux is a publisher that emits data to all subscribers as soon as it is available. This is in contrast to a cold publisher, which emits data to subscribers only when they subscribe.
Açıklaması şöyle
With Hot Publishers, there will be only one data producer. All Subscribers listen to the data produced by the single data producer. The data is shared.

Imagine a TV station. It does not matter if there is no one to watch the program. It will be emitted regardless. Watchers can start watching anytime they want. But all watchers get the same info at any given moment. 

Watchers would lose the content if they joined late. The same is with the Hot Publishers.
Örnek
Şöyle yaparız
Flux<String> netFlux = Flux.fromStream(ReactiveJavaTutorial::getVideo)
  .delayElements(Duration.ofSeconds(2))
  .share(); // turn the cold publisher into a hot publisher
Backpressure 
Açıklaması şöyle
However, it is important to note that not all publishers respect backpressure. .... These are often termed as 'hot' publishers. Dealing with these publishers requires specific strategies, such as buffering, dropping, or sampling data.
Yani publisher, Backpressure kavramını desteklemez, aboneyi beklemez ve veri üretmeye devam eder.

Örnek
Şöyle yaparız. Böylece araya bir buffer koyulur
Flux.range(1, 100)
  .onBackpressureBuffer(10)
  .subscribe(new BaseSubscriber<Integer>() {
    @Override
    protected void hookOnSubscribe(Subscription subscription) {
      request(10);
    }

    @Override
    protected void hookOnNext(Integer value) {
      // process the value
      System.out.println(value);
      if (value % 10 == 0) {
        request(10);
      }
    }
});
Açıklaması şöyle
In this code, the onBackpressureBuffer operator is used to create a buffer that holds up to 10 items. If the buffer is full, the application will throw a BufferOverflowException.


Hiç yorum yok:

Yorum Gönder