Skip to content

Instantly share code, notes, and snippets.

@metamorph
Created December 5, 2012 18:12
Show Gist options
  • Save metamorph/4218066 to your computer and use it in GitHub Desktop.
Save metamorph/4218066 to your computer and use it in GitHub Desktop.
Akka - Resume in supervisor. I would expect MESSAGE_2 to reach the 'Node' actor.
[INFO] [12/05/2012 19:11:03.478] [Resume-akka.actor.default-dispatcher-1] [akka://Resume/user/super/manager] Relaying to node: MESSAGE_1
[INFO] [12/05/2012 19:11:03.478] [Resume-akka.actor.default-dispatcher-3] [akka://Resume/user/super/manager/node] Node got message: MESSAGE_1
[ERROR] [12/05/2012 19:11:03.479] [Resume-akka.actor.default-dispatcher-1] [akka://Resume/user/super/manager] java.lang.Exception: Crash
java.lang.RuntimeException: java.lang.Exception: Crash
at foo.Manager$$anonfun$receive$2.apply(Resume.scala:42)
at foo.Manager$$anonfun$receive$2.apply(Resume.scala:41)
at akka.actor.Actor$class.apply(Actor.scala:318)
at foo.Manager.apply(Resume.scala:37)
at akka.actor.ActorCell.invoke(ActorCell.scala:626)
at akka.dispatch.Mailbox.processMailbox(Mailbox.scala:197)
at akka.dispatch.Mailbox.run(Mailbox.scala:179)
at akka.dispatch.ForkJoinExecutorConfigurator$MailboxExecutionTask.exec(AbstractDispatcher.scala:516)
at akka.jsr166y.ForkJoinTask.doExec(ForkJoinTask.java:259)
at akka.jsr166y.ForkJoinPool$WorkQueue.runTask(ForkJoinPool.java:975)
at akka.jsr166y.ForkJoinPool.runWorker(ForkJoinPool.java:1479)
at akka.jsr166y.ForkJoinWorkerThread.run(ForkJoinWorkerThread.java:104)
Caused by: java.lang.Exception: Crash
at foo.Main$delayedInit$body.apply(Resume.scala:60)
at scala.Function0$class.apply$mcV$sp(Function0.scala:34)
at scala.runtime.AbstractFunction0.apply$mcV$sp(AbstractFunction0.scala:12)
at scala.App$$anonfun$main$1.apply(App.scala:60)
at scala.App$$anonfun$main$1.apply(App.scala:60)
at scala.collection.LinearSeqOptimized$class.foreach(LinearSeqOptimized.scala:59)
at scala.collection.immutable.List.foreach(List.scala:76)
at scala.collection.generic.TraversableForwarder$class.foreach(TraversableForwarder.scala:30)
at scala.App$class.main(App.scala:60)
at foo.Main$.main(Resume.scala:55)
at foo.Main.main(Resume.scala)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:57)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at java.lang.reflect.Method.invoke(Method.java:601)
at sbt.Run.invokeMain(Run.scala:68)
at sbt.Run.run0(Run.scala:61)
at sbt.Run.execute$1(Run.scala:50)
at sbt.Run$$anonfun$run$1.apply$mcV$sp(Run.scala:54)
at sbt.TrapExit$.executeMain$1(TrapExit.scala:33)
at sbt.TrapExit$$anon$1.run(TrapExit.scala:42)
[INFO] [12/05/2012 19:11:03.482] [Resume-akka.actor.default-dispatcher-1] [akka://Resume/user/super/manager] Relaying to node: MESSAGE_2
package foo
import akka.actor._
import akka.actor.OneForOneStrategy
import akka.actor.SupervisorStrategy._
import akka.pattern.ask
import akka.util.duration._
import akka.util.Timeout
/**
* Supervises and act as a proxy for the 'manager'.
*/
class Supervisor extends Actor {
// Spawn the child actor.
val manager = context.actorOf(Props[Manager], "manager")
context.watch(manager)
// Define the supervisor strategy for the manager
override val supervisorStrategy = OneForOneStrategy(
maxNrOfRetries = 1,
withinTimeRange = 1 second) {
case _ => Resume
}
def receive = {
case Terminated => context.stop(self) // The manager have stopped.
case msg => manager.forward(msg)
}
}
// Message that will be unwrapped and dispatched to the (child-) node.
case class ToNode(msg: Any)
/**
* Manages a Node.
*/
class Manager extends Actor with ActorLogging {
val node = context.actorOf(Props[Node], "node")
def receive = {
case ex: Exception => throw new RuntimeException(ex)
case ToNode(msg) =>
log.info("Relaying to node: {}", msg)
node.forward(msg)
}
}
class Node extends Actor with ActorLogging {
def receive = {
case msg => log.info("Node got message: {}", msg)
}
}
object Main extends App {
val system = ActorSystem("Resume")
val ref = system.actorOf(Props[Supervisor], "super")
ref ! ToNode("MESSAGE_1")
ref ! new Exception("Crash")
ref ! ToNode("MESSAGE_2")
Thread.sleep(2000L)
system.shutdown()
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment