-
-
Save monkey-codes/2c18ea9f3bb87455267f21f974ad9dfe to your computer and use it in GitHub Desktop.
Subscription query example
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
| @Component | |
| class ActivityProjection( | |
| val queryUpdateEmitter: QueryUpdateEmitter | |
| ){ | |
| var lastActivity: Activity = ... | |
| @EventHandler(payloadType = OrderTakenEvent::class) | |
| fun onOrderTaken(event: EventMessage<OrderTakenEvent>) { | |
| lastActivity = Activity( | |
| eventId = UUID.fromString(event.identifier), | |
| entityId = entityId, | |
| activity = "Order taken") | |
| queryUpdateEmitter.emit( | |
| GetRecentActivityQuery::class.java, { query -> true }, lastActivity | |
| ) | |
| } | |
| @QueryHandler | |
| fun handle(query: GetRecentActivityQuery): Activity = lastActivity | |
| } | |
| @RestController | |
| @RequestMapping("/api") | |
| class ActivityQueryController( | |
| val queryGateway: QueryGateway | |
| ) { | |
| @GetMapping("/activity") | |
| fun activity(): ResponseEntity<SseEmitter> { | |
| val emitter = SseEmitter() | |
| val subscriptionQueryResult = queryGateway.subscriptionQuery( | |
| GetRecentActivityQuery(), | |
| Activity::class.java, Activity::class.java | |
| ) | |
| subscriptionQueryResult.handle({ initial -> | |
| emitter.send(event() | |
| .id(initial.eventId.toString()) | |
| .data(initial, MediaType.APPLICATION_JSON) | |
| ) | |
| }, { update -> | |
| ... | |
| emitter.send( | |
| event() | |
| .id(update.eventId.toString()) | |
| .data(update, MediaType.APPLICATION_JSON) | |
| ) | |
| ... | |
| }) | |
| ... | |
| return ResponseEntity.ok().headers(responseHeaders).body(emitter); | |
| } | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment