Added trapExceptions to declarative supervisor configuration

This commit is contained in:
jboner 2009-11-30 21:22:24 +01:00
parent 72eda9780f
commit 7b1bae36a8
15 changed files with 50 additions and 41 deletions

View file

@ -23,7 +23,7 @@ case class OneForOneStrategy(maxNrOfRetries: Int, withinTimeRange: Int) extends
* <pre>
* val factory = SupervisorFactory(
* SupervisorConfig(
* RestartStrategy(OneForOne, 3, 10),
* RestartStrategy(OneForOne, 3, 10, List(classOf[Exception]),
* Supervise(
* myFirstActor,
* LifeCycle(Permanent)) ::
@ -43,6 +43,7 @@ case class OneForOneStrategy(maxNrOfRetries: Int, withinTimeRange: Int) extends
* @author <a href="http://jonasboner.com">Jonas Bon&#233;r</a>
*/
class SupervisorFactory(val config: SupervisorConfig) extends Logging {
type ExceptionList = List[Class[_ <: Throwable]]
def newInstance: Supervisor = newInstanceFor(config)
@ -55,10 +56,10 @@ class SupervisorFactory(val config: SupervisorConfig) extends Logging {
}
protected def create(strategy: RestartStrategy): Supervisor = strategy match {
case RestartStrategy(scheme, maxNrOfRetries, timeRange) =>
case RestartStrategy(scheme, maxNrOfRetries, timeRange, trapExceptions: ExceptionList) =>
scheme match {
case AllForOne => new Supervisor(AllForOneStrategy(maxNrOfRetries, timeRange))
case OneForOne => new Supervisor(OneForOneStrategy(maxNrOfRetries, timeRange))
case AllForOne => new Supervisor(AllForOneStrategy(maxNrOfRetries, timeRange), trapExceptions)
case OneForOne => new Supervisor(OneForOneStrategy(maxNrOfRetries, timeRange), trapExceptions)
}
}
}
@ -79,21 +80,21 @@ object SupervisorFactory {
*
* @author <a href="http://jonasboner.com">Jonas Bon&#233;r</a>
*/
sealed class Supervisor private[akka] (handler: FaultHandlingStrategy)
extends Actor with Logging with Configurator {
trapExit = List(classOf[Throwable])
sealed class Supervisor private[akka] (handler: FaultHandlingStrategy, trapExceptions: List[Class[_ <: Throwable]])
extends Actor with Logging with Configurator {
trapExit = trapExceptions
faultHandler = Some(handler)
dispatcher = Dispatchers.newThreadBasedDispatcher(this)
val actors = new ConcurrentHashMap[String, Actor]
private val actors = new ConcurrentHashMap[String, Actor]
// Cheating, should really go through the dispatcher rather than direct access to a CHM
def getInstance[T](clazz: Class[T]) = actors.get(clazz.getName).asInstanceOf[T]
def getComponentInterfaces: List[Class[_]] = actors.values.toArray.toList.map(_.getClass)
def isDefined(clazz: Class[_]): Boolean = actors.containsKey(clazz.getName)
override def start = synchronized {
override def start: Actor = synchronized {
ConfiguratorRepository.registerConfigurator(this)
getLinkedActors.toArray.toList.asInstanceOf[List[Actor]].foreach { actor =>
actor.start
@ -112,7 +113,7 @@ sealed class Supervisor private[akka] (handler: FaultHandlingStrategy)
}
def receive = {
case unknown => throw new IllegalArgumentException("Supervisor does not respond to any messages. Unknown message [" + unknown + "]")
case unknown => throw new IllegalArgumentException("Supervisor " + toString + " does not respond to any messages. Unknown message [" + unknown + "]")
}
def configure(config: SupervisorConfig, factory: SupervisorFactory) = config match {
@ -124,9 +125,11 @@ sealed class Supervisor private[akka] (handler: FaultHandlingStrategy)
actor.lifeCycle = Some(lifeCycle)
startLink(actor)
case SupervisorConfig(_, _) => // recursive configuration
factory.newInstanceFor(server.asInstanceOf[SupervisorConfig]).start
// FIXME what to do with recursively supervisors?
case supervisorConfig @ SupervisorConfig(_, _) => // recursive supervisor configuration
val supervisor = factory.newInstanceFor(supervisorConfig).start
supervisor.lifeCycle = Some(LifeCycle(Permanent))
actors.put(supervisor.getClass.getName, supervisor)
link(supervisor)
})
}
}

View file

@ -22,7 +22,11 @@ object ScalaConfig {
case class SupervisorConfig(restartStrategy: RestartStrategy, worker: List[Server]) extends Server
case class Supervise(actor: Actor, lifeCycle: LifeCycle) extends Server
case class RestartStrategy(scheme: FailOverScheme, maxNrOfRetries: Int, withinTimeRange: Int) extends ConfigElement
case class RestartStrategy(
scheme: FailOverScheme,
maxNrOfRetries: Int,
withinTimeRange: Int,
trapExceptions: List[Class[_ <: Throwable]]) extends ConfigElement
case object AllForOne extends FailOverScheme
case object OneForOne extends FailOverScheme
@ -114,9 +118,10 @@ object JavaConfig {
class RestartStrategy(
@BeanProperty val scheme: FailOverScheme,
@BeanProperty val maxNrOfRetries: Int,
@BeanProperty val withinTimeRange: Int) extends ConfigElement {
@BeanProperty val withinTimeRange: Int,
@BeanProperty val trapExceptions: Array[Class[_ <: Throwable]]) extends ConfigElement {
def transform = se.scalablesolutions.akka.config.ScalaConfig.RestartStrategy(
scheme.transform, maxNrOfRetries, withinTimeRange)
scheme.transform, maxNrOfRetries, withinTimeRange, trapExceptions.toList)
}
class LifeCycle(@BeanProperty val scope: Scope, @BeanProperty val callbacks: RestartCallbacks) extends ConfigElement {

View file

@ -471,7 +471,7 @@ class RemoteSupervisorTest extends JUnitSuite {
val factory = SupervisorFactory(
SupervisorConfig(
RestartStrategy(AllForOne, 3, 100),
RestartStrategy(AllForOne, 3, 100, List(classOf[Exception])),
Supervise(
pingpong1,
LifeCycle(Permanent))
@ -486,7 +486,7 @@ class RemoteSupervisorTest extends JUnitSuite {
val factory = SupervisorFactory(
SupervisorConfig(
RestartStrategy(OneForOne, 3, 100),
RestartStrategy(OneForOne, 3, 100, List(classOf[Exception])),
Supervise(
pingpong1,
LifeCycle(Permanent))
@ -504,7 +504,7 @@ class RemoteSupervisorTest extends JUnitSuite {
val factory = SupervisorFactory(
SupervisorConfig(
RestartStrategy(AllForOne, 3, 100),
RestartStrategy(AllForOne, 3, 100, List(classOf[Exception])),
Supervise(
pingpong1,
LifeCycle(Permanent))
@ -530,7 +530,7 @@ class RemoteSupervisorTest extends JUnitSuite {
val factory = SupervisorFactory(
SupervisorConfig(
RestartStrategy(OneForOne, 3, 100),
RestartStrategy(OneForOne, 3, 100, List(classOf[Exception])),
Supervise(
pingpong1,
LifeCycle(Permanent))
@ -556,13 +556,13 @@ class RemoteSupervisorTest extends JUnitSuite {
val factory = SupervisorFactory(
SupervisorConfig(
RestartStrategy(AllForOne, 3, 100),
RestartStrategy(AllForOne, 3, 100, List(classOf[Exception])),
Supervise(
pingpong1,
LifeCycle(Permanent))
::
SupervisorConfig(
RestartStrategy(AllForOne, 3, 100),
RestartStrategy(AllForOne, 3, 100, List(classOf[Exception])),
Supervise(
pingpong2,
LifeCycle(Permanent))

View file

@ -457,7 +457,7 @@ class SupervisorTest extends JUnitSuite {
val factory = SupervisorFactory(
SupervisorConfig(
RestartStrategy(AllForOne, 3, 100),
RestartStrategy(AllForOne, 3, 100, List(classOf[Exception])),
Supervise(
pingpong1,
LifeCycle(Permanent))
@ -470,7 +470,7 @@ class SupervisorTest extends JUnitSuite {
val factory = SupervisorFactory(
SupervisorConfig(
RestartStrategy(OneForOne, 3, 100),
RestartStrategy(OneForOne, 3, 100, List(classOf[Exception])),
Supervise(
pingpong1,
LifeCycle(Permanent))
@ -485,7 +485,7 @@ class SupervisorTest extends JUnitSuite {
val factory = SupervisorFactory(
SupervisorConfig(
RestartStrategy(AllForOne, 3, 100),
RestartStrategy(AllForOne, 3, 100, List(classOf[Exception])),
Supervise(
pingpong1,
LifeCycle(Permanent))
@ -508,7 +508,7 @@ class SupervisorTest extends JUnitSuite {
val factory = SupervisorFactory(
SupervisorConfig(
RestartStrategy(OneForOne, 3, 100),
RestartStrategy(OneForOne, 3, 100, List(classOf[Exception])),
Supervise(
pingpong1,
LifeCycle(Permanent))
@ -531,13 +531,13 @@ class SupervisorTest extends JUnitSuite {
val factory = SupervisorFactory(
SupervisorConfig(
RestartStrategy(AllForOne, 3, 100),
RestartStrategy(AllForOne, 3, 100, List(classOf[Exception])),
Supervise(
pingpong1,
LifeCycle(Permanent))
::
SupervisorConfig(
RestartStrategy(AllForOne, 3, 100),
RestartStrategy(AllForOne, 3, 100, List(classOf[Exception])),
Supervise(
pingpong2,
LifeCycle(Permanent))

View file

@ -37,7 +37,8 @@ public class ActiveObjectGuiceConfiguratorTest extends TestCase {
bind(Ext.class).to(ExtImpl.class).in(Scopes.SINGLETON);
}
}).configure(
new RestartStrategy(new AllForOne(), 3, 5000), new Component[]{
new RestartStrategy(new AllForOne(), 3, 5000, new Class[]{Exception.class}),
new Component[]{
new Component(
Foo.class,
new LifeCycle(new Permanent()),

View file

@ -19,7 +19,7 @@ public class InMemNestedStateTest extends TestCase {
public InMemNestedStateTest() {
conf.configure(
new RestartStrategy(new AllForOne(), 3, 5000),
new RestartStrategy(new AllForOne(), 3, 5000, new Class[]{Exception.class}),
new Component[]{
// FIXME: remove string-name, add ctor to only accept target class
new Component(InMemStateful.class, new LifeCycle(new Permanent()), 10000000),

View file

@ -23,7 +23,7 @@ public class InMemoryStateTest extends TestCase {
public InMemoryStateTest() {
Config.config();
conf.configure(
new RestartStrategy(new AllForOne(), 3, 5000),
new RestartStrategy(new AllForOne(), 3, 5000, new Class[] {Exception.class}),
new Component[]{
new Component(InMemStateful.class,
new LifeCycle(new Permanent()),

View file

@ -20,7 +20,7 @@ public class PersistentNestedStateTest extends TestCase {
protected void setUp() {
PersistenceManager.init();
conf.configure(
new RestartStrategy(new AllForOne(), 3, 5000),
new RestartStrategy(new AllForOne(), 3, 5000, new Class[] {Exception.class}),
new Component[]{
// FIXME: remove string-name, add ctor to only accept target class
new Component(PersistentStateful.class, new LifeCycle(new Permanent()), 10000000),

View file

@ -17,7 +17,7 @@ public class PersistentStateTest extends TestCase {
protected void setUp() {
PersistenceManager.init();
conf.configure(
new RestartStrategy(new AllForOne(), 3, 5000),
new RestartStrategy(new AllForOne(), 3, 5000, new Class[] {Exception.class}),
new Component[] {
new Component(PersistentStateful.class, new LifeCycle(new Permanent()), 10000000),
new Component(PersistentFailer.class, new LifeCycle(new Permanent()), 1000)

View file

@ -17,7 +17,7 @@ public class RemotePersistentStateTest extends TestCase {
protected void setUp() {
PersistenceManager.init();
conf.configure(
new RestartStrategy(new AllForOne(), 3, 5000),
new RestartStrategy(new AllForOne(), 3, 5000, new Class[]{Exception.class}),
new Component[] {
new Component(PersistentStateful.class, new LifeCycle(new Permanent()), 1000000, new RemoteAddress("localhost", 9999)),
new Component(PersistentFailer.class, new LifeCycle(new Permanent()), 1000000, new RemoteAddress("localhost", 9999))

View file

@ -32,7 +32,7 @@ public class RestTest extends TestCase {
@BeforeClass
protected void setUp() {
conf.configure(
new RestartStrategy(new AllForOne(), 3, 5000),
new RestartStrategy(new AllForOne(), 3, 5000, new Class[]{Exception.class}),
new Component[] {
new Component(
JerseyFoo.class,

View file

@ -8,7 +8,7 @@ public class Boot {
public Boot() throws Exception {
manager.configure(
new RestartStrategy(new OneForOne(), 3, 5000),
new RestartStrategy(new OneForOne(), 3, 5000, new Class[]{Exception.class}),
new Component[] {
new Component(
sample.java.SimpleService.class,

View file

@ -38,7 +38,7 @@ class Boot {
val factory = SupervisorFactory(
SupervisorConfig(
RestartStrategy(OneForOne, 3, 100),
RestartStrategy(OneForOne, 3, 100, List(classOf[Exception])),
Supervise(
new SimpleService,
LifeCycle(Permanent)) ::

View file

@ -20,7 +20,7 @@ import org.atmosphere.cpr.BroadcastFilter
class Boot {
val factory = SupervisorFactory(
SupervisorConfig(
RestartStrategy(OneForOne, 3, 100),
RestartStrategy(OneForOne, 3, 100, List(classOf[Exception])),
Supervise(
new SimpleService,
LifeCycle(Permanent)) ::

View file

@ -13,7 +13,7 @@ import se.scalablesolutions.akka.state.TransactionalState
class Boot {
val factory = SupervisorFactory(
SupervisorConfig(
RestartStrategy(OneForOne, 3, 100),
RestartStrategy(OneForOne, 3, 100, List(classOf[Exception])),
// Dummy implementations of all authentication actors
// see akka.conf to enable one of these for the AkkaSecurityFilterFactory
Supervise(