2013-09-14 14:19:18 +02:00
|
|
|
/**
|
2014-02-02 19:05:45 -06:00
|
|
|
* Copyright (C) 2009-2014 Typesafe Inc. <http://www.typesafe.com>
|
2013-09-14 14:19:18 +02:00
|
|
|
*/
|
|
|
|
|
|
|
|
|
|
package sample.persistence
|
|
|
|
|
|
|
|
|
|
import akka.actor._
|
|
|
|
|
import akka.persistence._
|
|
|
|
|
|
|
|
|
|
object ConversationRecoveryExample extends App {
|
|
|
|
|
case object Ping
|
|
|
|
|
case object Pong
|
|
|
|
|
|
|
|
|
|
class Ping extends Processor {
|
|
|
|
|
val pongChannel = context.actorOf(Channel.props, "pongChannel")
|
|
|
|
|
var counter = 0
|
|
|
|
|
|
|
|
|
|
def receive = {
|
2013-12-03 16:34:26 +01:00
|
|
|
case m @ ConfirmablePersistent(Ping, _, _) =>
|
2013-09-14 14:19:18 +02:00
|
|
|
counter += 1
|
|
|
|
|
println(s"received ping ${counter} times ...")
|
|
|
|
|
m.confirm()
|
2013-11-07 10:45:02 +01:00
|
|
|
if (!recoveryRunning) Thread.sleep(1000)
|
2014-01-17 06:58:25 +01:00
|
|
|
pongChannel ! Deliver(m.withPayload(Pong), sender.path)
|
2014-02-03 16:08:19 +01:00
|
|
|
case "init" if (counter == 0) =>
|
|
|
|
|
pongChannel ! Deliver(Persistent(Pong), sender.path)
|
2013-09-14 14:19:18 +02:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
class Pong extends Processor {
|
|
|
|
|
val pingChannel = context.actorOf(Channel.props, "pingChannel")
|
|
|
|
|
var counter = 0
|
|
|
|
|
|
|
|
|
|
def receive = {
|
2013-12-03 16:34:26 +01:00
|
|
|
case m @ ConfirmablePersistent(Pong, _, _) =>
|
2013-09-14 14:19:18 +02:00
|
|
|
counter += 1
|
|
|
|
|
println(s"received pong ${counter} times ...")
|
|
|
|
|
m.confirm()
|
2013-11-07 10:45:02 +01:00
|
|
|
if (!recoveryRunning) Thread.sleep(1000)
|
2014-01-17 06:58:25 +01:00
|
|
|
pingChannel ! Deliver(m.withPayload(Ping), sender.path)
|
2013-09-14 14:19:18 +02:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
val system = ActorSystem("example")
|
|
|
|
|
|
|
|
|
|
val ping = system.actorOf(Props(classOf[Ping]), "ping")
|
|
|
|
|
val pong = system.actorOf(Props(classOf[Pong]), "pong")
|
|
|
|
|
|
|
|
|
|
ping tell ("init", pong)
|
|
|
|
|
}
|