15 Ocak 2021 Cuma

SpringWebFlux Schedulers

Kullanım
Açıklaması şöyle. Normalde Flux pipeline subcsribe metodunu çağıran thread üzerinde çalışır. Pipeline'daki thread'leri publishOn() ve subscriveOn() ile değiştirebiliriz.
Once subscribed, the pipeline is getting executed by default on the thread which subscribed. When the publishOn method is encountered, it switches the context for the downstream operations. But the source which is the Flux / Mono / or any publisher, is always executed on the current thread which subscribed. This SubscribeOn method will change the behavior.
Örnek - publishOn
Şöyle yaparız
Flux<Integer> flux = Flux.range(0, 2)
  .map(i -> {
    return i;
   })
  .publishOn(Schedulers.boundedElastic());
Örnek - subscribeOn
Şöylee yaparız
Runnable r = () -> flux
  .subscribeOn(Schedulers.single())
  .subscribe(s ->...);
boundedElastic metodu
Açıklaması şöyle. Uzun süreli işler içindir.
This is a preferred one instead of above elastic. This thread pool contains 10 * number of CPU cores you have. Good choice for IO operations or any blocking call.
elastic metodu
Elastic kelimesi Reactive sistemlerde şu anlama geliyor.
Reactive Systems can react to changes in the input rate by increasing or decreasing the resources allocated to service these inputs.
Açıklaması şöyle
This is a thread pool with unlimited threads which is no longer preferred. So DO NOT USE this option.
immediate metodu
Thread'i değiştirmez. Yani map() metodunun subscribe() metodunu çağıran thread üzerinde çalışmasını sağlar

Örnek
Şöyle yaparız
Flux<Integer> flux = Flux.range(0, 2)
  .publishOn(Schedulers.immediate())
  .map(i -> {
    return i;
  });

//create a runnable with flux subscription
Runnable r = () -> flux.subscribe(s -> {...});
Thread t1 = new Thread(r, "t1");
Thread t2 = new Thread(r, "t2");

t1.start();
t2.start();
newSingle metodu
Yeni bir thread açar ve map() ve subscribe() metodlarını hep aynı thread üzerinde çalıştırır

parallel metodu
Açıklaması şöyle. Kısa süreli işler içindir.
A fixed pool of workers that is tuned for parallel work. It creates as many workers as you have CPU cores. Should be used for any CPU operation. Not for IO or blocking calls.
single metodu
Tek thread kullanır ve map() ve subscribe() metodlarını hep aynı thread üzerinde çalıştırır

Örnek
Şöyle yaparız
Flux<Integer> flux = Flux.range(0, 2)
  .publishOn(Schedulers.single())
  .map(i -> {
    return i;
  });

//create a runnable with flux subscription
Runnable r = () -> flux.subscribe(s -> {...});
Thread t1 = new Thread(r, "t1");
Thread t2 = new Thread(r, "t2");

t1.start();
t2.start();

Hiç yorum yok:

Yorum Gönder