liveBook · Manning

Uni

As explained in Quarkus in Action

Uni is a Mutiny type representing an asynchronous computation that emits one item or a failure.

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

How Quarkus in Action covers it

How deep it goes
Defines its item-or-failure behavior and subscription semantics, then shows service-level use without detailing Uni’s internals.
Examples
Shows a gRPC inventory response and a reservation endpoint chaining onItem after persist().
What it assumes
Assumes familiarity with Java service methods and Panache while defining Uni and contrasting it with Multi.
What you can do after
Apply it via Mutiny and Quarkus to return single asynchronous results from gRPC, persistence, and REST operations.
How it connects
Connects Uni to Multi, reactive pipelines, Hibernate Reactive Panache, gRPC, and JAX-RS.

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.