官术网_书友最值得收藏!

Exposing the SSE endpoint

To expose the SSE endpoint, we need a REST controller that is autowired with the TemperatureSensor instance. The following code shows the controller, which utilizes RxSeeEmitter:

@RestController
public class TemperatureController {
private final TemperatureSensor temperatureSensor; // (1)

public TemperatureController(TemperatureSensor temperatureSensor) {
this.temperatureSensor = temperatureSensor;
}

@RequestMapping(
value = "/temperature-stream",
method = RequestMethod.GET)
public SseEmitter events(HttpServletRequest request) {
RxSeeEmitter emitter = new RxSeeEmitter(); // (2)

temperatureSensor.temperatureStream() // (3)
.subscribe(emitter.getSubscriber()); // (4)

return emitter; // (5)
}
}

The TemperatureController is the same Spring Web MVC @RestController as before. It holds a reference to the TemperatureSensor bean (1). When a new SSE session is created, the controller instantiates our augmented RxSeeEmitter (2) and subscribes to the RxSeeEmitter subscribers (4) to the temperature stream referenced from the TemperatureSensor instance (3). Then the RxSeeEmitter instance is returned to the Servlet container for processing (5).

As we can see with RxJava, the REST controller holds less logic, does not manage the dead SseEmitter instances, and does not care about synchronization. In turn, the reactive implementation manages the routine of the TemperatureSensor's values, reading, and publishing. The RxSeeEmitter translates reactive streams to outgoing SSE messages, and TemperatureController only binds a new SSE session to a new RxSeeEmitter that is subscribed to a stream of temperature readings. Furthermore, this implementation does not use Spring's EventBus, so it is more portable and can be tested without initializing a Spring context.

主站蜘蛛池模板: 嫩江县| 龙岩市| 云南省| 花莲县| 元谋县| 双城市| 且末县| 林州市| 中西区| 德化县| 册亨县| 泗洪县| 万州区| 静安区| 荆门市| 平昌县| 镇远县| 南丹县| 阿克陶县| 西乌珠穆沁旗| 五峰| 珲春市| 岳西县| 绍兴县| 修武县| 乌兰察布市| 青州市| 天长市| 河北省| 轮台县| 龙川县| 嘉鱼县| 达拉特旗| 万盛区| 昌平区| 彩票| 天水市| 赤水市| 敦化市| 丹巴县| 康乐县|