0

Lately i've implemented a Reactive REST GET endpoint with Quarkus/Mutiny using a callback structure;

Connect MyRequestService to Reactive REST GET endpoint with Quarkus/Mutiny

After finishing, I was wondering how this is settled with a call to a blocking service;

How do i call a blocking service from my Reactive REST GET endpoint with Quarkus/Mutiny

Roland Beuker
  • 354
  • 1
  • 15

1 Answers1

0

I didn't see a quick answer in the documentation, but it turned out to be quite simple;

enter image description here

The ServiceResource just forwards the call to the Service.

enter image description here

MyRequestService creates a MyJsonResultSupplier and delivers this to the Mutiny Uni with method item(). The resulting Uni is returned to the ServiceResource.

enter image description here

Mutiny uses method get() on the Supplier for a MyJsonResult. The call blocks with an acquire on semaphore mMyJsonResultSupplierSemaphore. Next, another worker thread calls method ready() which sets mMyJsonResult and releases semaphore mMyJsonResultSupplierSemaphore unblocking method get() towards Mutiny.

Mutiny completely hides the reactive part of the story, so you can just block on a method call within a registered supplier.

Roland Beuker
  • 354
  • 1
  • 15
  • For a non-blocking example with a callback check; https://stackoverflow.com/questions/66761579/connect-myrequestservice-to-reactive-rest-get-endpoint-with-quarkus-mutiny – Roland Beuker Apr 05 '21 at 09:30
  • For a CompletionStage / CompletableFuture API version check; https://stackoverflow.com/questions/67038537/dispatch-a-completionstage-completablefuture-api-service-in-a-reactive-rest-ge – Roland Beuker Apr 10 '21 at 19:49
  • if you check `Thread.currentThread()` before `Uni.createFrom().item` and inside it, you will see that the thread will be `vert.x-eventloop-thread-*` for both places. So in this case you are still blocking event loop, what is bad. What you can do is to get vertx instance and execute blocking part with `vertx.executeBlocking()`, in this case it will be executed at worker thread. – quit Jan 27 '23 at 12:08