RxJava API 和 Java 9 Flow API 之间的区别
Difference between RxJava API and the Java 9 Flow API
在最近几个主要版本的 Java 的每次迭代中,似乎都有新的方法来管理并发任务。
在 Java 9 中,我们有 RxJava 的 Flow API which resembles the Flowable API,但在 Java 9 中有一组更简单的 类 和接口。
Java 9
有一个Flow.Publisher
、Flow.Subscriber
、Flow.Processor
、Flow.Subscription
和SubmissionPublisher
,仅此而已。
RxJava
具有 Flow API 的整个 包 ,如 类,即 io.reactivex.flowables
、io.reactivex.subscribers
、io.reactivex.processors
、io.reactivex.observers
和 io.reactivex.observables
似乎在做类似的事情。
这两个库之间的主要区别是什么?为什么有人会使用 Java 9 Flow 库而不是更多样化的 RxJava 库,反之亦然?
"What are the main differences between these two libraries?"
正如您自己指出的那样,Java 9 库更加基础,基本上用作反应流的通用 API 而不是成熟的解决方案。
"Why would someone use the Java 9 Flow library over the much more diverse RxJava library or vice versa?"
好吧,出于同样的原因,人们使用基本的库结构而不是库——少了一个管理依赖。另外,由于Java9中的FlowAPI比较通用,所以受具体实现的约束较少。
一开始有 Rx,第一版。它是反应式 APIs 的语言不可知规范,具有 Java、JavaScript、.NET 的实现。然后他们改进了它,我们看到了 Rx 2. It has implementations for different languages as well. At the time of Rx 2 Spring team was working on Reactor — 他们自己的一套反应式 APIs.
然后他们都想:为什么不共同努力,创造一个 API 来统治他们。 Reactive Commons 就是这样设置的。为构建高度优化的响应流兼容操作符而进行的联合研究工作。当前的实现者包括 RxJava2 和 Reactor.
与此同时,JDK 开发人员意识到响应式内容很棒,值得包含在 Java 中。正如在 Java 世界中常见的那样,事实上的标准变成了法律上的标准。还记得 Hibernate 和 JPA、Joda Time 和 Java 8 Date/Time API 吗?所以 JDK 开发人员所做的是提取反应式 API 的核心,最基本的部分,并将其制定为标准。 j.u.c.Flow
就是这样诞生的。
从技术上讲,j.u.c.Flow
简单得多,它仅由 four simple interfaces 组成,而其他库提供数十个 类 和数百个运算符。
我希望,这回答了问题 "what is the difference between them"。
为什么有人会选择 j.u.c.Flow
而不是 Rx?好吧,因为现在它是一个标准!
目前 JDK 仅附带 j.u.c.Flow
的一种实现:HTTP/2 API。它实际上是一个孵化器API。但在未来,我们可能会期待 Reactor、RxJava 2 以及其他库(如反应式 DB 驱动程序甚至 FS IO)对它的支持。
这两个库之间的主要区别是什么?
这在大多数情况下都是有用的评论(但太长而无法放入),JEP 266: More Concurrency Updates 负责在 Java9 中引入 Flow
API 在其描述中说明了这一点(强调我的)-
Interfaces supporting the Reactive Streams publish-subscribe
framework, nested within the new class Flow.
Publisher
s produce items
consumed by one or more Subscriber
s, each managed by a Subscription
.
Communication relies on a simple form of flow control (method
Subscription.request
, for communicating back pressure) that can be
used to avoid resource management problems that may otherwise occur in
"push" based systems. A utility class SubmissionPublisher
is provided
that developers can use to create custom components.
These (very
small) interfaces correspond to those defined with broad participation
(from the Reactive Streams initiative) and support interoperability
across a number of async systems running on JVMs.
Nesting the interfaces within a class is a conservative policy allowing
their use across various short-term and long-term possibilities. There
are no plans to provide network- or I/O-based java.util.concurrent
components for distributed messaging, but it is possible that future JDK
releases will include such APIs in other packages.
为什么有人会使用 Java 9 Flow 库而不是更加多样化的 RxJava 库,反之亦然?
放眼更广阔的前景,这完全是基于客户正在开发的应用程序类型及其对框架的使用等因素的意见。
What are the main differences between these two libraries?
Java 9 Flow API 不是一个独立的库,而是 Java 标准版库的一个组件,由 Reactive Streams 规范采用的 4 个接口组成成立于 2015 年初。理论上,它的包含可以启用 in-JDK 特定用途,例如孵化 HttpClient,可能部分计划的 Async Database Connection,当然还有 SubmissionPublisher
.
RxJava 是 Java 库,它使用 ReactiveX 风格 API 设计来提供一组丰富的反应式(推送)数据流运算符。版本 2,通过 Flowable
和各种 XxxProcessor
s,实现了 Reactive Streams API,它允许 Flowable
的实例被其他兼容库使用,进而可以包装任何Publisher
变成 Flowable
来使用它们并用它们组成丰富的运算符集。
所以 Reactive Streams API 是 最小接口规范 而 RxJava 2 是一个 实现 其中,再加上 RxJava 声明了大量额外的方法,以形成其自己的丰富而流畅的 API。
RxJava 1 在其他来源中启发了 Reactive Streams 规范,但无法利用它(必须保持兼容)。 RxJava 2,作为一个完全重写和一个单独的主版本,可以包含和使用 Reactive Streams 规范(甚至在内部扩展它,感谢 Rsc project) and has been released almost a year before Java 9. In addition, it was decided both v1 and v2 keeps supporting Java 6 and thus a lot of Android runtimes. Therefore it couldn't capitalize directly on the Flow API provided now by Java 9 directly but only through a bridge)。 [= 需要这样的桥梁48=] 也在其他基于 Reactive Streams 的库中提供。
RxJava 3 可能以 Java 9 Flow API 为目标,但这尚未确定,具体取决于后续 Java 版本带来的功能(即值类型),我们可能在一年左右的时间内没有 v3。
到那时,有一个名为 Reactive4JavaFlow 的原型库,它确实实现了 Flow API 并提供了 ReactiveX 风格 rich fluent API。
Why would someone use the Java 9 Flow library over the much more diverse RxJava library or vice versa?
Flow API 是互操作规范,而不是最终用户 API。通常,您不会直接使用它,而是将流程传递给它的各种实现。当讨论 JEP 266 时,作者没有发现任何现有库的 API 足以让 Flow API 有一些默认的东西(不像丰富的 java.util.Stream
)。因此,决定用户现在必须依赖第 3 方实现。
您必须等待现有的反应式库通过它们自己的桥接实现或要实现的新库来原生支持 Flow API。
在 Flow API 上提供一组丰富的运算符是库实现它的唯一原因。数据源供应商(即反应式数据库驱动程序、网络库)可以开始通过 Flow API 实现自己的数据访问器,并依靠丰富的库来包装它们并为它们提供转换和协调,而无需强迫每个人都实现所有这些运营商的种类。
因此,一个更好的问题是,您应该现在开始使用基于 Flow API 的互操作还是坚持使用 Reactive Streams?
如果您需要相对较快的工作和可靠的解决方案,我建议您暂时坚持使用 Reactive Streams 生态系统。如果你有充裕的时间或者想探索一些东西,你可以开始使用 Flow API.
在最近几个主要版本的 Java 的每次迭代中,似乎都有新的方法来管理并发任务。
在 Java 9 中,我们有 RxJava 的 Flow API which resembles the Flowable API,但在 Java 9 中有一组更简单的 类 和接口。
Java 9
有一个Flow.Publisher
、Flow.Subscriber
、Flow.Processor
、Flow.Subscription
和SubmissionPublisher
,仅此而已。
RxJava
具有 Flow API 的整个 包 ,如 类,即 io.reactivex.flowables
、io.reactivex.subscribers
、io.reactivex.processors
、io.reactivex.observers
和 io.reactivex.observables
似乎在做类似的事情。
这两个库之间的主要区别是什么?为什么有人会使用 Java 9 Flow 库而不是更多样化的 RxJava 库,反之亦然?
"What are the main differences between these two libraries?"
正如您自己指出的那样,Java 9 库更加基础,基本上用作反应流的通用 API 而不是成熟的解决方案。
"Why would someone use the Java 9 Flow library over the much more diverse RxJava library or vice versa?"
好吧,出于同样的原因,人们使用基本的库结构而不是库——少了一个管理依赖。另外,由于Java9中的FlowAPI比较通用,所以受具体实现的约束较少。
一开始有 Rx,第一版。它是反应式 APIs 的语言不可知规范,具有 Java、JavaScript、.NET 的实现。然后他们改进了它,我们看到了 Rx 2. It has implementations for different languages as well. At the time of Rx 2 Spring team was working on Reactor — 他们自己的一套反应式 APIs.
然后他们都想:为什么不共同努力,创造一个 API 来统治他们。 Reactive Commons 就是这样设置的。为构建高度优化的响应流兼容操作符而进行的联合研究工作。当前的实现者包括 RxJava2 和 Reactor.
与此同时,JDK 开发人员意识到响应式内容很棒,值得包含在 Java 中。正如在 Java 世界中常见的那样,事实上的标准变成了法律上的标准。还记得 Hibernate 和 JPA、Joda Time 和 Java 8 Date/Time API 吗?所以 JDK 开发人员所做的是提取反应式 API 的核心,最基本的部分,并将其制定为标准。 j.u.c.Flow
就是这样诞生的。
从技术上讲,j.u.c.Flow
简单得多,它仅由 four simple interfaces 组成,而其他库提供数十个 类 和数百个运算符。
我希望,这回答了问题 "what is the difference between them"。
为什么有人会选择 j.u.c.Flow
而不是 Rx?好吧,因为现在它是一个标准!
目前 JDK 仅附带 j.u.c.Flow
的一种实现:HTTP/2 API。它实际上是一个孵化器API。但在未来,我们可能会期待 Reactor、RxJava 2 以及其他库(如反应式 DB 驱动程序甚至 FS IO)对它的支持。
这两个库之间的主要区别是什么?
这在大多数情况下都是有用的评论(但太长而无法放入),JEP 266: More Concurrency Updates 负责在 Java9 中引入 Flow
API 在其描述中说明了这一点(强调我的)-
Interfaces supporting the Reactive Streams publish-subscribe framework, nested within the new class Flow.
Publisher
s produce items consumed by one or moreSubscriber
s, each managed by aSubscription
.Communication relies on a simple form of flow control (method
Subscription.request
, for communicating back pressure) that can be used to avoid resource management problems that may otherwise occur in "push" based systems. A utility classSubmissionPublisher
is provided that developers can use to create custom components.These (very small) interfaces correspond to those defined with broad participation (from the Reactive Streams initiative) and support interoperability across a number of async systems running on JVMs.
Nesting the interfaces within a class is a conservative policy allowing their use across various short-term and long-term possibilities. There are no plans to provide network- or I/O-based
java.util.concurrent
components for distributed messaging, but it is possible that future JDK releases will include such APIs in other packages.
为什么有人会使用 Java 9 Flow 库而不是更加多样化的 RxJava 库,反之亦然?
放眼更广阔的前景,这完全是基于客户正在开发的应用程序类型及其对框架的使用等因素的意见。
What are the main differences between these two libraries?
Java 9 Flow API 不是一个独立的库,而是 Java 标准版库的一个组件,由 Reactive Streams 规范采用的 4 个接口组成成立于 2015 年初。理论上,它的包含可以启用 in-JDK 特定用途,例如孵化 HttpClient,可能部分计划的 Async Database Connection,当然还有 SubmissionPublisher
.
RxJava 是 Java 库,它使用 ReactiveX 风格 API 设计来提供一组丰富的反应式(推送)数据流运算符。版本 2,通过 Flowable
和各种 XxxProcessor
s,实现了 Reactive Streams API,它允许 Flowable
的实例被其他兼容库使用,进而可以包装任何Publisher
变成 Flowable
来使用它们并用它们组成丰富的运算符集。
所以 Reactive Streams API 是 最小接口规范 而 RxJava 2 是一个 实现 其中,再加上 RxJava 声明了大量额外的方法,以形成其自己的丰富而流畅的 API。
RxJava 1 在其他来源中启发了 Reactive Streams 规范,但无法利用它(必须保持兼容)。 RxJava 2,作为一个完全重写和一个单独的主版本,可以包含和使用 Reactive Streams 规范(甚至在内部扩展它,感谢 Rsc project) and has been released almost a year before Java 9. In addition, it was decided both v1 and v2 keeps supporting Java 6 and thus a lot of Android runtimes. Therefore it couldn't capitalize directly on the Flow API provided now by Java 9 directly but only through a bridge)。 [= 需要这样的桥梁48=] 也在其他基于 Reactive Streams 的库中提供。
RxJava 3 可能以 Java 9 Flow API 为目标,但这尚未确定,具体取决于后续 Java 版本带来的功能(即值类型),我们可能在一年左右的时间内没有 v3。
到那时,有一个名为 Reactive4JavaFlow 的原型库,它确实实现了 Flow API 并提供了 ReactiveX 风格 rich fluent API。
Why would someone use the Java 9 Flow library over the much more diverse RxJava library or vice versa?
Flow API 是互操作规范,而不是最终用户 API。通常,您不会直接使用它,而是将流程传递给它的各种实现。当讨论 JEP 266 时,作者没有发现任何现有库的 API 足以让 Flow API 有一些默认的东西(不像丰富的 java.util.Stream
)。因此,决定用户现在必须依赖第 3 方实现。
您必须等待现有的反应式库通过它们自己的桥接实现或要实现的新库来原生支持 Flow API。
在 Flow API 上提供一组丰富的运算符是库实现它的唯一原因。数据源供应商(即反应式数据库驱动程序、网络库)可以开始通过 Flow API 实现自己的数据访问器,并依靠丰富的库来包装它们并为它们提供转换和协调,而无需强迫每个人都实现所有这些运营商的种类。
因此,一个更好的问题是,您应该现在开始使用基于 Flow API 的互操作还是坚持使用 Reactive Streams?
如果您需要相对较快的工作和可靠的解决方案,我建议您暂时坚持使用 Reactive Streams 生态系统。如果你有充裕的时间或者想探索一些东西,你可以开始使用 Flow API.