我如何在Akka的persistFSM中恢复状态

时间:2018-08-23 06:59:07

标签: akka-persistent-fsm

大家好,我是akka的新手,遇到了问题。我在akka中使用PersistenceFSM进行项目的持久性和恢复。

但是,问题在于,它不会持久存在,并且不会从初始状态开始。

我希望在重新启动Actor时从上次失败的地方开始。

class ActorTracker(parent:Map[Int,List[Int]],ActorMap:Map[Int,ActorRef],toposort:Queue[Int])extends Actor with PersistentFSM[ActorTracker.orderActorprocessing,ActorTracker.completeActor,ActorTracker.DomainEvt]{
  var parentId:Int=0

  var counter:Int=0
  import ActorTracker._
  import context._
  implicit def executionContext = context.dispatcher



  override def persistenceId: String = "ActorTracker"

  override def domainEventClassTag: ClassTag[ActorTracker.DomainEvt] = classTag[ActorTracker.DomainEvt]

  override def applyEvent(domainEvent: ActorTracker.DomainEvt, currentData: completeActor): completeActor = {
    domainEvent match{
      case UpdateEvt(toposort)=>
        println(currentData.toposort)
        StartId(toposort)

    }
  }

//I want to persist event and after failure when we recover it Start from //where was event FAILED.
// but I cant persist and recover




  startWith(RunActor,StartId(toposort))


  when(RunActor) {
    case Event(Initialize,_)=>
      self!Startrun
      stay replying StartId(toposort)
    case Event(Startrun, _) =>
      //println(toposort)
      while(toposort.isEmpty==false && parent(toposort.front).isEmpty) {
        parentId=toposort.front
        toposort.dequeue()
        ActorMap(parentId) ! StartProcessing//(nameMap(parentId),childMap(parentId))
        goto(ActorComplete)
      }
      goto(ActorComplete)

  }

  when(ActorComplete){
    case Event(FailedActor,_)=>
      self!PoisonPill
      stay

    case Event(Executed,_)=>
      counter+=1
      self !Childrun
      stay applying UpdateEvt(toposort)


    case Event(Childrun,_)=>
      if(toposort.isEmpty) {
        println("terminating")
        self!PoisonPill
      }
      while(toposort.isEmpty==false &&parent(toposort.front).size==counter ){
        counter = 0
        parentId = toposort.front
        while(toposort.isEmpty==false&&parent(toposort.front).equals(parent(parentId))) {
          println(toposort.front)
          parentId=toposort.front
          ActorMap(parentId) ! StartProcessing
          toposort.dequeue()
          stay
        }
      }
      stay
  }
  whenUnhandled{
    case Event(e, s) ⇒
      log.warning("received unhandled request {} in state {}/{}", e, stateName, s)
      stay
  }
  override def receiveRecover: Receive = {
    case RecoveryCompleted =>println("recover"+toposort)
    // perform init after recovery, before any other messages
    //...
    case evt               =>println("recovering")
  }
}

要演示PersistentFSM特性的特征,请考虑代表Web商店客户的actor。我们的“ WebStoreCustomerFSMActor”的合同是接受

0 个答案:

没有答案