Scala akka 事件源如何将消息返回到 root?
Scala akka event sourcing how to get a message back to root?
我目前正在努力读取我的演员的状态,所以在这种情况下,我只想从我的状态 class 中获取历史参数 - 因为示例在调用端点时打印它。
我已经成功做到了?运算符之前,但我从未尝试过使用事件源。
到目前为止我的代码是这样的:
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
谢谢!!
我没有在 akka 中使用过事件源,但快速浏览了文档,我认为这可能会有帮助:
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
还有这个 Effect
在 akka 的事件源文档中,看起来很有趣:
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()
}
}
如您所见,它非常易于使用。
也可以回复每个效果后的最新状态:
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
}
文档中还有很多其他选项,所以我强烈建议您看一下:https://doc.akka.io/docs/akka/current/typed/persistence.html
我目前正在努力读取我的演员的状态,所以在这种情况下,我只想从我的状态 class 中获取历史参数 - 因为示例在调用端点时打印它。
我已经成功做到了?运算符之前,但我从未尝试过使用事件源。
到目前为止我的代码是这样的:
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
谢谢!!
我没有在 akka 中使用过事件源,但快速浏览了文档,我认为这可能会有帮助:
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
还有这个 Effect
在 akka 的事件源文档中,看起来很有趣:
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()
}
}
如您所见,它非常易于使用。 也可以回复每个效果后的最新状态:
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
}
文档中还有很多其他选项,所以我强烈建议您看一下:https://doc.akka.io/docs/akka/current/typed/persistence.html