0

Spring boot 3.0.2 - I want to block in reactor thread to validate an event. Unless the event is validated , I do not want to move ahead. How do we achieve it ?

.block() -> throws exception , so it cannot be used.

java.lang.IllegalStateException: block()/blockFirst()/blockLast() are blocking, which is not supported in thread reactor-http-kqueue-7 at reactor.core.publisher.BlockingSingleSubscriber.blockingGet(BlockingSingleSubscriber.java:83) ~[reactor-core-3.5.2.jar:3.5.2] at reactor.core.publisher.Mono.block(Mono.java:1710) ~[reactor-core-3.5.2.jar:3.5.2]

Flux.fromIterable(messages).map((message) -> {
            try {
                return this.readValue(message);
            } catch (Exception var3) {
                throw new RuntimeException(var3);
            }
        }).flatMap(this::process).subscribeOn(Schedulers.boundedElastic()).doOnError((error) -> {
            this.messageErrorHandler.handle(error);
        }).blockLast(Duration.ofMillis(Long.parseLong((String)this.properties.get("blockLast.timeout.ms"))));

Validation is done inside process() method

eventValidator.validate(eventPayload);

validate makes IO call

Mono<Data> data = repo.getData();

So basically I want to block on this data.

ugexe
  • 5,297
  • 1
  • 28
  • 49
Anshu Pandey
  • 49
  • 1
  • 4
  • share your code – kerbermeister Mar 15 '23 at 10:10
  • Maybe one of these answers can help you : [Blocking cause IllegalStateException](https://stackoverflow.com/a/67549756/2678097) or [In-depth explanation about block operation](https://stackoverflow.com/a/75096742/2678097) – amanin Mar 15 '23 at 10:54
  • why do you want to _block_? – Igor Artamonov Mar 15 '23 at 14:24
  • you are making subscribeOn() that means your pipeline will be in a new thread even blockLast(); – toootooo Mar 15 '23 at 15:43
  • @IgorArtamonov I want to block bcoz unless the validation of data with Message payload from request is not successfull, I do not want to save the message in repository. So one thing I thought of was - Can I make validation reactive and onSuccess of it, I can make the call to save in repository ? – Anshu Pandey Mar 15 '23 at 20:57
  • You should not block in this case. Just use `flatMap` – Igor Artamonov Mar 16 '23 at 03:17
  • The idea of using reactor is to make your end 2 end request reactive, Assuming you are using this with webflux which internally subscribes to your reactive pipeline, you should not use block. The processing should stop if your flatmap throws an error. – confused Mar 16 '23 at 03:27
  • If you have a blocking api there isn't much you can do (if the api returns a concrete value instead of some kind of publisher/future/promise, then its blocking). You might be able to block a thread using pools but you're not really reactive at that point. – Adam Bickford Mar 17 '23 at 15:34

0 Answers0