Repository navigation
feat: reactive SSE stream of new orders - #9
Merged
Merged
Conversation
…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.
20 tasks done
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Branch
feature/reactive-endpoints (bonus)
Task checklist
quarkus-hibernate-reactive-panacheandquarkus-reactive-pg-clientnext to the blocking JDBC stack (namedreactivepersistence unit +streamingreactive datasource, Dev Services provisions one container)OrderPanacheRepositoryReactiveused only by the streaming endpointAFTER_SUCCESS) published byOrderServiceImpl.create, bridged into aMultibyOrderEventBroadcasterGET /api/v1/orders/streamreturningMulti<OrderResponse>as SSE (ORDER:READ)AssertSubscriberbroadcast tests and an HTTP SSE test, without blocking the test threadCommit summary
ReactiveOrder, reactive repository, event, broadcaster, event publication, SSE endpointTest checklist
mvn verifyis green on the whole project (188 tests)Code review checklist
ApiResponse<T>(a stream of events is the documented exception), errors are RFC 9457 Problem Details@PermissionsAllowed), no manual permissionif@CacheResulthas its matching@CacheInvalidate(unchanged)AuditLogger(n/a)Known limits:
@Blockingonstream()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.