Created
March 24, 2019 18:11
-
-
Save akhilvijayan05/140d232e409fbc856bcc8c6952b0193d to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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