0
votes

My actor extends akka.persistence.PersistentActor. I want to send a message case class SetConfig(config: String) at first and make sure no other message is processed before SetConfig is persisted and applied.

What is the best practice to achieve this ?

1

1 Answers

1
votes

I would use Akka FSM together with PersistentActor.

You can have these 2 states:

sealed trait State
case object Idle extends State
case object Active extends State

and this data:

sealed trait Data
case object Uninitialized extends Data
final case class Config(config: String) extends Data

you actor will extend FSM[State, Data], and will:

startWith(Idle, Uninitialized)

Then you can say that you only accept SetConfig messages when Idle:

when(Idle) {
  case Event(SetConfig(conf), Uninitialized) =>
    goto(Active) using Config(conf)
}

and once you transition to Active you can receive other messages with:

when(Active) {
  case Event(...

// don't forget to start it up in initial state with:
initialize()

Finally on state transitions you can persist your state using regular Akka Persistence patterns.