Creating an Observable for an Event Loop

Recently I read a little about rx-java. I am wondering if the frame matches the count for the communication system between the streams. I am working on a REST server written in java. Every time there is some PUT / POSTED resource, I want to do some calculations using the workflow pool. However, I would still like to keep track of requests, perhaps print some statistics. Essentially, I need an Observable , so I can handle requests in a flexible way with multiple Observer s.

My question is, how can I create a suitable Observable ? Most of the tutorials I've seen deal with Observables operations, such as matching, etc. Observatives are mainly created from collections or entire ranges. In any case, it is impossible to create new values ​​for the created Observables. Apparently, the only way to maintain this flexibility is to use Observable.create . However, this looks rather low level. I would have to implement a queue list for each new subscriber and do a synchronized push for each of them. Is it really necessary or is something like this already implemented in rx-java?

+5
source share
1 answer

What you are looking for is Subject . They act both as Observers and as Observers. For example, ReplaySubject will play all events sent to it to all subscribers.

 Subject<String> replaySubject = ReplaySubject.create(); replaySubject.subscribe(s -> System.out.println(s)); // elsewhere... replaySubject.onNext("First"); replaySubject.onNext("Second"); replaySubject.onComplete(); 
+5
source

Source: https://habr.com/ru/post/1244565/


All Articles