Akka持久性 - LevelDB

时间:2015-11-06 13:42:08

标签: akka akka-persistence

持久性Actor一次接收一条消息。在持续100条消息后,它应该将这100条消息发送到目标Actor。我打算在保存快照后删除这些消息。

我每50条消息后正在快照。我正在接收SavingSnapshotSuccess后删除消息。但是没有发生删除。我仍然会在日志中看到这些消息被追加。没有收到DeleteMessagesSuccess / Failure Message。删除快照时也没有收到DeleteSnapshotsSuccess / Failure。

以下是版本:

"com.typesafe.akka"%%   "akka-actor" % "2.4.0",
"com.typesafe.akka"%%   "akka-cluster"  % "2.4.0",
"com.typesafe.akka"%%   "akka-contrib"   % "2.3.14",
"com.typesafe.akka"%%   "akka-slf4j"% "2.4.0",
"com.typesafe.akka"%%   "akka-testkit"% "2.4.0" % "test",
"com.typesafe.akka"%%   "akka-persistence"   % "2.4.0"

代码:

class Consumer extends PersistentActor with ActorLogging{

  override def persistenceId = "bulkMessageConsumer-1"

  var state = BulkState()

  def updateState(event: Event): Unit = { 
    state = state.updated(event)
   }

  def numEvents = state.size

  val receiveRecover: Receive = {
   case evt: Event                                 => updateState(evt)
   case SnapshotOffer(_, snapshot: BulkState) => state = snapshot
  }

  val receiveCommand: Receive = {

case Snapshoot ⇒
  saveSnapshot(state)

  case sss @ SaveSnapshotSuccess(metadata) ⇒
  deleteMessages(toSequenceNr = metadata.sequenceNr - 1)
  deleteSnapshots(SnapshotSelectionCriteria(maxSequenceNr = metadata.sequenceNr - 1, maxTimestamp = metadata.timestamp - 1))

case SaveSnapshotFailure(metadata, reason) ⇒
  log.error(reason, "Unable to save snapshot [metadata: {}]", metadata)

case c @ Command(data) =>
  persistAsync(Event(c.data)) { event =>
    basicChecks
    updateState(event)
  }

case DeleteSnapshotsSuccess =>
   log.info("deleting snapshots")

case DeleteSnapshotsFailure =>
  log.info("deleting snapshots failure")

case dms @ DeleteMessagesSuccess =>
  log.info("messages deleted succesfully:")

case DeleteMessagesFailure =>
  log.info("messages deletion failure")

case Print =>
   println(state)
}

def send(): Unit ={
  val framedMessage = MyMessage
  DestinationActorRef ! framedMessage
  remove()
}

def remove(): Unit ={
  state = state.removeAll
}

def checkNumEventsReachBulkSize: Boolean ={
    numEvents==100
 }

 def checkSnapshotBatch :Boolean ={
    numEvents % 50 == 0 && numEvents >0
 }

 def basicChecks: Unit ={
    if(checkSnapshotBatch){
    self ! Snapshoot
 }

if(checkNumEventsReachBulkSize){
  send()
   }
 }

}

0 个答案:

没有答案