Skip to content

feat: reactive SSE stream of new orders - #9

Merged
EdgarEldy merged 14 commits into
developfrom
feature/reactive-endpoints
Sep 19, 2026
Merged

EdgarEldy merged 14 commits into
developfrom
feature/reactive-endpoints

Conversation

@EdgarEldy

Copy link
Copy Markdown
Owner

Branch

feature/reactive-endpoints (bonus)

Task checklist

  • quarkus-hibernate-reactive-panache and quarkus-reactive-pg-client next to the blocking JDBC stack (named reactive persistence unit + streaming reactive datasource, Dev Services provisions one container)
  • OrderPanacheRepositoryReactive used only by the streaming endpoint
  • CDI event (AFTER_SUCCESS) published by OrderServiceImpl.create, bridged into a Multi by OrderEventBroadcaster
  • GET /api/v1/orders/stream returning Multi<OrderResponse> as SSE (ORDER:READ)
  • Tests: AssertSubscriber broadcast tests and an HTTP SSE test, without blocking the test thread

Commit summary

  • dependencies, persistence unit / datasource configuration, docker-compose reactive URL
  • ReactiveOrder, reactive repository, event, broadcaster, event publication, SSE endpoint
  • fix from code review: a failed reload no longer terminates the stream
  • tests and README ticked

Test checklist

  • Unit tests pass
  • Integration tests pass (where the branch's checklist calls for them)
  • mvn verify is green on the whole project (188 tests)

Code review checklist

  • No business logic in JAX-RS resources
  • Success responses are wrapped in ApiResponse<T> (a stream of events is the documented exception), errors are RFC 9457 Problem Details
  • Permission checks are declarative (@PermissionsAllowed), no manual permission if
  • Every @CacheResult has its matching @CacheInvalidate (unchanged)
  • Every RBAC mutation calls AuditLogger (n/a)
  • The database schema only changes through a Flyway migration (no change)
  • No manual Testcontainers setup (Dev Services only)
  • Commits are atomic, follow Conventional Commits, no em dash, no Claude/Anthropic mention

Known limits: @Blocking on stream() because the JWT blacklist filter uses JDBC; each SSE client reloads the row itself; the native build with these extensions is verified only by the CI native job.

…QL client

The two extensions sit next to the blocking JDBC stack. Both talk to the same PostgreSQL database, and Dev Services still provisions the container, so the tests need no manual container setup.

Their versions come from the Quarkus BOM. The reactive slice of the project is deliberately small, one streaming endpoint, to show that blocking and reactive code coexist in one application.
…atasource

A persistence unit is either blocking, over JDBC, or reactive, over the Vert.x client, never both. The existing entities stay in the default unit, and a named reactive unit maps only the package holding the streaming entity. Schema management stays off there too, because Flyway is the only owner of the schema.

The named unit needs a named reactive datasource, which reuses the connection Dev Services computes for the default one, so no second empty database starts. In production the reactive client expects a postgresql:// URL and not the JDBC one, so it reads its own variable.
The packaged application now needs a reactive URL in addition to the JDBC one, since the reactive client does not accept the jdbc: form. It points at the same host and database, and the user name and password are reused, so nothing new has to be set in the environment file.
This read-only mapping of the orders table lives in its own package because it belongs to the reactive unit and not to the blocking one. Customer and product are mapped as plain id columns, so the blocking entities never have to be dragged into the reactive unit.

It is never written: the blocking order service owns the writes and Flyway owns the schema. The entity only exists so the streaming endpoint can reload a committed order without blocking.
It follows the same repository pattern as the blocking side, taken from the reactive Panache variant: every method returns a Mutiny Uni, an asynchronous result of zero or one item, instead of a value. It is used only by the streaming endpoint, and the blocking repository and service keep serving everything else.
The event is a plain record carrying only the id of the order. Consumers reload the row after the commit, so the event never holds an entity that could be stale or attached to a closed session.
The bean observes the order created event with the AFTER_SUCCESS transaction phase, so the observer only runs once the creating transaction commits and never for a rolled back order. It pushes the id into a hot broadcast processor, which delivers it to every current subscriber and replays nothing to late ones.

Each subscription gets its own bounded buffer, so a slow client fails alone and order creation never waits for anyone. It also hops back onto the Vert.x context of the request before reloading the order through the reactive repository, since Hibernate Reactive must run on such a context.
After persisting the order, the blocking service fires a synchronous CDI event inside its transaction. It knows nothing about who listens, which keeps the imperative code untouched and lets the reactive side decide when to react: observers declared for the after-success phase only run once the transaction commits.
GET /api/v1/orders/stream returns a Mutiny Multi as Server-Sent Events: one long-lived response with one JSON event per new order. Quarkus REST subscribes and applies backpressure, so there is no manual thread handling. The success envelope does not apply to a stream, each element is a bare order response, and the same ORDER:READ permission protects it.

The method is marked blocking because the token blacklist filter runs a JDBC query, which is forbidden on the event loop where a Multi endpoint would otherwise be dispatched. That only concerns the request pipeline, the stream itself stays reactive.
Creating an order now fires an event, so the plain Mockito tests, which build the service without a container, need a mock of the event source to avoid a null pointer. Their assertions are unchanged.
The tests subscribe with an AssertSubscriber and wait with a timeout, so the test thread is never blocked. They check that a committed order arrives with all its fields, that a rolled back one emits nothing, that two subscribers both receive it, and that a late subscriber gets no replay.

The subscription runs in a duplicated Vert.x context marked safe, which is what Quarkus REST provides for each request and what Hibernate Reactive Panache requires.
A JDK HTTP client opens the stream asynchronously, an order is then posted through the normal endpoint, and a data line with the new order must arrive within a timeout. The test also checks the event stream content type, the 401 without a token and the 403 without the read permission, and closes the stream at the end so no connection leaks.
The five tasks are ticked: the reactive dependencies and persistence unit, the reactive repository, the CDI event bridged into a Multi, the SSE endpoint, and the tests.
Each broadcast id is reloaded from the database before it is sent. A missing row or a transient error used to fail that step and terminate the whole subscription, so a client would silently lose every later order until it reconnected.

A missing row is now skipped, and a failed reload is logged and dropped, so one bad element costs one event and not the connection.
@EdgarEldy
EdgarEldy merged commit 6f57114 into develop Sep 19, 2026
3 checks passed
@EdgarEldy EdgarEldy mentioned this pull request Sep 19, 2026
20 tasks done
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant