【发布时间】:2022-08-18 17:39:24
【问题描述】:
我现在在努力阅读我的演员的状态,所以在这种情况下,我只想从我的 State 类中获取历史参数 - 例如在调用端点时打印它。
我已经成功地做到了?之前的操作员,但我从未尝试过事件溯源。
到目前为止,我的代码是这样的:
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)
}
在我的主要方法中,我想打印状态:
val personActor: ActorSystem[MyPersistentBehavior.Command] = ActorSystem(MyPersistentBehavior(\"IDDD\"), \"AHA\")
//personActor ? GetState <- something like this
谢谢!!
标签: scala state akka event-sourcing