A processor represents a state of data processing. It is therefore presented as both a publisher and a subscriber. Since it is a publisher, we can create a processor and Subscribe to it. Most of the functions of a publisher can be performed using a processor; it can inject custom data, as well as generate errors and completion events. We can also interface all operators on it.
Consider the following code:
DirectProcessor<Long> data = DirectProcessor.create();
data.take(2).subscribe(t -> System.out.println(t));
data.onNext(10L);
data.onNext(11L);
data.onNext(12L);
In the preceding code, we did the following things:
- We added an instance of DirectProcessor
- In the second line, we added the take operator, to select two elements
- We also subscribed and printed the data on the console
- In the last three lines, we published three data elements
Let&apos...