Flux.create 和 Flux.generate 的区别 [英] Difference Between Flux.create and Flux.generate
问题描述
Flux.create
和 Flux.generate
?我正在寻找——最好是通过一个示例用例——来了解我什么时候应该使用其中一个.
简而言之:
<块引用>Flux::create
不会对应用程序状态的变化做出反应,而 Flux::generate
会.
长版
Flux::create
当您想要计算不受应用状态和管道(您的管道)状态影响的多个(0...无穷大)值时,您将使用它 == 之后 Flux::create
== 下游的操作链.
为什么?因为您发送给 Flux::create
的方法一直在计算元素(或不计算).下游将决定它想要多少个元素(元素==下一个信号),如果他跟不上,那些已经发出的元素将被删除/缓冲在某些策略中(默认情况下,它们将被缓冲,直到下游要求更多).
第一个也是最简单的用例是发出值,理论上,您可以将这些值总结为一个集合,然后才获取每个元素并对其进行处理:
Flux文章Flux = Flux.create((FluxSink sink) -> {/* 从服务器获取所有最新文章,并一一发送到下游.*/列表<字符串>文章 = getArticalsFromServer();articals.forEach(sink::next);});
如您所见,Flux.create
用于阻塞方法 (getArticalsFromServer
) 与异步代码之间的交互.
我确定 Flux.create
还有其他用例.
Flux::generate
Flux.generate((SynchronousSink synchronousSink) -> {synchronousSink.next(1);}).doOnNext(number -> System.out.println(number)).doOnNext(number -> System.out.println(number + 4)).订阅();
输出将是 1 5 1 5 1 5......永远
在您发送给Flux::generate
的方法的每次调用中,synchronousSink
只能发出:onSubscribe onNext?(onError | onComplete)?
.
这意味着 Flux::generate
将计算并发出值按需.你应该什么时候使用它?如果计算可能不会在下游使用的元素的成本太高,或者您发出的事件受应用程序状态或管道状态的影响(您的管道 == 之后 Flux::create
== 下游的操作链).
例如,如果您正在构建 Torrent 应用程序,那么您将实时接收数据块.您可以使用 Flux::generate
将任务(要下载的块)分配给多个线程,并且您将计算要在 Flux::generate
中下载的块,只有当某些线程在问.所以你只会发出你没有的块.使用 Flux::create
的相同算法将失败,因为 Flux::create
将发出我们没有的所有块,如果某些块下载失败,那么我们有一个问题.因为 Flux::create
不会对应用程序状态的变化做出反应,而 Flux::generate
会.
What is the difference between Flux.create
and Flux.generate
? I am looking--ideally with an example use case--to understand when I should use one or the other.
In short:
Flux::create
doesn't react to changes in the state of the app whileFlux::generate
does.
The long version
Flux::create
You will use it when you want to calculate multiple (0...infinity) values which are not influenced by the state of your app and the state of your pipeline (your pipeline == the chain of operations which comes after Flux::create
== downstream).
Why? Because the method which you sent to Flux::create
keeps calculating elements (or none). The downstream will determine how many elements (elements == next signals) it wants and if he can't keep up, those elements which are already emitted will be removed/buffered in some strategy (by default they will be buffered until the downstream will ask for more).
The first and easiest use case is for emitting values which you, theoretically, could sum to a collection and only then take each element and do something with it:
Flux<String> articlesFlux = Flux.create((FluxSink<String> sink) -> {
/* get all the latest article from a server and emit them one by one to downstream. */
List<String> articals = getArticalsFromServer();
articals.forEach(sink::next);
});
As you can see, Flux.create
is used for interaction between blocking method (getArticalsFromServer
) to asynchronous code.
I'm sure there are other use cases for Flux.create
.
Flux::generate
Flux.generate((SynchronousSink<Integer> synchronousSink) -> {
synchronousSink.next(1);
})
.doOnNext(number -> System.out.println(number))
.doOnNext(number -> System.out.println(number + 4))
.subscribe();
The output will be 1 5 1 5 1 5................forever
In each invocation of the method you sent to Flux::generate
, synchronousSink
can only emits: onSubscribe onNext? (onError | onComplete)?
.
It means that Flux::generate
will calculate and emit values on demand. When should you use it? In cases where it's too expensive to calculate elements which may not be used downstream or the events which you emit are influenced by the state of the app or from the state of your pipeline (your pipeline == the chain of operations which comes after Flux::create
== downstream).
For example, if you are building a torrent application then you are receiving blocks of data in real time. You could use Flux::generate
to give tasks (blocks to download) to multiple threads and you will calculate the block you want to download inside Flux::generate
only when some thread is asking. So you will emit only blocks you don't have. The same algorithm with Flux::create
will fail because Flux::create
will emit all the blocks we don't have and if some blocks failed to be downloaded then we have a problem. because Flux::create
doesn't react to changes in the state of the app while Flux::generate
does.
这篇关于Flux.create 和 Flux.generate 的区别的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!