OnSourcing

@OnMessage(functionKind = FunctionKind.SOURCING, defaultFunctionName = "onSourcing")
annotation class OnSourcing

Marks a function as a state sourcing handler.

Functions annotated with @OnSourcing are responsible for rebuilding aggregate state from historical events during event sourcing. They transform event streams into current aggregate state, enabling state recovery and reconstruction.

State sourcing handlers:

  • Are called during aggregate initialization from event store

  • Must be deterministic (same events always produce same state)

  • Should not have side effects (no external system calls)

  • Are applied in event order to build current state

  • Enable state snapshots and performance optimization

Example usage:

class OrderState{

@OnSourcing
fun onOrderCreated(event: OrderCreated) {
this.status = OrderStatus.CREATED
this.items.addAll(event.items)
this.totalAmount = event.totalAmount
}

@OnSourcing
fun onOrderPaid(event: OrderPaid) {
this.status = OrderStatus.PAID
this.paidAt = event.paidAt
}
}

See also

StateAggregate

for state-based aggregates that use sourcing

for event reaction handlers