Vert.x:Future 与 RxJava API 方法
Vert.x: Future vs RxJava API approach
我正在阅读 Vert.x 上的一本书。在某一时刻,它比较了 2 个代码片段,一个使用 Vert.x 的 Future,另一个使用 Vert.x 的 RxJava API。代码基本上从数据库中获取数据并处理结果集以将行映射到对象。比较 2 种方法,它指出:
However, while Futures make the code a bit more declarative, we are
retrieving all the rows in one batch and processing them. This result
can be huge and take a lot of time to be retrieved. At the same time,
you don’t need the whole result to start processing it. We can process
each row one by one as soon as you have them. Fortunately, Vert.x
provides an answer to this development model challenge and offers you
a way to implement reactive microservices using a reactive programming
development model. Vert.x provides RxJava APIs to:
• Combine and coordinate asynchronous tasks
• React to incoming messages as a stream
of input
In addition to improving readability, reactive programming allows you
to subscribe to a stream of results and process items as soon as they
are available. With Vert.x you can choose the development model you
prefer. In this report, we will use both callbacks and RxJava.
我不确定这里的 RxJava API 方法如何更好。数据库会一次发送所有行,而不是一一发送(假设我们从传统关系数据库中获取数据)。因此,在基于 RxJava 的方法中,我们也必须等待整个结果集通过网络到达,然后我们才能处理它们。
我found the book you're referencing, and the RxJava approach uses a different API for fetching rows from the database (SQLConnection.rxQueryStream
, instead of SQLConnection.query
), which does not fetch all rows at once. It's documented here.
The implementation does rely on the java.sql.ResultSet
object returned by the underlying JDBC driver to lazily fetch rows from the DB, however. Each JDBC driver behaves very differently in this regard, so whether or not you actually have a lazily-loading ResultSet
depends on which DB you're using, what your fetchSize
is set to, etc. This question 有更多关于延迟加载的信息 ResultSets
。
我正在阅读 Vert.x 上的一本书。在某一时刻,它比较了 2 个代码片段,一个使用 Vert.x 的 Future,另一个使用 Vert.x 的 RxJava API。代码基本上从数据库中获取数据并处理结果集以将行映射到对象。比较 2 种方法,它指出:
However, while Futures make the code a bit more declarative, we are retrieving all the rows in one batch and processing them. This result can be huge and take a lot of time to be retrieved. At the same time, you don’t need the whole result to start processing it. We can process each row one by one as soon as you have them. Fortunately, Vert.x provides an answer to this development model challenge and offers you a way to implement reactive microservices using a reactive programming development model. Vert.x provides RxJava APIs to:
• Combine and coordinate asynchronous tasks
• React to incoming messages as a stream of input
In addition to improving readability, reactive programming allows you to subscribe to a stream of results and process items as soon as they are available. With Vert.x you can choose the development model you prefer. In this report, we will use both callbacks and RxJava.
我不确定这里的 RxJava API 方法如何更好。数据库会一次发送所有行,而不是一一发送(假设我们从传统关系数据库中获取数据)。因此,在基于 RxJava 的方法中,我们也必须等待整个结果集通过网络到达,然后我们才能处理它们。
我found the book you're referencing, and the RxJava approach uses a different API for fetching rows from the database (SQLConnection.rxQueryStream
, instead of SQLConnection.query
), which does not fetch all rows at once. It's documented here.
The implementation does rely on the java.sql.ResultSet
object returned by the underlying JDBC driver to lazily fetch rows from the DB, however. Each JDBC driver behaves very differently in this regard, so whether or not you actually have a lazily-loading ResultSet
depends on which DB you're using, what your fetchSize
is set to, etc. This question 有更多关于延迟加载的信息 ResultSets
。