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

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:

"…t here is no single best fluent async/parallel API. CompletableFuture/CompletionStage best supports continuation style programming on futures, and java.util.Stream best supports (multi-stage, possibly-parallel) “pull” style operations on the elements of collections. Until now, one missing category was “push” style operations on items as they become available from an active source."
Note that under the hood, the Java Stream API uses Spliterator,  which is nothing more than a modified version of   Iterator, capable of parallel execution. As we might remember, the   Iterator  is not designed for pushing but for pulling over the   Iterator#next method. Similarly,   Spliterator has the  tryAdvance method, which is a combination of the    Iterator's  hasNext  and   next  methods. Consequently, we may conclude that in general the Stream API is pulling-based.

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:

  1. 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.
  2. 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.

Note that the adapter functionality is delivered as a separate library. To see all available libraries, please see the following link: http://www.reactive-streams.org/#jvm-interfaces-completed.

主站蜘蛛池模板: 鄄城县| 海宁市| 东乌珠穆沁旗| 奇台县| 泸州市| 措美县| 宝坻区| 博湖县| 资兴市| 东至县| 宝丰县| 富顺县| 沂水县| 枞阳县| 沈丘县| 上饶市| 同江市| 互助| 中方县| 安顺市| 洞口县| 莲花县| 和田县| 兴文县| 嵊泗县| 砚山县| 汾西县| 湘潭市| 饶阳县| 沙田区| 厦门市| 宜良县| 同仁县| 禄劝| 阳城县| 莒南县| 仪征市| 汨罗市| 罗山县| 屏山县| 平南县|