@NonBlocking tells Quarkus to execute an operation on the reactive event-loop thread rather than a worker thread. This keeps the event loop available for other work while scheduled I/O progresses, but the operation must not block. Quarkus normally infers dispatch from the method signature: reactive return types such as Uni use the event loop, while other return types are treated as potentially blocking. section 8.4.1 section 8.1.1
Use @NonBlocking when the method signature does not otherwise communicate the intended execution model:
- Apply it to a blocking-looking method when the implementation is actually nonblocking. section 8.4.1
- Keep blocking work off the event loop; Quarkus provides @Blocking to dispatch such work to a worker thread. section 8.1.1
- Treat attempts to block a Uni-based operation as an error condition; Quarkus can report the blocked event-loop thread. section 8.4.1
Listing 8.3 Nonblocking version of ReservationResource#make method
@Consumes(MediaType.APPLICATION_JSON)
@POST
@WithTransaction
public Uni<Reservation> make(Reservation reservation) {
...
return reservation.<Reservation>persist().onItem() #1
.call(persistedReservation -> {
...
});
}
A reservation endpoint returns Uni and persists asynchronously, illustrating a nonblocking resource method whose continuation resumes when the result becomes available. section 8.4.1
