October 2018
Intermediate to advanced
556 pages
15h 18m
English
The TemperatureSensor class previously sent events to a Spring ApplicationEventPublisher, but now it should return a reactive stream with Temperature events. The Reactive implementation of TemperatureSensor may look like the following:
@Component // (1)public class TemperatureSensor { private final Random rnd = new Random(); // (2) private final Observable<Temperature> dataStream = // (3) Observable .range(0, Integer.MAX_VALUE) // (4) .concatMap(tick -> Observable // (5) .just(tick) // (6) .delay(rnd.nextInt(5000), MILLISECONDS) // (7) .map(tickValue -> this.probe())) // (8) .publish() // (9) .refCount(); // (10) private Temperature probe() { return new Temperature(16 + rnd.nextGaussian() * 10); // (11) } public ...