2014-01-17 06:58:25 +01:00
|
|
|
package sample.persistence
|
|
|
|
|
|
2014-02-03 16:08:19 +01:00
|
|
|
import scala.concurrent.duration._
|
|
|
|
|
|
2014-01-17 06:58:25 +01:00
|
|
|
import akka.actor._
|
|
|
|
|
import akka.persistence._
|
|
|
|
|
|
|
|
|
|
object ViewExample extends App {
|
2014-06-25 12:51:21 +02:00
|
|
|
class ExamplePersistentActor extends PersistentActor {
|
2014-06-26 13:56:01 +02:00
|
|
|
override def persistenceId = "sample-id-4"
|
2014-01-17 06:58:25 +01:00
|
|
|
|
2014-06-25 12:51:21 +02:00
|
|
|
var count = 1
|
|
|
|
|
|
|
|
|
|
def receiveCommand: Actor.Receive = {
|
|
|
|
|
case payload: String =>
|
|
|
|
|
println(s"persistentActor received ${payload} (nr = ${count})")
|
|
|
|
|
persist(payload + count) { evt =>
|
|
|
|
|
count += 1
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
def receiveRecover: Actor.Receive = {
|
|
|
|
|
case _: String => count += 1
|
2014-01-17 06:58:25 +01:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2014-06-24 16:57:33 +02:00
|
|
|
class ExampleView extends PersistentView {
|
2014-01-17 06:58:25 +01:00
|
|
|
private var numReplicated = 0
|
|
|
|
|
|
2014-06-26 13:56:01 +02:00
|
|
|
override def persistenceId: String = "sample-id-4"
|
|
|
|
|
override def viewId = "sample-view-id-4"
|
2014-01-17 06:58:25 +01:00
|
|
|
|
|
|
|
|
def receive = {
|
|
|
|
|
case "snap" =>
|
|
|
|
|
saveSnapshot(numReplicated)
|
|
|
|
|
case SnapshotOffer(metadata, snapshot: Int) =>
|
|
|
|
|
numReplicated = snapshot
|
|
|
|
|
println(s"view received snapshot offer ${snapshot} (metadata = ${metadata})")
|
2014-06-24 16:57:33 +02:00
|
|
|
case payload if isPersistent =>
|
2014-01-17 06:58:25 +01:00
|
|
|
numReplicated += 1
|
2014-06-24 16:57:33 +02:00
|
|
|
println(s"view received persistent ${payload} (num replicated = ${numReplicated})")
|
|
|
|
|
case payload =>
|
|
|
|
|
println(s"view received not persitent ${payload}")
|
2014-01-17 06:58:25 +01:00
|
|
|
}
|
2014-06-23 14:33:35 +02:00
|
|
|
|
2014-01-17 06:58:25 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
val system = ActorSystem("example")
|
|
|
|
|
|
2014-06-25 12:51:21 +02:00
|
|
|
val persistentActor = system.actorOf(Props(classOf[ExamplePersistentActor]))
|
2014-01-17 06:58:25 +01:00
|
|
|
val view = system.actorOf(Props(classOf[ExampleView]))
|
|
|
|
|
|
2014-02-03 16:08:19 +01:00
|
|
|
import system.dispatcher
|
2014-01-17 06:58:25 +01:00
|
|
|
|
2014-06-25 12:51:21 +02:00
|
|
|
system.scheduler.schedule(Duration.Zero, 2.seconds, persistentActor, "scheduled")
|
2014-02-03 16:08:19 +01:00
|
|
|
system.scheduler.schedule(Duration.Zero, 5.seconds, view, "snap")
|
2014-01-17 06:58:25 +01:00
|
|
|
}
|