Skip to content

Instantly share code, notes, and snippets.

@jasongoodwin
Last active December 26, 2015 13:09
Show Gist options
  • Save jasongoodwin/7156238 to your computer and use it in GitHub Desktop.
Save jasongoodwin/7156238 to your computer and use it in GitHub Desktop.
Demonstrates changing the default Stop supervision behaviour on a top level actor encountering an exception/error during initialization (above /user guardian actor). Demonstrates how exceptions are thrown out of akka extensions as well (they throw like any other class).
import akka.actor._
import akka.actor.SupervisorStrategy._
import akka.routing.RoundRobinRouter
object Main {
val system = ActorSystem("TestActorSystem")
val escalator = OneForOneStrategy() {
case e: ActorInitializationException ⇒
println("Got exception during actor start: " + e)
throw new Error
Stop
}
val router = system.actorOf(Props[TestActor].withRouter(
RoundRobinRouter(1, supervisorStrategy = escalator)))
def main(args: Array[String]) {
router ! "HelloWorld"
}
}
class TestActor extends Actor {
override def preStart {
TestExtension(context.system) //Throws an error. Gets wrapped in ActorInitializationException. Causes actor stop.
}
def receive = {
case msg ⇒ println(msg)
}
}
class TestExtensionImpl extends Extension {
throw new Error("This is the error")
}
object TestExtension
extends ExtensionId[TestExtensionImpl]
with ExtensionIdProvider {
override def lookup = TestExtension
override def createExtension(system: ExtendedActorSystem) = new TestExtensionImpl
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment