Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Flux to List<Objects> without blocking

Looking for converting Flux to List<Object>. Getting error if I use block(). So, need to conver without blocking calls.

Flux.from(Collection.find())

Using reactive programming, but graphql expects List<objects> and erroring with returning Flux.

Code with Block()

public List<Test> findAll() {
        return Flux.from(testCollection.find()).collectList().block();

}

Error :-

block()/blockFirst()/blockLast() are blocking, which is not supported in thread reactor-http-kqueue-7

Here, I need to return List<Test> as I can not send Flux<Test> for some reason.

like image 621
user1578872 Avatar asked Aug 09 '26 16:08

user1578872


2 Answers

As stated in the comments, You can't. The reactive pattern is to stay in a flow.

So,

Mono<GraphqlResponse> = Flux.just("A", "B" "C")
  .collectList()
  .map(this::someMethod);

GraphqlResponse someMethod(List<String> abcs) {
    return graphQl.doSomething(abcs);
}
like image 143
K.Nicholas Avatar answered Aug 12 '26 07:08

K.Nicholas


following example is converting flux<Object> to List<Object> but remember that converting a Flux to a List makes the whole thing NOT reactive:

public static void main(String[] args) {
    Flux<String> flux = Flux.just("test1", "test2", "test3");
    List<String> list = new ArrayList<>();
    flux.collectList().subscribe(list::addAll);
    list.forEach(System.out::println);
}
like image 25
Issa Khodadadi Avatar answered Aug 12 '26 05:08

Issa Khodadadi



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!