diff --git a/akka-camel/src/main/scala/Producer.scala b/akka-camel/src/main/scala/Producer.scala index 6609318900..8bc0a82611 100644 --- a/akka-camel/src/main/scala/Producer.scala +++ b/akka-camel/src/main/scala/Producer.scala @@ -124,7 +124,7 @@ trait Producer { this: Actor => * the protected produce methods depending on the return values of * oneway and async. */ - protected def produce: PartialFunction[Any, Unit] = { + protected def produce: Receive = { case msg => { if ( oneway && !async) produceOnewaySync(msg) else if ( oneway && async) produceOnewayAsync(msg) diff --git a/akka-core/src/main/scala/actor/Actor.scala b/akka-core/src/main/scala/actor/Actor.scala index 594c2972db..671919aa60 100644 --- a/akka-core/src/main/scala/actor/Actor.scala +++ b/akka-core/src/main/scala/actor/Actor.scala @@ -30,15 +30,15 @@ import java.util.{HashSet => JHashSet} trait ActorWithNestedReceive extends Actor { import Actor.actor private var nestedReactsProcessors: List[ActorRef] = Nil - private val processNestedReacts: PartialFunction[Any, Unit] = { + private val processNestedReacts: Receive = { case message if !nestedReactsProcessors.isEmpty => val processors = nestedReactsProcessors.reverse processors.head forward message nestedReactsProcessors = processors.tail.reverse } - protected def react: PartialFunction[Any, Unit] - protected def reactAgain(pf: PartialFunction[Any, Unit]) = nestedReactsProcessors ::= actor(pf) + protected def react: Receive + protected def reactAgain(pf: Receive) = nestedReactsProcessors ::= actor(pf) protected def receive = processNestedReacts orElse react } */ @@ -67,7 +67,7 @@ abstract class RemoteActor(hostname: String, port: Int) extends Actor { // Life-cycle messages for the Actors @serializable sealed trait LifeCycleMessage -case class HotSwap(code: Option[PartialFunction[Any, Unit]]) extends LifeCycleMessage +case class HotSwap(code: Option[Actor.Receive]) extends LifeCycleMessage case class Restart(reason: Throwable) extends LifeCycleMessage case class Exit(dead: ActorRef, killer: Throwable) extends LifeCycleMessage case class Link(child: ActorRef) extends LifeCycleMessage @@ -88,6 +88,11 @@ object Actor extends Logging { val TIMEOUT = config.getInt("akka.actor.timeout", 5000) val SERIALIZE_MESSAGES = config.getBool("akka.actor.serialize-messages", false) + /** A Receive is the type that defines actor message behavior + * currently modeled as a PartialFunction[Any,Unit] + */ + type Receive = PartialFunction[Any,Unit] + private[actor] val actorRefInCreation = new scala.util.DynamicVariable[Option[ActorRef]](None) /** @@ -134,10 +139,10 @@ object Actor extends Logging { * } * */ - def actor(body: PartialFunction[Any, Unit]): ActorRef = + def actor(body: Receive): ActorRef = actorOf(new Actor() { self.lifeCycle = Some(LifeCycle(Permanent)) - def receive: PartialFunction[Any, Unit] = body + def receive: Receive = body }).start /** @@ -156,10 +161,10 @@ object Actor extends Logging { * } * */ - def transactor(body: PartialFunction[Any, Unit]): ActorRef = + def transactor(body: Receive): ActorRef = actorOf(new Transactor() { self.lifeCycle = Some(LifeCycle(Permanent)) - def receive: PartialFunction[Any, Unit] = body + def receive: Receive = body }).start /** @@ -176,7 +181,7 @@ object Actor extends Logging { * } * */ - def temporaryActor(body: PartialFunction[Any, Unit]): ActorRef = + def temporaryActor(body: Receive): ActorRef = actorOf(new Actor() { self.lifeCycle = Some(LifeCycle(Temporary)) def receive = body @@ -201,7 +206,7 @@ object Actor extends Logging { */ def init[A](body: => Unit) = { def handler[A](body: => Unit) = new { - def receive(handler: PartialFunction[Any, Unit]) = + def receive(handler: Receive) = actorOf(new Actor() { self.lifeCycle = Some(LifeCycle(Permanent)) body @@ -252,6 +257,8 @@ object Actor extends Logging { * @author Jonas Bonér */ trait Actor extends Logging { + //Type alias because traits cannot have companion objects... + type Receive = Actor.Receive /** * For internal use only, functions as the implicit sender references when invoking @@ -305,7 +312,7 @@ trait Actor extends Logging { * } * */ - protected def receive: PartialFunction[Any, Unit] + protected def receive: Receive /** * User overridable callback/setting. @@ -351,9 +358,9 @@ trait Actor extends Logging { // ==== INTERNAL IMPLEMENTATION DETAILS ==== // ========================================= - private[akka] def base: PartialFunction[Any, Unit] = lifeCycles orElse (self.hotswap getOrElse receive) + private[akka] def base: Receive = lifeCycles orElse (self.hotswap getOrElse receive) - private val lifeCycles: PartialFunction[Any, Unit] = { + private val lifeCycles: Receive = { case HotSwap(code) => self.hotswap = code case Restart(reason) => self.restart(reason) case Exit(dead, reason) => self.handleTrapExit(dead, reason) diff --git a/akka-core/src/main/scala/actor/ActorRef.scala b/akka-core/src/main/scala/actor/ActorRef.scala index 6971250381..ae11834439 100644 --- a/akka-core/src/main/scala/actor/ActorRef.scala +++ b/akka-core/src/main/scala/actor/ActorRef.scala @@ -186,7 +186,7 @@ trait ActorRef extends TransactionManagement { /** * Holds the hot swapped partial function. */ - protected[akka] var hotswap: Option[PartialFunction[Any, Unit]] = None // FIXME: _hotswap should be a stack + protected[akka] var hotswap: Option[Actor.Receive] = None // FIXME: _hotswap should be a stack /** * User overridable callback/setting. diff --git a/akka-core/src/main/scala/routing/Listeners.scala b/akka-core/src/main/scala/routing/Listeners.scala index db544f58f8..47f1f30867 100644 --- a/akka-core/src/main/scala/routing/Listeners.scala +++ b/akka-core/src/main/scala/routing/Listeners.scala @@ -21,7 +21,7 @@ trait Listeners { self : Actor => import se.scalablesolutions.akka.actor.Agent private lazy val listeners = Agent(Set[ActorRef]()) - protected def listenerManagement : PartialFunction[Any,Unit] = { + protected def listenerManagement : Receive = { case Listen(l) => listeners( _ + l) case Deafen(l) => listeners( _ - l ) case WithListeners(f) => listeners foreach f diff --git a/akka-core/src/main/scala/routing/Routers.scala b/akka-core/src/main/scala/routing/Routers.scala index 0636c764fe..a4b6978250 100644 --- a/akka-core/src/main/scala/routing/Routers.scala +++ b/akka-core/src/main/scala/routing/Routers.scala @@ -15,7 +15,7 @@ trait Dispatcher { this: Actor => protected def routes: PartialFunction[Any, ActorRef] - protected def dispatch: PartialFunction[Any, Unit] = { + protected def dispatch: Receive = { case a if routes.isDefinedAt(a) => if (self.replyTo.isDefined) routes(a).forward(transform(a))(Some(self)) else routes(a).!(transform(a))(None) diff --git a/akka-http/src/main/scala/Security.scala b/akka-http/src/main/scala/Security.scala index 2d57c9080d..b5e993fd27 100644 --- a/akka-http/src/main/scala/Security.scala +++ b/akka-http/src/main/scala/Security.scala @@ -187,7 +187,7 @@ trait AuthenticationActor[C <: Credentials] extends Actor { * and a se3curity context is created for the ContainerRequest * this should ensure good integration with current Jersey security */ - protected val authenticate: PartialFunction[Any, Unit] = { + protected val authenticate: Receive = { case Authenticate(req, roles) => { verify(extractCredentials(req)) match { case Some(u: UserInfo) => { @@ -254,7 +254,7 @@ trait DigestAuthenticationActor extends AuthenticationActor[DigestCredentials] w val nonceMap = mkNonceMap //Discards old nonces - protected val invalidateNonces: PartialFunction[Any, Unit] = { + protected val invalidateNonces: Receive = { case InvalidateNonces => val ts = System.currentTimeMillis nonceMap.filter(tuple => (ts - tuple._2) < nonceValidityPeriod) diff --git a/akka-persistence/akka-persistence-mongo/src/test/scala/MongoPersistentActorSpec.scala b/akka-persistence/akka-persistence-mongo/src/test/scala/MongoPersistentActorSpec.scala index 0e784648eb..902527b8cf 100644 --- a/akka-persistence/akka-persistence-mongo/src/test/scala/MongoPersistentActorSpec.scala +++ b/akka-persistence/akka-persistence-mongo/src/test/scala/MongoPersistentActorSpec.scala @@ -35,7 +35,7 @@ class BankAccountActor extends Transactor { private lazy val accountState = MongoStorage.newMap private lazy val txnLog = MongoStorage.newVector - def receive: PartialFunction[Any, Unit] = { + def receive: Receive = { // check balance case Balance(accountNo) => txnLog.add("Balance:" + accountNo) diff --git a/akka-samples/akka-sample-chat/src/main/scala/ChatServer.scala b/akka-samples/akka-sample-chat/src/main/scala/ChatServer.scala index d6caf210c3..69b5d7ec6c 100644 --- a/akka-samples/akka-sample-chat/src/main/scala/ChatServer.scala +++ b/akka-samples/akka-sample-chat/src/main/scala/ChatServer.scala @@ -127,7 +127,7 @@ trait SessionManagement { this: Actor => val storage: ActorRef // needs someone to provide the ChatStorage val sessions = new HashMap[String, ActorRef] - protected def sessionManagement: PartialFunction[Any, Unit] = { + protected def sessionManagement: Receive = { case Login(username) => log.info("User [%s] has logged in", username) val session = actorOf(new Session(username, storage)) @@ -153,7 +153,7 @@ trait SessionManagement { this: Actor => trait ChatManagement { this: Actor => val sessions: HashMap[String, ActorRef] // needs someone to provide the Session map - protected def chatManagement: PartialFunction[Any, Unit] = { + protected def chatManagement: Receive = { case msg @ ChatMessage(from, _) => sessions(from) ! msg case msg @ GetChatLog(from) => sessions(from) forward msg } @@ -181,8 +181,8 @@ trait ChatServer extends Actor { def receive = sessionManagement orElse chatManagement // abstract methods to be defined somewhere else - protected def chatManagement: PartialFunction[Any, Unit] - protected def sessionManagement: PartialFunction[Any, Unit] + protected def chatManagement: Receive + protected def sessionManagement: Receive protected def shutdownSessions: Unit override def shutdown = {