- Hands-On Reactive Programming in Spring 5
- Oleh Dokuka Igor Lozynskyi
- 650字
- 2021-07-23 16:36:29
JDK 9
Likewise, the value of specification was seen by the JDK implementing team too. Not long after the first release of the specification, Doug Lee created a proposal to add the aforementioned interface in JDK 9. The proposal was supported by the fact that the current Stream API offers just a pull model, and a push model was a missing point here:
The primary goal of the proposal was to specify interfaces for reactive streams inside JDK. According to the proposal, all interfaces defined in the Reactive Streams specification are provided within the java.util.concurrent.Flow class as static sub-classes. On the one hand, that improvement is significant because Reactive Streams becomes a JDK standard. On the other hand, many vendors have already relied on the specification provided within the org.reactivestreams.* package. Since most vendors (such as RxJava) support several versions of JDK, it is impossible to just implement these interfaces along with the previous ones. Consequently, this improvement manifests an additional requirement to be compatible with JDK 9+ and to somehow convert one specification to another.
Fortunately, the Reactive Streams specification provides an additional module for that purpose, which allows the conversion of Reactive Streams types to JDK Flow types:
... // (1)
import org.reactivestreams.Publisher; //
import java.util.concurrent.Flow; //
... //
Flow.Publisher jdkPublisher = ...; // (2)
Publisher external = FlowAdapters.toPublisher(jdkPublisher) // (2.1)
Flow.Publisher jdkPublisher2 = FlowAdapters.toFlowPublisher( //
external // (2.2)
); //
The key is as follows:
- These are the import definitions. As might be noticed from the imports' statements, we have the import of the Publisher from the original Reactive Streams library and the import of the Flow, which is the access point to all interfaces of Reactive Streams, but ported to JDK 9.
- This is the Flow.Publisher instance definition. Here we define the instance of Publisher from the JDK 9. In turn, at point (2.1), we use the FlowAdapters.toPublisher method from the original Reactive Streams library to convert Flow.Publisher to org.reactivestreams.Publisher. Also, for demo purposes, at line (2.2) we use the FlowAdapters.toFlowPublisher method to convert org.reactivestreams.Publisher back to Flow.Publisher.
The preceding example shows how we may easily convert Flow.Publisher to org.reactivestreams.Publisher. It should be noted that the example is unrelated to the real business use-case because there were no well known reactive libraries written from scratch on top of the JDK 9 Flow API at the time this book was published. Consequently, there was no need to migrate from the Reactive Streams specification as the external library that supports JDK 6 and higher. However, in the future, everything will most likely change, and new iterations of reactive libraries will definitely be written on top of the Reactive Streams specification and ported to JDK 9.
- 局域網(wǎng)組建、管理與維護項目教程(Windows Server 2003)
- 面向物聯(lián)網(wǎng)的嵌入式系統(tǒng)開發(fā):基于CC2530和STM32微處理器
- 中小型局域網(wǎng)組建、管理與維護實戰(zhàn)
- 計算機網(wǎng)絡工程實用教程(第2版)
- Building RESTful Web services with Go
- 網(wǎng)絡環(huán)境中基于用戶視角的信息質(zhì)量評價研究
- React Cookbook
- 物聯(lián)網(wǎng)場景設計與開發(fā)(初級)
- 6G無線網(wǎng)絡空口關鍵技術
- 互聯(lián)網(wǎng)+思維與創(chuàng)新:通往未來的+號
- 物聯(lián)網(wǎng)M2M開發(fā)技術:基于無線CPU-Q26XX
- Migrating to Drupal7
- 加密與解密實戰(zhàn)全攻略
- ElasticSearch Server
- 賽博空間簡史