- Hands-On Reactive Programming in Spring 5
- Oleh Dokuka Igor Lozynskyi
- 314字
- 2021-07-23 16:36:19
Implementing business logic
We may now outline the design of our system in the following diagram:

In this use case, the domain model will consist only of the Temperature class with the only double value inside. For simplicity purposes, it is also used as an event object, as shown in the following code:
final class Temperature {
private final double value;
// constructor & getter...
}
To simulate the sensor, let's implement the TemperatureSensor class and decorate it with a @Component annotation to register the Spring bean, as follows:
@Component
public class TemperatureSensor {
private final ApplicationEventPublisher publisher; // (1)
private final Random rnd = new Random(); // (2)
private final ScheduledExecutorService executor = // (3)
Executors.newSingleThreadScheduledExecutor();
public TemperatureSensor(ApplicationEventPublisher publisher) {
this.publisher = publisher;
}
@PostConstruct
public void startProcessing() { // (4)
this.executor.schedule(this::probe, 1, SECONDS);
}
private void probe() { // (5)
double temperature = 16 + rnd.nextGaussian() * 10;
publisher.publishEvent(new Temperature(temperature));
// schedule the next read after some random delay (0-5 seconds)
executor
.schedule(this::probe, rnd.nextInt(5000), MILLISECONDS); // (5.1)
}
}
So, our simulated temperature sensor only depends on the ApplicationEventPublisher class (1), provided by Spring Framework. This class makes it possible to publish events to the system. It is a requirement to have a random generator (2) to contrive temperatures with some random intervals. An event generation process happens in a separate ScheduledExecutorService (3), where each event's generation schedules the next round of an event's generation with a random delay (5.1). All that logic is defined in the probe() method (5). In turn, the mentioned class has the startProcessing() method annotated with @PostConstruct (4), which is called by Spring Framework when the bean is ready and triggers the whole sequence of random temperature values.
- 網(wǎng)絡(luò)云百問百答
- Web安全防護(hù)指南:基礎(chǔ)篇
- 物聯(lián)網(wǎng)工程規(guī)劃技術(shù)
- Hands-On Industrial Internet of Things
- 計算機網(wǎng)絡(luò)安全實訓(xùn)教程(第二版)
- 物聯(lián)網(wǎng)安全技術(shù)
- Metasploit Penetration Testing Cookbook
- Learning Swift(Second Edition)
- 光纖通信系統(tǒng)與網(wǎng)絡(luò)(修訂版)
- 網(wǎng)絡(luò)設(shè)計與應(yīng)用(第2版)
- 網(wǎng)絡(luò)利他行為研究:積極心理學(xué)的視角
- 從實踐中學(xué)習(xí)手機抓包與數(shù)據(jù)分析
- Professional Scala
- Enterprise ApplicationDevelopment with Ext JSand Spring
- 賽博空間簡史