OnStateEvent
Marks a function as a state event handler for real-time state change notifications.
Functions annotated with @OnStateEvent are triggered when aggregate state changes occur, enabling reactive processing of state updates. Unlike @OnEvent which handles domain events, @OnStateEvent responds to state transitions and modifications.
State event handlers are useful for:
Real-time UI updates and notifications
Cross-aggregate state synchronization
External system integration based on state changes
Audit logging of state modifications
Cache invalidation and data consistency
Example usage:
@EventProcessor
class OrderStateProcessor {
@OnStateEvent // Listen to all aggregates
fun onAnyStateChange(changed: StateChanged, state: OrderState) {
// React to any state change
auditService.logStateChange(changed.aggregateId, changed.oldState, state)
}
@OnStateEvent(value = ["order"]) // Only order aggregates
fun onOrderStateChange(changed: StateChanged, state: OrderState) {
if (state.status == OrderStatus.SHIPPED) {
notificationService.sendShippingConfirmation(state.customerId)
}
}
}
// Remote context (when state is not locally available)
@OnStateEvent
fun onRemoteStateEvent(changed: StateChanged, stateRecord: StateRecord<OrderState>) {
val state = stateRecord.toState<OrderState>()
// Process remote state changes
}Parameters
Array of aggregate names to listen for state events from. If empty, listens to state events from all aggregates. Enables selective processing.
See also
for domain event handlers
for state-based aggregates