Skip to content

Instantly share code, notes, and snippets.

@akhilvijayan05
Created March 24, 2019 18:11
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save akhilvijayan05/140d232e409fbc856bcc8c6952b0193d to your computer and use it in GitHub Desktop.
Save akhilvijayan05/140d232e409fbc856bcc8c6952b0193d to your computer and use it in GitHub Desktop.
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