【问题标题】:Why is this persistent actor not working为什么这个持久的演员不起作用
【发布时间】:2016-04-03 16:18:59
【问题描述】:

代码如下:

import akka.persistence._
import akka.actor.{Actor, ActorRef, ActorSystem, Props, ActorLogging}


class Counter extends PersistentActor with ActorLogging {

  import Counter._

  var state: State = new State(0)

  override def receiveRecover: Receive = {
    case RecoveryCompleted => println("Recovery completed.")
    case SnapshotOffer(_, snapshot: State) => state = snapshot
    case op: Operation => updateState(op)
  }


  override def persistenceId: String = "counter-persistent"

  override def receiveCommand: Receive = {
    case op: Operation =>
      println(s"Counter receive ${op}")
      persist(op) {
        op => updateState(op)
      }
    case "print" => println(s"The current state of couter is ${state}")
    case SaveSnapshotFailure(_, reason) => println(s"save snapshot failed, reason: ${reason}")
    case SaveSnapshotSuccess(_) => println(s"snapshot saved")
  }

  def updateState(op: Operation): Unit = op match {
    case Increment(n) =>
      state = state.inc(n)
      takeSnapshot
    case Decrement(n) =>
      state = state.dec(n)
      takeSnapshot
  }

  def takeSnapshot: Unit = {
    //    if (state % 5 == 0) saveSnapshot()
    saveSnapshot()
  }
}


object Counter {

  sealed trait Operation {
    val count: Int
  }

  case class Increment(override val count: Int) extends Operation

  case class Decrement(override val count: Int) extends Operation

  final case class State(n: Int) {
    def inc(x: Int) = State(n + x)

    def dec(x: Int) = State(n - x)
  }

}







object Persistent extends App {

  import Counter._

  val system = ActorSystem("persistent-actors")

  val counter = system.actorOf(Props[Counter])

  counter ! Increment(3)
  counter ! Increment(5)
  counter ! Decrement(3)
  counter ! "print"

  Thread.sleep(1000)

  system.terminate()

}

配置(application.conf):

akka {
  persistence {
    journal {
      plugin = "akka.persistence.journal.leveldb",
      leveldb {
        dir = "target/example/journal",
        native = false
      }
    },
    snapshot-store {
      plugin = "akka.persistence.snapshot-store.local",
      local {
        dir = "target/example/snapshots"
      }
    }
  }
}

运行应用两次表明状态根本不是持久的:

Recovery completed.
Counter receive Increment(3)
Counter receive Increment(5)
Counter receive Decrement(3)
The current state of couter is State(5)
snapshot saved
snapshot saved
snapshot saved

Recovery completed.
Counter receive Increment(3)
Counter receive Increment(5)
Counter receive Decrement(3)
The current state of couter is State(5)
snapshot saved
snapshot saved
snapshot saved

为什么?

【问题讨论】:

  • 你配置持久化插件了吗?你用的是哪个期刊?
  • 您面临的问题是什么不起作用。请提供更多信息。
  • @manub 配置现已发布。
  • @curious 添加了详细信息。

标签: scala akka


【解决方案1】:

这里的问题是您在演员收到每条操作消息后拍摄快照,但在拍摄快照时您没有保存其状态。如果你仔细看你拍的快照代码:

def takeSnapshot: Unit = {
    //    if (state % 5 == 0) saveSnapshot()
    saveSnapshot()
  }

saveSnapshot() 的调用不会对您的状态进行快照,因为没有传递给它的参数。

你需要修改你的takeSnapshot方法有点像这样:

  def takeSnapshot: Unit = {
    //    if (state % 5 == 0) saveSnapshot()
    saveSnapshot(state) // Pass the states you need to store while taking a snapshot.
  }

这会起作用。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2023-03-03
    • 2023-03-08
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多