Shettyh
Shettyh

Reputation: 1216

Reactor Flux chaining, break and continue if one succeeds

Use case is to try to connect to one of the backend servers, if one succeeds then thats sufficient. Is there any way to accomplish this using Reactor framework without blocking ?

For example:

List<String> servers = ...
Flux.fromIterable(servers)

This flux Should try to connect and send data to servers one by one until if there is a success.

Is there any way to accomplish this ?

Thanks in advance.

Upvotes: 0

Views: 838

Answers (1)

Bartek Jablonski
Bartek Jablonski

Reputation: 2737

Exactly for this purpose there is Flux#first method. So you need to query server and return empty flux if there is no result, so reactor will try next query and so on:

    Flux.first(
        Flux.fromIterable(servers)
        .flatMap(this::queryServer)
    )

Upvotes: 1

Related Questions