Last active
December 26, 2015 13:09
-
-
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).
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._ | |
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