Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
import akka.actor.Props
import akka.persistence.{PersistentActor, SnapshotOffer}
import com.knoldus.models._
import com.knoldus.persistence.CounterPersistentActor.Response
class CounterPersistentActor(id: String) extends PersistentActor {
override val persistenceId: String = id
var state = State(count = 0)
def updateState(event:Event) = {
event match {
case Event(Increment(count)) => state = State(state.count + count)
case Event(Decrement(count)) => state = State(state.count - count)
}
}
override def receiveRecover: Receive = {
case event: Event =>
println(s"Actor is currently recovering its state")
updateState(event)
case SnapshotOffer(_, snapshot: State) =>
println(s"Snapshot data: $snapshot")
state = snapshot
}
override def receiveCommand: Receive = {
case command @ Command(op) =>
println(s"$command is under process")
persist(Event(op)) { event =>
updateState(event)
sender() ! Response("Done Processing")
}
case Checkpoint =>
println(s"Current State: ${state.count}")
sender() ! Response(s"Current State: ${state.count}")
}
}
object CounterPersistentActor {
def props(id: String) = Props(new CounterPersistentActor(id))
case class Response(message: String)
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment