- 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.
推薦閱讀
- 物聯(lián)網(wǎng)工程規(guī)劃技術(shù)
- 光網(wǎng)絡(luò)評(píng)估及案例分析
- 物聯(lián)網(wǎng)之魂:物聯(lián)網(wǎng)協(xié)議與物聯(lián)網(wǎng)操作系統(tǒng)
- 計(jì)算機(jī)網(wǎng)絡(luò)與數(shù)據(jù)通信
- 網(wǎng)絡(luò)互聯(lián)技術(shù)(實(shí)踐篇)
- WordPress 5 Complete
- 世界互聯(lián)網(wǎng)發(fā)展報(bào)告·2019
- 2小時(shí)讀懂物聯(lián)網(wǎng)
- 網(wǎng)絡(luò)環(huán)境中基于用戶視角的信息質(zhì)量評(píng)價(jià)研究
- Master Apache JMeter:From Load Testing to DevOps
- 信息技術(shù)安全評(píng)估準(zhǔn)則:源流、方法與實(shí)踐
- 物聯(lián)網(wǎng)與智慧廣電
- CDN技術(shù)詳解
- VMware vSphere 5.0虛擬化架構(gòu)實(shí)戰(zhàn)指南
- Hands-On Full Stack Web Development with Aurelia