I need to store to Cassandra and publish to Kafka multiple events, and call some final handler()
only after all events are stored and published.
I came across Update actor state only after all events are persisted approach, but it doesn't cover a case when events should be published to Kafka as well.
There is kafka publisher and base aggregate root actor that processes multiple events and then calls a handler()
(it is typically used to return response from the actor):
abstract class AggregateRootActor () extends ActorPersistence {
def processEvents(events: Seq[Event])(handler: Event => Unit): Unit = {
persistAll(events) { persistedEvent =>
state = //updateActorState
//publish messages to kafka
val futureResult = publisher.publishToKafka(event)
// where to switch context to handle `EventProcessingCompleted` after all events are
// published?
context.become {
case EventProcessingCompleted => handler(persistedEvent)
case ... //
}
}
self ! EventProcessingCompleted
}
}
Any suggested solutions are welcome!