Quantcast
Channel: Akka Libraries - Discussion Forum for Akka technologies
Viewing all articles
Browse latest Browse all 1365

EventSourcedBehavior: send command to self from event handler

$
0
0

I have a situation where, on the first Command received by my behavior, I need to first retrieve some data from a DB to fully initialize the State, and only then process the Command.

What is the best way to do this?

I have tried doing something like the below. In the *** sections, do I need to use context.pipeToSelf again, and if I do, will the original command be processed after the state is updated?

def uninitializedCommandHandler(context, state, command) {
  command match {
     case InitializeCommand(dataFromDb, originalCommand) =>
        // *** can I send the originalCommand to myself here, or should I do it in the event handler ***
        Effect.persist(InitializedEvent(state.copy(data = dataFromDb), maybeOriginalCommand)
     case anyOtherCommand => 
        // here we need to load the data
        context.pipeToSelf(loadData) { case Success(data) => InitializeCommand(data, originalCommand) }
        Effect.none
  }
}

def handleEvent(context, state, event) {
  event match {
    case InitializedEvent(dataFromDb, originalCommand) =>
      // *** should I send the originalCommand to myself here? ***
      state.copy(data = dataFromDb)
  }
}

Thanks in advance.

1 post - 1 participant

Read full topic


Viewing all articles
Browse latest Browse all 1365

Trending Articles