Mono vs Flux in Reactive Stream

前端 未结 3 2050
闹比i
闹比i 2020-12-24 01:10

As per the documentation:

Flux is a stream which can emit 0..N elements:

Flux fl = Flux.just(\"a\", \"b\",         


        
相关标签:
3条回答
  • 2020-12-24 01:28

    From the docs here

    This distinction carries a bit of semantic information into the type, indicating the rough cardinality of the asynchronous processing. For instance, an HTTP request produces only one response, so there is not much sense in doing a count operation. Expressing the result of such an HTTP call as a Mono thus makes more sense than expressing it as a Flux, as it offers only operators that are relevant to a context of zero items or one item.

    0 讨论(0)
  • 2020-12-24 01:39

    Flux is equivalent to RxJava Observable is capable of emitting
    - zero or more item (streams of many elements)
    - and then OPTIONALLY , completing OR failing

    Mono can only emit one item at the most (streams one element)

    Relations:

    • If you concatente two Monos you will get a Flux
    • You can call single() on Flux to return a Mono
    0 讨论(0)
  • 2020-12-24 01:51

    In many cases, you are doing some computation or calling a service and you expect exactly one result (or maybe zero or one result), and not a collection that contains possibly multiple results. In such cases, it's more convenient to have a Mono.

    Compare it to "regular" Java: you would not use List as the return type of any method that can return zero or one result. You would use Optional instead, which makes it immediately clear that you do not expect more than one result.

    0 讨论(0)
提交回复
热议问题