Uni is SmallRye Mutiny’s type for an asynchronous computation that emits one item or a failure. Use it when a service operation has one eventual result, such as a gRPC response or a persisted entity. Multi is the corresponding type for a stream of results. section 8.2.2 section 4.8
A Uni can be part of a reactive pipeline: later stages run when an earlier stage emits an item. The pipeline starts when subscribed to; for Uni, calling subscribe directly triggers the computation. Unlike a full Reactive Streams publisher, Uni can also emit null. section 8.2.2
Return one gRPC response
A generated InventoryService declares add and remove methods returning Uni<CarResponse>. An implementation can update the inventory and return a response with Uni.createFrom().item(response). Quarkus manages the gRPC server through its gRPC extension. section 4.8
Reactive data access changes what a method receives, not just when it receives it. Hibernate Reactive Panache returns Uni<E> or Uni<List<E>> where blocking Panache returned E or List<E>. In a reservation endpoint, persist() returns a Uni<Reservation>; an onItem continuation runs after persistence completes, without keeping the I/O thread blocked while the database processes the insert. section 7.7.1 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 -> {
...
});
}
The reservation method shows a Uni<Reservation> returned from a REST endpoint and an onItem continuation chained to persist(). section 8.4.1
Returning Uni from a JAX-RS method lets Quarkus run it on an I/O thread and resume the request when the result is available. Blocking on the Uni instead gives up that nonblocking behavior. To use reactive PostgreSQL persistence, replace the blocking Panache and JDBC extensions with Hibernate Reactive Panache and the reactive PostgreSQL client. section 8.4.1 section 7.7.1
