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

Building applications with @EventListener

To play with the Publish-Subscribe pattern in Spring Framework, let's do an exercise. In turn, assume that we have to implement a simple web service that shows the current temperature in the room. For this purpose, we have a temperature sensor, which sends events with the current temperature in Celsius from time to time. We potentially want to have both mobile and web applications, but for the sake of conciseness, we are implementing only a simple web application. Furthermore, as questions of communication with microcontrollers are out of the scope of this book, we are simulating a temperature sensor using a random number generator.

To make our application following the reactive design, we cannot use an old pulling model for data retrieval. Fortunately, nowadays we have some well-adopted protocols for asynchronous message propagation from a server to a client, namely WebSockets and Server-Sent Events (SSE). In the current example, we will use the last one. The SSE allows a client to receive automatic updates from a server, and is commonly used to send message updates or continuous data streams to a browser. With the inception of HTML5, all modern browsers have a JavaScript API called EventSource, which is used by clients who request a particular URL to receive an event stream. The EventSource also autoreconnects by default in the case of communication issues. It is important to highlight that SSE is an excellent candidate for fulfilling communication needs between components in the reactive system. On par with WebSocket, SSE is used a lot in this book.

To read more about Server-Sent Events, please read the chapter of High Performance Browser Networking by Ilya Grigorik at  https://hpbn.co/server-sent-events-sse/.

Also, the following article by Mark Brown gives a good comparison between WebSockets and Server-Sent Events:
https://www.sitepoint.com/real-time-apps-websockets-server-sent-events/
主站蜘蛛池模板: 新营市| 鹰潭市| 昌宁县| 靖边县| 个旧市| 陈巴尔虎旗| 玛曲县| 梧州市| 河池市| 江城| 濉溪县| 德江县| 肃南| 泗水县| 名山县| 阜平县| 曲周县| 教育| 怀来县| 安徽省| 乌拉特中旗| 肇庆市| 项城市| 右玉县| 且末县| 澄江县| 延安市| 祁连县| 华蓥市| 曲麻莱县| 固安县| 兴安县| 新余市| 通城县| 长治市| 紫金县| 宝清县| 云南省| 堆龙德庆县| 罗源县| 丘北县|