From 32592b46a74e9bee2dcd3133038362148fe56b85 Mon Sep 17 00:00:00 2001 From: Viktor Klang Date: Tue, 19 Oct 2010 14:17:22 +0200 Subject: [PATCH] Refining Supervision API and remove AllForOne, OneForOne and replace with AllForOneStrategy, OneForOneStrategy etc --- .../src/main/scala/actor/Supervisor.scala | 6 +- .../src/main/scala/config/Configurator.scala | 4 +- .../main/scala/config/SupervisionConfig.scala | 112 +++++++++--------- .../akka/config/SupervisionConfig.java | 3 +- .../actor/supervisor/SupervisorMiscSpec.scala | 4 +- .../actor/supervisor/SupervisorSpec.scala | 14 +-- .../src/test/scala/misc/SchedulerSpec.scala | 3 +- .../src/main/scala/remote/Cluster.scala | 3 +- .../scala/remote/RemoteSupervisorSpec.scala | 12 +- .../scala/remote/RemoteTypedActorSpec.scala | 2 +- .../src/main/java/sample/rest/java/Boot.java | 2 +- .../src/main/scala/SimpleService.scala | 2 +- .../src/main/scala/SimpleService.scala | 2 +- .../SupervisionBeanDefinitionParser.scala | 9 +- .../main/scala/SupervisionFactoryBean.scala | 2 +- .../SupervisionBeanDefinitionParserTest.scala | 12 +- .../scala/SupervisionFactoryBeanTest.scala | 6 +- .../src/main/scala/actor/TypedActor.scala | 4 +- .../scala/config/TypedActorConfigurator.scala | 4 +- .../config/TypedActorGuiceConfigurator.scala | 10 +- .../RestartTransactionalTypedActorSpec.scala | 2 +- .../TypedActorGuiceConfiguratorSpec.scala | 30 ++--- .../typed-actor/TypedActorLifecycleSpec.scala | 4 +- 23 files changed, 123 insertions(+), 129 deletions(-) diff --git a/akka-actor/src/main/scala/actor/Supervisor.scala b/akka-actor/src/main/scala/actor/Supervisor.scala index 8655c9de14..74eb2fcda8 100644 --- a/akka-actor/src/main/scala/actor/Supervisor.scala +++ b/akka-actor/src/main/scala/actor/Supervisor.scala @@ -81,11 +81,7 @@ object SupervisorFactory { private[akka] def retrieveFaultHandlerAndTrapExitsFrom(config: SupervisorConfig): FaultHandlingStrategy = config match { - case SupervisorConfig(RestartStrategy(scheme, maxNrOfRetries, timeRange, trapExceptions), _) => - scheme match { - case a:AllForOne => AllForOneStrategy(trapExceptions.toList,maxNrOfRetries, timeRange) - case o:OneForOne => OneForOneStrategy(trapExceptions.toList,maxNrOfRetries, timeRange) - } + case SupervisorConfig(faultHandler, _) => faultHandler } } diff --git a/akka-actor/src/main/scala/config/Configurator.scala b/akka-actor/src/main/scala/config/Configurator.scala index eabb57ab51..8560649018 100644 --- a/akka-actor/src/main/scala/config/Configurator.scala +++ b/akka-actor/src/main/scala/config/Configurator.scala @@ -4,12 +4,12 @@ package se.scalablesolutions.akka.config -import se.scalablesolutions.akka.config.Supervision. {SuperviseTypedActor, RestartStrategy} +import se.scalablesolutions.akka.config.Supervision. {SuperviseTypedActor, FaultHandlingStrategy} private[akka] trait TypedActorConfiguratorBase { def getExternalDependency[T](clazz: Class[T]): T - def configure(restartStrategy: RestartStrategy, components: List[SuperviseTypedActor]): TypedActorConfiguratorBase + def configure(restartStrategy: FaultHandlingStrategy, components: List[SuperviseTypedActor]): TypedActorConfiguratorBase def inject: TypedActorConfiguratorBase diff --git a/akka-actor/src/main/scala/config/SupervisionConfig.scala b/akka-actor/src/main/scala/config/SupervisionConfig.scala index 6585e1b921..27abeba578 100644 --- a/akka-actor/src/main/scala/config/SupervisionConfig.scala +++ b/akka-actor/src/main/scala/config/SupervisionConfig.scala @@ -18,34 +18,71 @@ object Supervision { sealed abstract class ConfigElement abstract class Server extends ConfigElement - abstract class FailOverScheme extends ConfigElement sealed abstract class LifeCycle extends ConfigElement + sealed abstract class FaultHandlingStrategy(val trapExit: List[Class[_ <: Throwable]]) extends ConfigElement - case class SupervisorConfig(restartStrategy: RestartStrategy, worker: List[Server]) extends Server { - //Java API - def this(restartStrategy: RestartStrategy, worker: Array[Server]) = this(restartStrategy,worker.toList) + case class SupervisorConfig(restartStrategy: FaultHandlingStrategy, worker: List[Server]) extends Server { + //Java API + def this(restartStrategy: FaultHandlingStrategy, worker: Array[Server]) = this(restartStrategy,worker.toList) } - class Supervise(val actorRef: ActorRef, val lifeCycle: LifeCycle, _remoteAddress: RemoteAddress) extends Server { - val remoteAddress: Option[RemoteAddress] = Option(_remoteAddress) + class Supervise(val actorRef: ActorRef, val lifeCycle: LifeCycle, val remoteAddress: Option[RemoteAddress]) extends Server { + //Java API + def this(actorRef: ActorRef, lifeCycle: LifeCycle, remoteAddress: RemoteAddress) = + this(actorRef, lifeCycle, Option(remoteAddress)) } object Supervise { def apply(actorRef: ActorRef, lifeCycle: LifeCycle, remoteAddress: RemoteAddress) = new Supervise(actorRef, lifeCycle, remoteAddress) - def apply(actorRef: ActorRef, lifeCycle: LifeCycle) = new Supervise(actorRef, lifeCycle, null) + def apply(actorRef: ActorRef, lifeCycle: LifeCycle) = new Supervise(actorRef, lifeCycle, None) def unapply(supervise: Supervise) = Some((supervise.actorRef, supervise.lifeCycle, supervise.remoteAddress)) } - case class RestartStrategy(scheme: FailOverScheme, maxNrOfRetries: Int, withinTimeRange: Int, trapExceptions: Array[Class[_ <: Throwable]]) extends ConfigElement - - object RestartStrategy { - def apply(scheme: FailOverScheme, maxNrOfRetries: Int, withinTimeRange: Int, trapExceptions: List[Class[_ <: Throwable]]) = - new RestartStrategy(scheme,maxNrOfRetries,withinTimeRange,trapExceptions.toArray) + object AllForOneStrategy { + def apply(trapExit: List[Class[_ <: Throwable]], maxNrOfRetries: Int, withinTimeRange: Int): AllForOneStrategy = + new AllForOneStrategy(trapExit, + if (maxNrOfRetries < 0) None else Some(maxNrOfRetries), if (withinTimeRange < 0) None else Some(withinTimeRange)) } - //Java API - class AllForOne extends FailOverScheme - class OneForOne extends FailOverScheme + case class AllForOneStrategy(override val trapExit: List[Class[_ <: Throwable]], + maxNrOfRetries: Option[Int] = None, + withinTimeRange: Option[Int] = None) extends FaultHandlingStrategy(trapExit) { + def this(trapExit: List[Class[_ <: Throwable]], maxNrOfRetries: Int, withinTimeRange: Int) = + this(trapExit, + if (maxNrOfRetries < 0) None else Some(maxNrOfRetries), if (withinTimeRange < 0) None else Some(withinTimeRange)) + + def this(trapExit: Array[Class[_ <: Throwable]], maxNrOfRetries: Int, withinTimeRange: Int) = + this(trapExit.toList, + if (maxNrOfRetries < 0) None else Some(maxNrOfRetries), if (withinTimeRange < 0) None else Some(withinTimeRange)) + + def this(trapExit: java.util.List[Class[_ <: Throwable]], maxNrOfRetries: Int, withinTimeRange: Int) = + this(trapExit.toArray.toList.asInstanceOf[List[Class[_ <: Throwable]]], + if (maxNrOfRetries < 0) None else Some(maxNrOfRetries), if (withinTimeRange < 0) None else Some(withinTimeRange)) + } + + object OneForOneStrategy { + def apply(trapExit: List[Class[_ <: Throwable]], maxNrOfRetries: Int, withinTimeRange: Int): OneForOneStrategy = + new OneForOneStrategy(trapExit, + if (maxNrOfRetries < 0) None else Some(maxNrOfRetries), if (withinTimeRange < 0) None else Some(withinTimeRange)) + } + + case class OneForOneStrategy(override val trapExit: List[Class[_ <: Throwable]], + maxNrOfRetries: Option[Int] = None, + withinTimeRange: Option[Int] = None) extends FaultHandlingStrategy(trapExit) { + def this(trapExit: List[Class[_ <: Throwable]], maxNrOfRetries: Int, withinTimeRange: Int) = + this(trapExit, + if (maxNrOfRetries < 0) None else Some(maxNrOfRetries), if (withinTimeRange < 0) None else Some(withinTimeRange)) + + def this(trapExit: Array[Class[_ <: Throwable]], maxNrOfRetries: Int, withinTimeRange: Int) = + this(trapExit.toList, + if (maxNrOfRetries < 0) None else Some(maxNrOfRetries), if (withinTimeRange < 0) None else Some(withinTimeRange)) + + def this(trapExit: java.util.List[Class[_ <: Throwable]], maxNrOfRetries: Int, withinTimeRange: Int) = + this(trapExit.toArray.toList.asInstanceOf[List[Class[_ <: Throwable]]], + if (maxNrOfRetries < 0) None else Some(maxNrOfRetries), if (withinTimeRange < 0) None else Some(withinTimeRange)) + } + + case object NoFaultHandlingStrategy extends FaultHandlingStrategy(Nil) //Scala API case object Permanent extends LifeCycle @@ -57,9 +94,8 @@ object Supervision { def temporary() = Temporary def undefinedLifeCycle = UndefinedLifeCycle - //Scala API - object AllForOne extends AllForOne { def apply() = this } - object OneForOne extends OneForOne { def apply() = this } + //Java API + def noFaultHandlingStrategy = NoFaultHandlingStrategy case class SuperviseTypedActor(_intf: Class[_], val target: Class[_], @@ -118,44 +154,4 @@ object Supervision { def this(target: Class[_], lifeCycle: LifeCycle, timeout: Long, transactionRequired: Boolean, dispatcher: MessageDispatcher, remoteAddress: RemoteAddress) = this(null: Class[_], target, lifeCycle, timeout, transactionRequired, dispatcher, remoteAddress) } - - sealed abstract class FaultHandlingStrategy { - def trapExit: List[Class[_ <: Throwable]] - } - - object AllForOneStrategy { - def apply(trapExit: List[Class[_ <: Throwable]], maxNrOfRetries: Int, withinTimeRange: Int) = - new AllForOneStrategy(trapExit, if (maxNrOfRetries < 0) None else Some(maxNrOfRetries), if (withinTimeRange < 0) None else Some(withinTimeRange)) - def apply(trapExit: Array[Class[Throwable]], maxNrOfRetries: Int, withinTimeRange: Int) = - new AllForOneStrategy(trapExit.toList,maxNrOfRetries,withinTimeRange) - } - - case class AllForOneStrategy(trapExit: List[Class[_ <: Throwable]], - maxNrOfRetries: Option[Int] = None, - withinTimeRange: Option[Int] = None) extends FaultHandlingStrategy { - def this(trapExit: List[Class[_ <: Throwable]],maxNrOfRetries: Int, withinTimeRange: Int) = - this(trapExit, if (maxNrOfRetries < 0) None else Some(maxNrOfRetries), if (withinTimeRange < 0) None else Some(withinTimeRange)) - def this(trapExit: Array[Class[Throwable]],maxNrOfRetries: Int, withinTimeRange: Int) = - this(trapExit.toList,maxNrOfRetries,withinTimeRange) - } - - object OneForOneStrategy { - def apply(trapExit: List[Class[_ <: Throwable]], maxNrOfRetries: Int, withinTimeRange: Int) = - new OneForOneStrategy(trapExit, if (maxNrOfRetries < 0) None else Some(maxNrOfRetries), if (withinTimeRange < 0) None else Some(withinTimeRange)) - def apply(trapExit: Array[Class[Throwable]], maxNrOfRetries: Int, withinTimeRange: Int) = - new OneForOneStrategy(trapExit.toList,maxNrOfRetries,withinTimeRange) - } - - case class OneForOneStrategy(trapExit: List[Class[_ <: Throwable]], - maxNrOfRetries: Option[Int] = None, - withinTimeRange: Option[Int] = None) extends FaultHandlingStrategy { - def this(trapExit: List[Class[_ <: Throwable]],maxNrOfRetries: Int, withinTimeRange: Int) = - this(trapExit, if (maxNrOfRetries < 0) None else Some(maxNrOfRetries), if (withinTimeRange < 0) None else Some(withinTimeRange)) - def this(trapExit: Array[Class[Throwable]],maxNrOfRetries: Int, withinTimeRange: Int) = - this(trapExit.toList,maxNrOfRetries,withinTimeRange) - } - - case object NoFaultHandlingStrategy extends FaultHandlingStrategy { - def trapExit: List[Class[_ <: Throwable]] = Nil - } } \ No newline at end of file diff --git a/akka-actor/src/test/java/se/scalablesolutions/akka/config/SupervisionConfig.java b/akka-actor/src/test/java/se/scalablesolutions/akka/config/SupervisionConfig.java index d8de22eb59..dcd2001d27 100644 --- a/akka-actor/src/test/java/se/scalablesolutions/akka/config/SupervisionConfig.java +++ b/akka-actor/src/test/java/se/scalablesolutions/akka/config/SupervisionConfig.java @@ -3,6 +3,7 @@ package se.scalablesolutions.akka.config; import se.scalablesolutions.akka.actor.ActorRef; import java.util.ArrayList; +import java.util.Arrays; import java.util.List; import static se.scalablesolutions.akka.config.Supervision.*; @@ -15,6 +16,6 @@ public class SupervisionConfig { targets.add(new Supervise(ref, permanent(), new RemoteAddress("localhost",9999))); } - return new SupervisorConfig(new RestartStrategy(new OneForOne(),50,1000,new Class[]{ Exception.class}), targets.toArray(new Server[0])); + return new SupervisorConfig(new OneForOneStrategy(new Class[] { Exception.class },50,1000), targets.toArray(new Server[0])); } } diff --git a/akka-actor/src/test/scala/actor/supervisor/SupervisorMiscSpec.scala b/akka-actor/src/test/scala/actor/supervisor/SupervisorMiscSpec.scala index 261df68eab..5fcaf13173 100644 --- a/akka-actor/src/test/scala/actor/supervisor/SupervisorMiscSpec.scala +++ b/akka-actor/src/test/scala/actor/supervisor/SupervisorMiscSpec.scala @@ -6,7 +6,7 @@ package se.scalablesolutions.akka.actor import org.scalatest.WordSpec import org.scalatest.matchers.MustMatchers import se.scalablesolutions.akka.dispatch.Dispatchers -import se.scalablesolutions.akka.config.Supervision.{RestartStrategy, SupervisorConfig, OneForOne, Supervise, Permanent} +import se.scalablesolutions.akka.config.Supervision.{SupervisorConfig, OneForOneStrategy, Supervise, Permanent} import java.util.concurrent.CountDownLatch class SupervisorMiscSpec extends WordSpec with MustMatchers { @@ -57,7 +57,7 @@ class SupervisorMiscSpec extends WordSpec with MustMatchers { val sup = Supervisor( SupervisorConfig( - RestartStrategy(new OneForOne, 3, 5000, List(classOf[Exception])), + OneForOneStrategy(List(classOf[Exception]),3, 5000), Supervise(actor1, Permanent) :: Supervise(actor2, Permanent) :: Supervise(actor3, Permanent) :: diff --git a/akka-actor/src/test/scala/actor/supervisor/SupervisorSpec.scala b/akka-actor/src/test/scala/actor/supervisor/SupervisorSpec.scala index 3a1758d7c6..5f73c4ce8e 100644 --- a/akka-actor/src/test/scala/actor/supervisor/SupervisorSpec.scala +++ b/akka-actor/src/test/scala/actor/supervisor/SupervisorSpec.scala @@ -501,7 +501,7 @@ class SupervisorSpec extends JUnitSuite { Supervisor( SupervisorConfig( - RestartStrategy(AllForOne, 3, 5000, List(classOf[Exception])), + AllForOneStrategy(List(classOf[Exception]), 3, 5000), Supervise( temporaryActor, Temporary) @@ -513,7 +513,7 @@ class SupervisorSpec extends JUnitSuite { Supervisor( SupervisorConfig( - RestartStrategy(AllForOne, 3, 5000, List(classOf[Exception])), + AllForOneStrategy(List(classOf[Exception]), 3, 5000), Supervise( pingpong1, Permanent) @@ -525,7 +525,7 @@ class SupervisorSpec extends JUnitSuite { Supervisor( SupervisorConfig( - RestartStrategy(OneForOne, 3, 5000, List(classOf[Exception])), + OneForOneStrategy(List(classOf[Exception]), 3, 5000), Supervise( pingpong1, Permanent) @@ -539,7 +539,7 @@ class SupervisorSpec extends JUnitSuite { Supervisor( SupervisorConfig( - RestartStrategy(AllForOne, 3, 5000, List(classOf[Exception])), + AllForOneStrategy(List(classOf[Exception]), 3, 5000), Supervise( pingpong1, Permanent) @@ -561,7 +561,7 @@ class SupervisorSpec extends JUnitSuite { Supervisor( SupervisorConfig( - RestartStrategy(OneForOne, 3, 5000, List(classOf[Exception])), + OneForOneStrategy(List(classOf[Exception]), 3, 5000), Supervise( pingpong1, Permanent) @@ -583,13 +583,13 @@ class SupervisorSpec extends JUnitSuite { Supervisor( SupervisorConfig( - RestartStrategy(AllForOne, 3, 5000, List(classOf[Exception])), + AllForOneStrategy(List(classOf[Exception]), 3, 5000), Supervise( pingpong1, Permanent) :: SupervisorConfig( - RestartStrategy(AllForOne, 3, 5000, Nil), + AllForOneStrategy(Nil, 3, 5000), Supervise( pingpong2, Permanent) diff --git a/akka-actor/src/test/scala/misc/SchedulerSpec.scala b/akka-actor/src/test/scala/misc/SchedulerSpec.scala index c1f1071161..2278536c1d 100644 --- a/akka-actor/src/test/scala/misc/SchedulerSpec.scala +++ b/akka-actor/src/test/scala/misc/SchedulerSpec.scala @@ -110,8 +110,7 @@ class SchedulerSpec extends JUnitSuite { Supervisor( SupervisorConfig( - RestartStrategy(AllForOne, 3, 1000, - List(classOf[Exception])), + AllForOneStrategy(List(classOf[Exception]), 3, 1000), Supervise( actor, Permanent) diff --git a/akka-remote/src/main/scala/remote/Cluster.scala b/akka-remote/src/main/scala/remote/Cluster.scala index df1a79009e..f7ef779289 100644 --- a/akka-remote/src/main/scala/remote/Cluster.scala +++ b/akka-remote/src/main/scala/remote/Cluster.scala @@ -241,8 +241,7 @@ object Cluster extends Cluster with Logging { private[akka] def createSupervisor(actor: ActorRef): Option[Supervisor] = Some(Supervisor( - SupervisorConfig( - RestartStrategy(OneForOne, 5, 1000, List(classOf[Exception])), + SupervisorConfig(OneForOneStrategy(List(classOf[Exception]), 5, 1000), Supervise(actor, Permanent) :: Nil))) private[this] def clusterActor = if (clusterActorRef.isEmpty) None else Some(clusterActorRef.get.actor.asInstanceOf[ClusterActor]) diff --git a/akka-remote/src/test/scala/remote/RemoteSupervisorSpec.scala b/akka-remote/src/test/scala/remote/RemoteSupervisorSpec.scala index 735aba23cb..9834b035af 100644 --- a/akka-remote/src/test/scala/remote/RemoteSupervisorSpec.scala +++ b/akka-remote/src/test/scala/remote/RemoteSupervisorSpec.scala @@ -480,7 +480,7 @@ class RemoteSupervisorSpec extends JUnitSuite { val factory = SupervisorFactory( SupervisorConfig( - RestartStrategy(AllForOne, 3, 100, List(classOf[Exception])), + AllForOneStrategy(List(classOf[Exception]), 3, 100), Supervise( pingpong1, Permanent) @@ -496,7 +496,7 @@ class RemoteSupervisorSpec extends JUnitSuite { val factory = SupervisorFactory( SupervisorConfig( - RestartStrategy(OneForOne, 3, 100, List(classOf[Exception])), + OneForOneStrategy(List(classOf[Exception]), 3, 100), Supervise( pingpong1, Permanent) @@ -517,7 +517,7 @@ class RemoteSupervisorSpec extends JUnitSuite { val factory = SupervisorFactory( SupervisorConfig( - RestartStrategy(AllForOne, 3, 100, List(classOf[Exception])), + AllForOneStrategy(List(classOf[Exception]), 3, 100), Supervise( pingpong1, Permanent) @@ -548,7 +548,7 @@ class RemoteSupervisorSpec extends JUnitSuite { val factory = SupervisorFactory( SupervisorConfig( - RestartStrategy(OneForOne, 3, 100, List(classOf[Exception])), + OneForOneStrategy(List(classOf[Exception]), 3, 100), Supervise( pingpong1, Permanent) @@ -577,13 +577,13 @@ class RemoteSupervisorSpec extends JUnitSuite { val factory = SupervisorFactory( SupervisorConfig( - RestartStrategy(AllForOne, 3, 100, List(classOf[Exception])), + AllForOneStrategy(List(classOf[Exception]), 3, 100), Supervise( pingpong1, Permanent) :: SupervisorConfig( - RestartStrategy(AllForOne, 3, 100, List(classOf[Exception])), + AllForOneStrategy(List(classOf[Exception]), 3, 100), Supervise( pingpong2, Permanent) diff --git a/akka-remote/src/test/scala/remote/RemoteTypedActorSpec.scala b/akka-remote/src/test/scala/remote/RemoteTypedActorSpec.scala index 58c977654d..cd8f09a615 100644 --- a/akka-remote/src/test/scala/remote/RemoteTypedActorSpec.scala +++ b/akka-remote/src/test/scala/remote/RemoteTypedActorSpec.scala @@ -48,7 +48,7 @@ class RemoteTypedActorSpec extends server.start("localhost", 9995) Config.config conf.configure( - new RestartStrategy(AllForOne(), 3, 5000, List(classOf[Exception]).toArray), + new AllForOneStrategy(List(classOf[Exception]), 3, 5000), List( new SuperviseTypedActor( classOf[RemoteTypedActorOne], diff --git a/akka-samples/akka-sample-rest-java/src/main/java/sample/rest/java/Boot.java b/akka-samples/akka-sample-rest-java/src/main/java/sample/rest/java/Boot.java index 4775e0e7a8..d9b33cf559 100644 --- a/akka-samples/akka-sample-rest-java/src/main/java/sample/rest/java/Boot.java +++ b/akka-samples/akka-sample-rest-java/src/main/java/sample/rest/java/Boot.java @@ -11,7 +11,7 @@ public class Boot { public final static TypedActorConfigurator configurator = new TypedActorConfigurator(); static { configurator.configure( - new RestartStrategy(new OneForOne(), 3, 5000, new Class[]{Exception.class}), + new OneForOneStrategy(new Class[]{Exception.class}, 3, 5000), new SuperviseTypedActor[] { new SuperviseTypedActor( SimpleService.class, diff --git a/akka-samples/akka-sample-rest-scala/src/main/scala/SimpleService.scala b/akka-samples/akka-sample-rest-scala/src/main/scala/SimpleService.scala index 56f977ab89..a316c54fcc 100644 --- a/akka-samples/akka-sample-rest-scala/src/main/scala/SimpleService.scala +++ b/akka-samples/akka-sample-rest-scala/src/main/scala/SimpleService.scala @@ -25,7 +25,7 @@ import org.atmosphere.jersey.Broadcastable class Boot { val factory = SupervisorFactory( SupervisorConfig( - RestartStrategy(OneForOne, 3, 100,List(classOf[Exception])), + OneForOneStrategy(List(classOf[Exception]), 3, 100), Supervise( actorOf[SimpleServiceActor], Permanent) :: diff --git a/akka-samples/akka-sample-security/src/main/scala/SimpleService.scala b/akka-samples/akka-sample-security/src/main/scala/SimpleService.scala index a382b0a40e..0f2dd0e59a 100644 --- a/akka-samples/akka-sample-security/src/main/scala/SimpleService.scala +++ b/akka-samples/akka-sample-security/src/main/scala/SimpleService.scala @@ -15,7 +15,7 @@ import se.scalablesolutions.akka.actor.ActorRegistry.actorFor class Boot { val factory = SupervisorFactory( SupervisorConfig( - RestartStrategy(OneForOne, 3, 100, List(classOf[Exception])), + OneForOneStrategy(List(classOf[Exception]), 3, 100), // Dummy implementations of all authentication actors // see akka.conf to enable one of these for the AkkaSecurityFilterFactory Supervise( diff --git a/akka-spring/src/main/scala/SupervisionBeanDefinitionParser.scala b/akka-spring/src/main/scala/SupervisionBeanDefinitionParser.scala index cb797ba29a..8eb2d6ef37 100644 --- a/akka-spring/src/main/scala/SupervisionBeanDefinitionParser.scala +++ b/akka-spring/src/main/scala/SupervisionBeanDefinitionParser.scala @@ -47,12 +47,17 @@ class SupervisionBeanDefinitionParser extends AbstractSingleBeanDefinitionParser } private[akka] def parseRestartStrategy(element: Element, builder: BeanDefinitionBuilder) { - val failover = if (mandatory(element, FAILOVER) == "AllForOne") AllForOne else OneForOne + val failover = mandatory(element, FAILOVER) val timeRange = mandatory(element, TIME_RANGE).toInt val retries = mandatory(element, RETRIES).toInt val trapExitsElement = mandatoryElement(element, TRAP_EXISTS_TAG) val trapExceptions = parseTrapExits(trapExitsElement) - val restartStrategy = new RestartStrategy(failover, retries, timeRange, trapExceptions) + + val restartStrategy = failover match { + case "AllForOne" => new AllForOneStrategy(trapExceptions, retries, timeRange) + case "OneForOne" => new OneForOneStrategy(trapExceptions, retries, timeRange) + case _ => new OneForOneStrategy(trapExceptions, retries, timeRange) //Default to OneForOne + } builder.addPropertyValue("restartStrategy", restartStrategy) } diff --git a/akka-spring/src/main/scala/SupervisionFactoryBean.scala b/akka-spring/src/main/scala/SupervisionFactoryBean.scala index 86709f06f0..b4559304b5 100644 --- a/akka-spring/src/main/scala/SupervisionFactoryBean.scala +++ b/akka-spring/src/main/scala/SupervisionFactoryBean.scala @@ -15,7 +15,7 @@ import se.scalablesolutions.akka.config.{TypedActorConfigurator, RemoteAddress} * @author michaelkober */ class SupervisionFactoryBean extends AbstractFactoryBean[AnyRef] { - @BeanProperty var restartStrategy: RestartStrategy = _ + @BeanProperty var restartStrategy: FaultHandlingStrategy = _ @BeanProperty var supervised: List[ActorProperties] = _ @BeanProperty var typed: String = "" diff --git a/akka-spring/src/test/scala/SupervisionBeanDefinitionParserTest.scala b/akka-spring/src/test/scala/SupervisionBeanDefinitionParserTest.scala index 45effd28e5..85c28b0c0e 100644 --- a/akka-spring/src/test/scala/SupervisionBeanDefinitionParserTest.scala +++ b/akka-spring/src/test/scala/SupervisionBeanDefinitionParserTest.scala @@ -11,7 +11,7 @@ import ScalaDom._ import org.w3c.dom.Element import org.springframework.beans.factory.support.BeanDefinitionBuilder -import se.scalablesolutions.akka.config.Supervision. {RestartStrategy, AllForOne} +import se.scalablesolutions.akka.config.Supervision. {FaultHandlingStrategy, AllForOneStrategy} /** * Test for SupervisionBeanDefinitionParser @@ -35,13 +35,11 @@ class SupervisionBeanDefinitionParserTest extends Spec with ShouldMatchers { it("should parse the supervisor restart strategy") { parser.parseSupervisor(createSupervisorElement, builder); - val strategy = builder.getBeanDefinition.getPropertyValues.getPropertyValue("restartStrategy").getValue.asInstanceOf[RestartStrategy] + val strategy = builder.getBeanDefinition.getPropertyValues.getPropertyValue("restartStrategy").getValue.asInstanceOf[FaultHandlingStrategy] assert(strategy ne null) - assert(strategy.scheme match { - case AllForOne => true - case _ => false }) - expect(3) { strategy.maxNrOfRetries } - expect(1000) { strategy.withinTimeRange } + assert(strategy.isInstanceOf[AllForOneStrategy]) + expect(3) { strategy.asInstanceOf[AllForOneStrategy].maxNrOfRetries.get } + expect(1000) { strategy.asInstanceOf[AllForOneStrategy].withinTimeRange.get } } it("should parse the supervised typed actors") { diff --git a/akka-spring/src/test/scala/SupervisionFactoryBeanTest.scala b/akka-spring/src/test/scala/SupervisionFactoryBeanTest.scala index d85cfff942..bb2fbb0b27 100644 --- a/akka-spring/src/test/scala/SupervisionFactoryBeanTest.scala +++ b/akka-spring/src/test/scala/SupervisionFactoryBeanTest.scala @@ -15,7 +15,7 @@ private[akka] class Foo @RunWith(classOf[JUnitRunner]) class SupervisionFactoryBeanTest extends Spec with ShouldMatchers { - val restartStrategy = new RestartStrategy(AllForOne(), 3, 1000, Array(classOf[Throwable])) + val faultHandlingStrategy = new AllForOneStrategy(List(classOf[Exception]), 3, 1000) val typedActors = List(createTypedActorProperties("se.scalablesolutions.akka.spring.Foo", "1000")) private def createTypedActorProperties(target: String, timeout: String) : ActorProperties = { @@ -28,8 +28,8 @@ class SupervisionFactoryBeanTest extends Spec with ShouldMatchers { describe("A SupervisionFactoryBean") { val bean = new SupervisionFactoryBean it("should have java getters and setters for all properties") { - bean.setRestartStrategy(restartStrategy) - assert(bean.getRestartStrategy == restartStrategy) + bean.setRestartStrategy(faultHandlingStrategy) + assert(bean.getRestartStrategy == faultHandlingStrategy) bean.setSupervised(typedActors) assert(bean.getSupervised == typedActors) } diff --git a/akka-typed-actor/src/main/scala/actor/TypedActor.scala b/akka-typed-actor/src/main/scala/actor/TypedActor.scala index a092f34070..0aeb127fdc 100644 --- a/akka-typed-actor/src/main/scala/actor/TypedActor.scala +++ b/akka-typed-actor/src/main/scala/actor/TypedActor.scala @@ -731,8 +731,8 @@ object TypedActor extends Logging { private[akka] def returnsFuture_?(methodRtti: MethodRtti): Boolean = classOf[Future[_]].isAssignableFrom(methodRtti.getMethod.getReturnType) - private[akka] def supervise(restartStrategy: RestartStrategy, components: List[Supervise]): Supervisor = - Supervisor(SupervisorConfig(restartStrategy, components)) + private[akka] def supervise(faultHandlingStrategy: FaultHandlingStrategy, components: List[Supervise]): Supervisor = + Supervisor(SupervisorConfig(faultHandlingStrategy, components)) def isJoinPointAndOneWay(message: Any): Boolean = if (isJoinPoint(message)) isOneWay(message.asInstanceOf[JoinPoint].getRtti.asInstanceOf[MethodRtti]) diff --git a/akka-typed-actor/src/main/scala/config/TypedActorConfigurator.scala b/akka-typed-actor/src/main/scala/config/TypedActorConfigurator.scala index 995640f979..f23cb6b8ec 100644 --- a/akka-typed-actor/src/main/scala/config/TypedActorConfigurator.scala +++ b/akka-typed-actor/src/main/scala/config/TypedActorConfigurator.scala @@ -43,9 +43,9 @@ class TypedActorConfigurator { */ def getInstance[T](clazz: Class[T]): T = INSTANCE.getInstance(clazz).head - def configure(restartStrategy: RestartStrategy, components: Array[SuperviseTypedActor]): TypedActorConfigurator = { + def configure(faultHandlingStrategy: FaultHandlingStrategy, components: Array[SuperviseTypedActor]): TypedActorConfigurator = { INSTANCE.configure( - restartStrategy, + faultHandlingStrategy, components.toList.asInstanceOf[scala.List[SuperviseTypedActor]]) this } diff --git a/akka-typed-actor/src/main/scala/config/TypedActorGuiceConfigurator.scala b/akka-typed-actor/src/main/scala/config/TypedActorGuiceConfigurator.scala index 4cd71ab060..38378f5e8b 100644 --- a/akka-typed-actor/src/main/scala/config/TypedActorGuiceConfigurator.scala +++ b/akka-typed-actor/src/main/scala/config/TypedActorGuiceConfigurator.scala @@ -27,7 +27,7 @@ import com.google.inject._ private[akka] class TypedActorGuiceConfigurator extends TypedActorConfiguratorBase with Logging { private var injector: Injector = _ private var supervisor: Option[Supervisor] = None - private var restartStrategy: RestartStrategy = _ + private var faultHandlingStrategy: FaultHandlingStrategy = NoFaultHandlingStrategy private var components: List[SuperviseTypedActor] = _ private var supervised: List[Supervise] = Nil private var bindings: List[DependencyBinding] = Nil @@ -68,9 +68,9 @@ private[akka] class TypedActorGuiceConfigurator extends TypedActorConfiguratorBa else c.target } - override def configure(restartStrategy: RestartStrategy, components: List[SuperviseTypedActor]): + override def configure(faultHandlingStrategy: FaultHandlingStrategy, components: List[SuperviseTypedActor]): TypedActorConfiguratorBase = synchronized { - this.restartStrategy = restartStrategy + this.faultHandlingStrategy = faultHandlingStrategy this.components = components.toArray.toList.asInstanceOf[List[SuperviseTypedActor]] bindings = for (component <- this.components) yield { newDelegatingProxy(component) @@ -144,7 +144,7 @@ private[akka] class TypedActorGuiceConfigurator extends TypedActorConfiguratorBa override def supervise: TypedActorConfiguratorBase = synchronized { if (injector eq null) inject - supervisor = Some(TypedActor.supervise(restartStrategy, supervised)) + supervisor = Some(TypedActor.supervise(faultHandlingStrategy, supervised)) this } @@ -173,7 +173,7 @@ private[akka] class TypedActorGuiceConfigurator extends TypedActorConfiguratorBa typedActorRegistry = new HashMap[Class[_], Tuple3[AnyRef, AnyRef, SuperviseTypedActor]] methodToUriRegistry = new HashMap[Method, String] injector = null - restartStrategy = null + faultHandlingStrategy = NoFaultHandlingStrategy } def stop = synchronized { diff --git a/akka-typed-actor/src/test/scala/actor/typed-actor/RestartTransactionalTypedActorSpec.scala b/akka-typed-actor/src/test/scala/actor/typed-actor/RestartTransactionalTypedActorSpec.scala index 3dc1702593..968379a003 100644 --- a/akka-typed-actor/src/test/scala/actor/typed-actor/RestartTransactionalTypedActorSpec.scala +++ b/akka-typed-actor/src/test/scala/actor/typed-actor/RestartTransactionalTypedActorSpec.scala @@ -27,7 +27,7 @@ class RestartTransactionalTypedActorSpec extends def before { Config.config conf.configure( - new RestartStrategy(AllForOne, 3, 5000, Array(classOf[Exception])), + AllForOneStrategy(List(classOf[Exception]), 3, 5000), List( new SuperviseTypedActor( classOf[TransactionalTypedActor], diff --git a/akka-typed-actor/src/test/scala/actor/typed-actor/TypedActorGuiceConfiguratorSpec.scala b/akka-typed-actor/src/test/scala/actor/typed-actor/TypedActorGuiceConfiguratorSpec.scala index f524f96777..ea6e939386 100644 --- a/akka-typed-actor/src/test/scala/actor/typed-actor/TypedActorGuiceConfiguratorSpec.scala +++ b/akka-typed-actor/src/test/scala/actor/typed-actor/TypedActorGuiceConfiguratorSpec.scala @@ -35,21 +35,21 @@ class TypedActorGuiceConfiguratorSpec extends conf.addExternalGuiceModule(new AbstractModule { def configure = bind(classOf[Ext]).to(classOf[ExtImpl]).in(Scopes.SINGLETON) }).configure( - new RestartStrategy(AllForOne(), 3, 5000, Array(classOf[Exception])), - List( - new SuperviseTypedActor( - classOf[Foo], - classOf[FooImpl], - Permanent, - 1000, - dispatcher), - new SuperviseTypedActor( - classOf[Bar], - classOf[BarImpl], - Permanent, - 1000, - dispatcher) - ).toArray).inject.supervise + AllForOneStrategy(classOf[Exception] :: Nil, 3, 5000), + List( + new SuperviseTypedActor( + classOf[Foo], + classOf[FooImpl], + Permanent, + 1000, + dispatcher), + new SuperviseTypedActor( + classOf[Bar], + classOf[BarImpl], + Permanent, + 1000, + dispatcher) + ).toArray).inject.supervise } diff --git a/akka-typed-actor/src/test/scala/actor/typed-actor/TypedActorLifecycleSpec.scala b/akka-typed-actor/src/test/scala/actor/typed-actor/TypedActorLifecycleSpec.scala index 715ddb8ef8..7bc521fe73 100644 --- a/akka-typed-actor/src/test/scala/actor/typed-actor/TypedActorLifecycleSpec.scala +++ b/akka-typed-actor/src/test/scala/actor/typed-actor/TypedActorLifecycleSpec.scala @@ -21,7 +21,7 @@ class TypedActorLifecycleSpec extends Spec with ShouldMatchers with BeforeAndAft var conf2: TypedActorConfigurator = _ override protected def beforeAll() = { - val strategy = new RestartStrategy(AllForOne(), 3, 1000, Array(classOf[Exception])) + val strategy = AllForOneStrategy(classOf[Exception] :: Nil, 3, 1000) val comp3 = new SuperviseTypedActor(classOf[SamplePojo], classOf[SamplePojoImpl], permanent(), 1000) val comp4 = new SuperviseTypedActor(classOf[SamplePojo], classOf[SamplePojoImpl], temporary(), 1000) conf1 = new TypedActorConfigurator().configure(strategy, Array(comp3)).supervise @@ -87,7 +87,7 @@ class TypedActorLifecycleSpec extends Spec with ShouldMatchers with BeforeAndAft SamplePojoImpl.reset val pojo = TypedActor.newInstance(classOf[SimpleJavaPojo], classOf[SimpleJavaPojoImpl]) val supervisor = TypedActor.newInstance(classOf[SimpleJavaPojo], classOf[SimpleJavaPojoImpl]) - link(supervisor, pojo, OneForOneStrategy(Array(classOf[Throwable]), 3, 2000)) + link(supervisor, pojo, OneForOneStrategy(classOf[Throwable] :: Nil, 3, 2000)) pojo.throwException Thread.sleep(500) SimpleJavaPojoImpl._pre should be(true)