Scala 为什么这个坚持不懈的参与者不起作用

Scala 为什么这个坚持不懈的参与者不起作用,scala,akka,Scala,Akka,代码如下: 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: R

代码如下:

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):

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

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

为什么?

这里的问题是,在参与者收到每个操作消息后都要拍摄快照,但拍摄快照时没有保存其状态。如果仔细查看快照代码:

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.
  }

这将起作用。

您是否配置了持久性插件?您使用的是哪种日志?您面临的问题是什么?哪些日志不起作用。请提供更多信息。@manub配置现已发布。@好奇的详细信息已添加。
  def takeSnapshot: Unit = {
    //    if (state % 5 == 0) saveSnapshot()
    saveSnapshot(state) // Pass the states you need to store while taking a snapshot.
  }