liveBook · Manning

@Nonblocking

As explained in Quarkus in Action

@NonBlocking is an annotation indicating that an operation should be executed without blocking the reactive event-loop execution model.

@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

How Quarkus in Action covers it

How deep it goes
Explains event-loop versus worker dispatch and blocked-thread consequences, but does not detail annotation internals.
Examples
Uses a reservation endpoint returning Uni, asynchronous persistence, and a blocked-event-loop warning.
What it assumes
Assumes awareness of reactive return types and distinguishes event-loop work from blocking worker-thread work.
What you can do after
Apply it via Quarkus to select event-loop execution and diagnose accidental blocking in reactive endpoints.
How it connects
Connects @NonBlocking with reactive programming, event loops, worker threads, Uni, Multi, and @Blocking.

Related concepts

This explainer was generated by Manning's knowledge graph from the text of Quarkus in Action. The section links point to the parts of the book it draws on.