I use SpringBoot and reactive programming with Webflux. I want to repeat the service until data is available(something is returned apart from null)
I have a service which insert some data into databse and there is 2nd service which consumes the data. I want to keep on querying the database from 2nd service untill the data is avaiable into it. Below code I am trying to achieve this using Project Reactor:
Mono<SubscriptionQueryResult<App, App>> subscriptionQuery = reactiveQueryGateway
.subscriptionQuery(new FindAppByIdQuery(appId), ResponseTypes.instanceOf(App.class), ResponseTypes.instanceOf(App.class));
subscriptionQuery
.filter(a -> Objects.nonNull(a.initialResult().block()))
.repeatWhen(Repeat.onlyIf(repeatContext -> true)
.exponentialBackoff(Duration.ofMillis(100), Duration.ofSeconds(100))
.timeout(Duration.ofSeconds(30))).subscribe();
while executing this i am getting below exception:
reactor.core.Exceptions$ErrorCallbackNotImplemented: java.lang.IllegalStateException: block()/blockFirst()/blockLast() are blocking, which is not supported in thread parallel-1
while going through webflux documentation i found that Calling block() function isn’t possible within Reactor thread. Such an attempt causes the above error:
To overcome that i tried below:
subscriptionQuery
.flatMap(a -> a.initialResult())
.filter(a -> Objects.nonNull(a))
.repeatWhen(Repeat.onlyIf(repeatContext -> true)
.exponentialBackoff(Duration.ofMillis(100), Duration.ofSeconds(100))
.timeout(Duration.ofSeconds(30)))
.subscribe();
but it is not giving me the desired result, i think i am missing something. can anyone please suggest the correct way to achieve this.
Thanks.