【问题标题】:How can akka persistent actor be started with guaranteed particular message received first?akka 持久性参与者如何在首先收到保证的特定消息的情况下启动?
【发布时间】:2015-03-31 15:38:58
【问题描述】:

我的演员扩展了 akka.persistence.PersistentActor。 我想首先发送一个消息案例类 SetConfig(config: String) 并确保在 SetConfig 被持久化和应用之前没有处理其他消息。

实现这一目标的最佳做法是什么?

【问题讨论】:

    标签: scala akka actor persistent


    【解决方案1】:

    我会使用 Akka FSMPersistentActor

    你可以有这两种状态:

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

    还有这个数据:

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

    你的演员将扩展FSM[State, Data],并将:

    startWith(Idle, Uninitialized)
    

    那么你可以说当Idle时你只接受SetConfig消息:

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

    一旦您转换到Active,您可以通过以下方式接收其他消息:

    when(Active) {
      case Event(...
    
    // don't forget to start it up in initial state with:
    initialize()
    

    最后,在状态转换中,您可以使用常规 Akka Persistence 模式来持久化您的状态。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2018-10-06
      • 1970-01-01
      • 2014-07-29
      • 1970-01-01
      • 1970-01-01
      • 2016-08-06
      • 2016-10-22
      • 1970-01-01
      相关资源
      最近更新 更多