首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >为什么这个顽固的演员不能工作?

为什么这个顽固的演员不能工作?
EN

Stack Overflow用户
提问于 2016-03-30 15:56:07
回答 1查看 447关注 0票数 1

以下是代码:

代码语言:javascript
复制
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):

代码语言:javascript
复制
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"
      }
    }
  }
}

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

代码语言:javascript
复制
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

为什么?

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2016-04-03 08:18:59

这里的问题是,在参与者接收到的每一条操作消息之后,您都要获取快照,但是在获取快照时,您并没有保存它的状态。如果仔细查看您的takeSnapshot代码:

代码语言:javascript
复制
def takeSnapshot: Unit = {
    //    if (state % 5 == 0) saveSnapshot()
    saveSnapshot()
  }

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

您需要修改takeSnapshot方法,如下所示:

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

这会管用的。

票数 3
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/36313962

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档