PersistentFSM不会保留到日志中

时间:2015-10-02 10:23:00

标签: scala akka actor akka-persistence

我正在尝试使用下面的简单示例测试Akka 2.4的新功能 - PersistentFSMhttp://doc.akka.io/docs/akka/2.4.0/scala/persistence.html#Persistent_FSM)。

此示例生成5个随机整数,并将其广告到序列中。我希望它能够持久保存数据,并在下一次运行应用程序时继续追加现有的数字序列。关于PersistentFSM的当前文档有点稀缺,并且没有明确的方法来完成这项工作。有什么想法吗?

TestFSM.scala:

import akka.actor.{Actor, ActorSystem, Props}
import akka.persistence.fsm.PersistentFSM
import akka.persistence.fsm.PersistentFSM.FSMState
import scala.reflect._
import scala.util.Random

final case class SetNumber(num: Integer)

sealed trait State extends FSMState
case object Idle extends State {
    override def identifier: String = "Idle"
}
case object Active extends State {
    override def identifier: String = "Active"
}

sealed trait Data {
    def add(number: Integer): Data
}
case object Empty extends Data {
    def add(number: Integer) = Numbers(Vector(number))
}
final case class Numbers(queue: Seq[Integer]) extends Data {
    def add(number: Integer) = Numbers(queue :+ number)
}

sealed trait DomainEvt
case class SetNumberEvt(num: Integer) extends DomainEvt

class Generator extends Actor with PersistentFSM[State, Data, DomainEvt] {

    override def applyEvent(domainEvent: DomainEvt, currentData: Data): Data = {
        domainEvent match {
            case SetNumberEvt(num) => currentData.add(num)
        }
    }

    override def persistenceId: String = "generator"

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

    startWith(Idle, Empty)

    when(Idle) {
        case Event(SetNumber(num), Empty) =>
            goto(Active) applying SetNumberEvt(num)
    }

    when(Active) {
        case Event(SetNumber(num), numbers: Data) =>
            println(numbers)
            stay applying SetNumberEvt(num)
    }

    initialize()

}

object TestFSM extends App {

    val system = ActorSystem()

    val actor = system.actorOf(Props[Generator])

    actor ! SetNumber(Random.nextInt())
    actor ! SetNumber(Random.nextInt())
    actor ! SetNumber(Random.nextInt())
    actor ! SetNumber(Random.nextInt())
    actor ! SetNumber(Random.nextInt())

    Thread.sleep(1000)
    system.terminate()

}

reference.conf:

akka.persistence.journal.plugin = "akka.persistence.journal.inmem"
akka.persistence.snapshot-store.plugin = "akka.persistence.snapshot-store.local"
akka.persistence.journal.leveldb.dir = "target/journal"
akka.persistence.snapshot-store.local.dir = "target/snapshots"

1 个答案:

答案 0 :(得分:3)

PersistentFSM现在隐含地在FSM继续时保持状态更改和域事件。

虽然您链接的文档不足,但可以从代码中看到persistAll在适当的时间点被调用:https://github.com/akka/akka/blob/v2.4.0/akka-persistence/src/main/scala/akka/persistence/fsm/PersistentFSM.scala#L110

建议你测试实现,看它是否确实存在。

编辑:

我能够执行您的代码,保持不变,并看到target/journal目录已创建。我使用了以下build.sbt

name := "Scratch"

version := "1.0"

scalaVersion := "2.11.7"

sbtVersion := "0.13.7"

mainClass := Some("TestFSM")

fork := true

libraryDependencies += "com.typesafe.akka" %% "akka-actor" % "2.4.0"

libraryDependencies += "com.typesafe.akka" %% "akka-persistence" % "2.4.0"

libraryDependencies += "org.iq80.leveldb"            % "leveldb"          % "0.7"

libraryDependencies += "org.fusesource.leveldbjni"   % "leveldbjni-all"   % "1.8"

以下application.conf

akka.persistence.journal.plugin = "akka.persistence.journal.leveldb"
akka.persistence.snapshot-store.plugin = "akka.persistence.snapshot-store.local"
akka.persistence.journal.leveldb.dir = "target/journal"
akka.persistence.snapshot-store.local.dir = "target/snapshots"

我认为application.conf是指定应用程序特定conf而不是reference.conf的适当方式,它适用于库。