- Hands-On Reactive Programming in Spring 5
- Oleh Dokuka Igor Lozynskyi
- 306字
- 2021-07-23 16:36:29
Vert.x adjustments
Along with the transformation of RxJava, the rest of the reactive libraries and frameworks vendors have also started adopting the Reactive Streams specification. Following the specification, Vert.x included an additional module which provides support for the Reactive Streams API. The following example demonstrates this addition:
... // (1)
.requestHandler(request -> { //
ReactiveReadStream<Buffer> rrs = // (2)
ReactiveReadStream.readStream(); //
HttpServerResponse response = request.response(); //
Flowable<Buffer> logs = Flowable // (3)
.fromPublisher(logsService.stream()) //
.map(Buffer::buffer) //
.doOnTerminate(response::end); //
logs.subscribe(rrs); // (4)
response.setStatusCode(200); // (5)
response.setChunked(true); //
response.putHeader("Content-Type", "text/plain"); //
response.putHeader("Connection", "keep-alive"); //
Pump.pump(rrs, response) // (6)
.start(); //
})
...
The key is as follows:
- This is the request handler declaration. This is a generic request handler that allows handling any requests sent to the server.
- This is the Subscriber and HTTP response declaration. Here ReactiveReadStream implements both org.reactivestreams.Subscriber and ReadStream, which allows transforming any Publisher to the source of data compatible with a Vert.x API.
- This is the processing flow declaration. In that example, we refer to the new Reactive Streams-based LogsService interface, and to write a functional transformation of the elements in the stream we use the Flowable API from RxJava 2.x.
- This is the subscription stage. Once the processing flow is declared, we may subscribe ReactiveReadStream to the Flowable.
- This is a response preparation stage.
- This is the final response being sent to the client. Here, the Pump class plays an important role in a sophisticated mechanism of backpressure control to prevent the underlying WriteStream buffer from getting too full.
As we can see, Vert.x does not provide a fluent API for writing a stream of element processing. However, it provides an API that allows converting any Publisher to the Vert.x API, keeping the sophisticated backpressure management from Reactive Streams in place.
推薦閱讀
- 電子政務(wù)效益的經(jīng)濟(jì)分析與評價
- 物聯(lián)網(wǎng)+BIM:構(gòu)建數(shù)字孿生的未來
- 互聯(lián)網(wǎng)基礎(chǔ)資源技術(shù)與應(yīng)用發(fā)展態(tài)勢(2021—2023)
- React:Cross-Platform Application Development with React Native
- 物聯(lián)網(wǎng)長距離無線通信技術(shù)應(yīng)用與開發(fā)
- 邁向自智網(wǎng)絡(luò)時代:IP自動駕駛網(wǎng)絡(luò)
- Mastering Dart
- Getting Started with Memcached
- Practical Web Penetration Testing
- 小型局域網(wǎng)組建
- 中國信息化年鑒2017
- 黑客心理學(xué):社會工程學(xué)原理
- 智能物聯(lián)安防視頻技術(shù)基礎(chǔ)與應(yīng)用
- EtherCAT工業(yè)以太網(wǎng)應(yīng)用技術(shù)
- Citrix XenDesktop 5.6 Cookbook