Home > Blockchain >  Scala akka event sourcing how to get a message back to root?
Scala akka event sourcing how to get a message back to root?

Time:05-27

I'm currently struggling with reading the state of my actor, so in this case I just want to get the history parameter from my State class - for example print it when an endpoint is called.

I've successfully managed to do it with ? operator before but I've never tried it with event sourcing.

So far the code I have is this:

object MyPersistentBehavior {
  sealed trait Command
  final case class Add(data: String) extends Command
  case object Clear extends Command

  sealed trait Event
  final case class Added(data: String) extends Event
  case object Cleared extends Event

  final case class State(history: List[String] = Nil)

  val commandHandler: (State, Command) => Effect[Event, State] = { (state, command) =>
    command match {
      case Add(data) => Effect.persist(Added(data))
      case Clear     => Effect.persist(Cleared)
    }
  }

  val eventHandler: (State, Event) => State = { (state, event) =>
    event match {
      case Added(data) => state.copy((data :: state.history).take(5))
      case Cleared     => State(Nil)
    }
  }

  def apply(id: String): Behavior[Command] =
    EventSourcedBehavior[Command, Event, State](
      persistenceId = PersistenceId.ofUniqueId(id),
      emptyState = State(Nil),
      commandHandler = commandHandler,
      eventHandler = eventHandler)
}

In my main method I want to print the state:

val personActor: ActorSystem[MyPersistentBehavior.Command] = ActorSystem(MyPersistentBehavior("IDDD"), "AHA")
//personActor ? GetState <- something like this

Thanks!!

CodePudding user response:

I've not worked with event sourcing in akka, but had a quick look in the documentations, and I though this migh help:

case class GetState(replyTo: ActorRef[StatusReply[AddPostDone]]) extends Command

// and in the match for commands:
...
    case GetState(replyTo) =>
      replyTo ! StatusReply.Success(state)

      // or if replyTo was of type ActorRef[State] => 
      replyTo ! state

There is also this Effect which is in akka's event sourcing documentation which looks interesting:

def onCommand(subscriber: ActorRef[State], state: State, command: Command): Effect[Event, State] = {
  command match {
    case Add(data) =>
      Effect.persist(Added(data)).thenRun(newState => subscriber ! newState)
    case Clear =>
      Effect.persist(Cleared).thenRun((newState: State) => subscriber ! newState).thenStop()
  }
}

And is pretty easy to use as you can see. You can also reply the latest state after each effect:

case class AddCommand(number: Int, replyTo: ActorRef[State]) extends Command

// in the command handler
    case add: AddCommand(num, replyTo) => 
      // your logic here
      Event.persist(Added(num)) thenRun { newState => 
        replyTo ! newState
      }

There are lots of other options in the documentation, so I highly recommend you to take a look: https://doc.akka.io/docs/akka/current/typed/persistence.html

  • Related