From 20881c777d1d3fa57aa3fb57e4301055ffe7ed53 Mon Sep 17 00:00:00 2001 From: Viktor Klang Date: Wed, 29 Aug 2012 18:00:14 +0200 Subject: [PATCH 1/2] #2396 - Removing the Props.apply(ActorContext => Actor.Receive) method as it is superceded by the ActorDSL --- .../scala/akka/actor/DeathWatchSpec.scala | 4 +-- .../actor/LocalActorRefProviderSpec.scala | 4 +-- .../actor/dispatch/DispatcherActorSpec.scala | 25 ++++++++++++------- .../akka/actor/dispatch/PinnedActorSpec.scala | 2 +- .../scala/akka/dataflow/Future2Actor.scala | 4 +-- .../src/test/scala/akka/pattern/AskSpec.scala | 8 +++--- .../src/main/scala/akka/actor/ActorDSL.scala | 16 ++++++------ .../src/main/scala/akka/actor/Props.scala | 5 ---- .../component/ActorEndpointPathTest.scala | 4 +-- .../test/scala/akka/testkit/AkkaSpec.scala | 12 ++++----- .../scala/akka/testkit/TestActorRefSpec.scala | 4 +-- 11 files changed, 43 insertions(+), 45 deletions(-) diff --git a/akka-actor-tests/src/test/scala/akka/actor/DeathWatchSpec.scala b/akka-actor-tests/src/test/scala/akka/actor/DeathWatchSpec.scala index 3c38d332a4..7dc121604f 100644 --- a/akka-actor-tests/src/test/scala/akka/actor/DeathWatchSpec.scala +++ b/akka-actor-tests/src/test/scala/akka/actor/DeathWatchSpec.scala @@ -99,7 +99,7 @@ trait DeathWatchSpec { this: AkkaSpec with ImplicitSender with DefaultTimeout filterException[ActorKilledException] { val supervisor = system.actorOf(Props(new Supervisor( OneForOneStrategy(maxNrOfRetries = 2)(List(classOf[Exception]))))) - val terminalProps = Props(context ⇒ { case x ⇒ context.sender ! x }) + val terminalProps = Props(new Actor { def receive = { case x ⇒ sender ! x } }) val terminal = Await.result((supervisor ? terminalProps).mapTo[ActorRef], timeout.duration) val monitor = startWatching(terminal) @@ -150,7 +150,7 @@ trait DeathWatchSpec { this: AkkaSpec with ImplicitSender with DefaultTimeout val parent = system.actorOf(Props(new Actor { def receive = { case "NKOTB" ⇒ - val currentKid = context.watch(context.actorOf(Props(ctx ⇒ { case "NKOTB" ⇒ ctx stop ctx.self }), "kid")) + val currentKid = context.watch(context.actorOf(Props(new Actor { def receive = { case "NKOTB" ⇒ context stop self } }), "kid")) currentKid forward "NKOTB" context become { case Terminated(`currentKid`) ⇒ diff --git a/akka-actor-tests/src/test/scala/akka/actor/LocalActorRefProviderSpec.scala b/akka-actor-tests/src/test/scala/akka/actor/LocalActorRefProviderSpec.scala index 084d2ddf8b..38896f7967 100644 --- a/akka-actor-tests/src/test/scala/akka/actor/LocalActorRefProviderSpec.scala +++ b/akka-actor-tests/src/test/scala/akka/actor/LocalActorRefProviderSpec.scala @@ -34,7 +34,7 @@ class LocalActorRefProviderSpec extends AkkaSpec(LocalActorRefProviderSpec.confi "An LocalActorRefProvider" must { "find actor refs using actorFor" in { - val a = system.actorOf(Props(ctx ⇒ { case _ ⇒ })) + val a = system.actorOf(Props(new Actor { def receive = { case _ ⇒ } })) val b = system.actorFor(a.path) a must be === b } @@ -52,7 +52,7 @@ class LocalActorRefProviderSpec extends AkkaSpec(LocalActorRefProviderSpec.confi for (i ← 0 until 100) { val address = "new-actor" + i implicit val timeout = Timeout(5 seconds) - val actors = for (j ← 1 to 4) yield Future(system.actorOf(Props(c ⇒ { case _ ⇒ }), address)) + val actors = for (j ← 1 to 4) yield Future(system.actorOf(Props(new Actor { def receive = { case _ ⇒ } }), address)) val set = Set() ++ actors.map(a ⇒ Await.ready(a, timeout.duration).value match { case Some(Success(a: ActorRef)) ⇒ 1 case Some(Failure(ex: InvalidActorNameException)) ⇒ 2 diff --git a/akka-actor-tests/src/test/scala/akka/actor/dispatch/DispatcherActorSpec.scala b/akka-actor-tests/src/test/scala/akka/actor/dispatch/DispatcherActorSpec.scala index c0b14896f8..a6b071d804 100644 --- a/akka-actor-tests/src/test/scala/akka/actor/dispatch/DispatcherActorSpec.scala +++ b/akka-actor-tests/src/test/scala/akka/actor/dispatch/DispatcherActorSpec.scala @@ -81,12 +81,15 @@ class DispatcherActorSpec extends AkkaSpec(DispatcherActorSpec.config) with Defa val latch = new CountDownLatch(100) val start = new CountDownLatch(1) val fastOne = system.actorOf( - Props(context ⇒ { case "sabotage" ⇒ works.set(false) }).withDispatcher(throughputDispatcher)) + Props(new Actor { def receive = { case "sabotage" ⇒ works.set(false) } }) + .withDispatcher(throughputDispatcher)) val slowOne = system.actorOf( - Props(context ⇒ { - case "hogexecutor" ⇒ context.sender ! "OK"; start.await - case "ping" ⇒ if (works.get) latch.countDown() + Props(new Actor { + def receive = { + case "hogexecutor" ⇒ sender ! "OK"; start.await + case "ping" ⇒ if (works.get) latch.countDown() + } }).withDispatcher(throughputDispatcher)) assert(Await.result(slowOne ? "hogexecutor", timeout.duration) === "OK") @@ -109,14 +112,18 @@ class DispatcherActorSpec extends AkkaSpec(DispatcherActorSpec.config) with Defa val ready = new CountDownLatch(1) val fastOne = system.actorOf( - Props(context ⇒ { - case "ping" ⇒ if (works.get) latch.countDown(); context.stop(context.self) + Props(new Actor { + def receive = { + case "ping" ⇒ if (works.get) latch.countDown(); context.stop(self) + } }).withDispatcher(throughputDispatcher)) val slowOne = system.actorOf( - Props(context ⇒ { - case "hogexecutor" ⇒ ready.countDown(); start.await - case "ping" ⇒ works.set(false); context.stop(context.self) + Props(new Actor { + def receive = { + case "hogexecutor" ⇒ ready.countDown(); start.await + case "ping" ⇒ works.set(false); context.stop(self) + } }).withDispatcher(throughputDispatcher)) slowOne ! "hogexecutor" diff --git a/akka-actor-tests/src/test/scala/akka/actor/dispatch/PinnedActorSpec.scala b/akka-actor-tests/src/test/scala/akka/actor/dispatch/PinnedActorSpec.scala index 6dfbf2aedd..c1e8f8a3f6 100644 --- a/akka-actor-tests/src/test/scala/akka/actor/dispatch/PinnedActorSpec.scala +++ b/akka-actor-tests/src/test/scala/akka/actor/dispatch/PinnedActorSpec.scala @@ -36,7 +36,7 @@ class PinnedActorSpec extends AkkaSpec(PinnedActorSpec.config) with BeforeAndAft "support tell" in { var oneWay = new CountDownLatch(1) - val actor = system.actorOf(Props(self ⇒ { case "OneWay" ⇒ oneWay.countDown() }).withDispatcher("pinned-dispatcher")) + val actor = system.actorOf(Props(new Actor { def receive = { case "OneWay" ⇒ oneWay.countDown() } }).withDispatcher("pinned-dispatcher")) val result = actor ! "OneWay" assert(oneWay.await(1, TimeUnit.SECONDS)) system.stop(actor) diff --git a/akka-actor-tests/src/test/scala/akka/dataflow/Future2Actor.scala b/akka-actor-tests/src/test/scala/akka/dataflow/Future2Actor.scala index 97f5e0d236..0e3d358322 100644 --- a/akka-actor-tests/src/test/scala/akka/dataflow/Future2Actor.scala +++ b/akka-actor-tests/src/test/scala/akka/dataflow/Future2Actor.scala @@ -23,14 +23,14 @@ class Future2ActorSpec extends AkkaSpec with DefaultTimeout { } "support convenient sending to multiple destinations with implicit sender" in { - implicit val someActor = system.actorOf(Props(ctx ⇒ Actor.emptyBehavior)) + implicit val someActor = system.actorOf(Props(new Actor { def receive = Actor.emptyBehavior })) Future(42) pipeTo testActor pipeTo testActor expectMsgAllOf(1 second, 42, 42) lastSender must be(someActor) } "support convenient sending with explicit sender" in { - val someActor = system.actorOf(Props(ctx ⇒ Actor.emptyBehavior)) + val someActor = system.actorOf(Props(new Actor { def receive = Actor.emptyBehavior })) Future(42).to(testActor, someActor) expectMsgAllOf(1 second, 42) lastSender must be(someActor) diff --git a/akka-actor-tests/src/test/scala/akka/pattern/AskSpec.scala b/akka-actor-tests/src/test/scala/akka/pattern/AskSpec.scala index 31a314da2e..a67f65d870 100644 --- a/akka-actor-tests/src/test/scala/akka/pattern/AskSpec.scala +++ b/akka-actor-tests/src/test/scala/akka/pattern/AskSpec.scala @@ -9,9 +9,9 @@ import akka.testkit.AkkaSpec import scala.concurrent.util.duration._ import scala.concurrent.Await import akka.testkit.DefaultTimeout -import akka.actor.{ Props, ActorRef } import akka.util.Timeout import scala.util.Failure +import akka.actor.{ Actor, Props, ActorRef } class AskSpec extends AkkaSpec { @@ -50,7 +50,7 @@ class AskSpec extends AkkaSpec { "return broken promises on 0 timeout" in { implicit val timeout = Timeout(0 seconds) - val echo = system.actorOf(Props(ctx ⇒ { case x ⇒ ctx.sender ! x })) + val echo = system.actorOf(Props(new Actor { def receive = { case x ⇒ sender ! x } })) val f = echo ? "foo" val expectedMsg = "Timeout length for an `ask` must be greater or equal to 1. Question not sent to [%s]" format echo intercept[IllegalArgumentException] { @@ -60,7 +60,7 @@ class AskSpec extends AkkaSpec { "return broken promises on < 0 timeout" in { implicit val timeout = Timeout(-1000 seconds) - val echo = system.actorOf(Props(ctx ⇒ { case x ⇒ ctx.sender ! x })) + val echo = system.actorOf(Props(new Actor { def receive = { case x ⇒ sender ! x } })) val f = echo ? "foo" val expectedMsg = "Timeout length for an `ask` must be greater or equal to 1. Question not sent to [%s]" format echo intercept[IllegalArgumentException] { @@ -70,7 +70,7 @@ class AskSpec extends AkkaSpec { "return broken promises on infinite timeout" in { implicit val timeout = Timeout.never - val echo = system.actorOf(Props(ctx ⇒ { case x ⇒ ctx.sender ! x })) + val echo = system.actorOf(Props(new Actor { def receive = { case x ⇒ sender ! x } })) val f = echo ? "foo" val expectedMsg = "Timeouts to `ask` must be finite. Question not sent to [%s]" format echo intercept[IllegalArgumentException] { diff --git a/akka-actor/src/main/scala/akka/actor/ActorDSL.scala b/akka-actor/src/main/scala/akka/actor/ActorDSL.scala index 196c794f27..b1e36f7559 100644 --- a/akka-actor/src/main/scala/akka/actor/ActorDSL.scala +++ b/akka-actor/src/main/scala/akka/actor/ActorDSL.scala @@ -80,17 +80,15 @@ object ActorDSL extends dsl.Inbox with dsl.Creators { protected class Extension(val system: ExtendedActorSystem) extends akka.actor.Extension with InboxExtension { - val boss = system.asInstanceOf[ActorSystemImpl].systemActorOf(Props.empty, "dsl").asInstanceOf[RepointableActorRef] + val boss = system.asInstanceOf[ActorSystemImpl].systemActorOf(Props( + new Actor { + def receive = { case any ⇒ sender ! any } + }), "dsl").asInstanceOf[RepointableActorRef] { - val timeout = system.settings.CreationTimeout.duration - val deadline = Deadline.now + timeout - while (!boss.isStarted) { - if (deadline.hasTimeLeft) - if (system.isTerminated) throw new IllegalStateException("actor system is already shutdown") - else Thread.sleep(10) - else throw new TimeoutException("failed to create /system/dsl actor within " + timeout) - } + implicit val timeout = system.settings.CreationTimeout + if (Await.result(boss ? "OK", system.settings.CreationTimeout.duration) != "OK") + throw new IllegalStateException("Creation of boss actor did not succeed!") } lazy val config = system.settings.config.getConfig("akka.actor.dsl") diff --git a/akka-actor/src/main/scala/akka/actor/Props.scala b/akka-actor/src/main/scala/akka/actor/Props.scala index 637877dd66..693ca75565 100644 --- a/akka-actor/src/main/scala/akka/actor/Props.scala +++ b/akka-actor/src/main/scala/akka/actor/Props.scala @@ -74,11 +74,6 @@ object Props { * using the supplied thunk. */ def apply(creator: Creator[_ <: Actor]): Props = default.withCreator(creator.create) - - /** - * Returns a new Props whose creator will instantiate an Actor that has the behavior specified - */ - def apply(behavior: ActorContext ⇒ Actor.Receive): Props = apply(new Actor { def receive = behavior(context) }) } /** diff --git a/akka-camel/src/test/scala/akka/camel/internal/component/ActorEndpointPathTest.scala b/akka-camel/src/test/scala/akka/camel/internal/component/ActorEndpointPathTest.scala index ada4497f29..a98ffc9fd0 100644 --- a/akka-camel/src/test/scala/akka/camel/internal/component/ActorEndpointPathTest.scala +++ b/akka-camel/src/test/scala/akka/camel/internal/component/ActorEndpointPathTest.scala @@ -7,15 +7,15 @@ package akka.camel.internal.component import org.scalatest.mock.MockitoSugar import org.scalatest.matchers.MustMatchers import akka.camel.TestSupport.SharedCamelSystem -import akka.actor.Props import org.scalatest.WordSpec +import akka.actor.{ Actor, Props } class ActorEndpointPathTest extends WordSpec with SharedCamelSystem with MustMatchers with MockitoSugar { def find(path: String) = ActorEndpointPath.fromCamelPath(path).findActorIn(system) "findActorIn returns Some(actor ref) if actor exists" in { - val path = system.actorOf(Props(behavior = ctx ⇒ { case _ ⇒ {} }), "knownactor").path + val path = system.actorOf(Props(new Actor { def receive = { case _ ⇒ } }), "knownactor").path find(path.toString) must be('defined) } diff --git a/akka-testkit/src/test/scala/akka/testkit/AkkaSpec.scala b/akka-testkit/src/test/scala/akka/testkit/AkkaSpec.scala index 67478b35e3..a1d32e019e 100644 --- a/akka-testkit/src/test/scala/akka/testkit/AkkaSpec.scala +++ b/akka-testkit/src/test/scala/akka/testkit/AkkaSpec.scala @@ -7,15 +7,14 @@ import language.{ postfixOps, reflectiveCalls } import org.scalatest.{ WordSpec, BeforeAndAfterAll, Tag } import org.scalatest.matchers.MustMatchers -import akka.actor.{ Actor, ActorRef, Props, ActorSystem, PoisonPill, DeadLetter } +import akka.actor.{ Actor, Props, ActorSystem, PoisonPill, DeadLetter, ActorSystemImpl } import akka.event.{ Logging, LoggingAdapter } import scala.concurrent.util.duration._ -import scala.concurrent.Await +import scala.concurrent.{ Await, Future } import com.typesafe.config.{ Config, ConfigFactory } import java.util.concurrent.TimeoutException -import akka.dispatch.{ MessageDispatcher, Dispatchers } +import akka.dispatch.Dispatchers import akka.pattern.ask -import akka.actor.ActorSystemImpl object TimingTest extends Tag("timing") object LongRunningTest extends Tag("long-running") @@ -90,9 +89,8 @@ abstract class AkkaSpec(_system: ActorSystem) protected def atTermination() {} - def spawn(dispatcherId: String = Dispatchers.DefaultDispatcherId)(body: ⇒ Unit) { - system.actorOf(Props(ctx ⇒ { case "go" ⇒ try body finally ctx.stop(ctx.self) }).withDispatcher(dispatcherId)) ! "go" - } + def spawn(dispatcherId: String = Dispatchers.DefaultDispatcherId)(body: ⇒ Unit): Unit = + Future(body)(system.dispatchers.lookup(dispatcherId)) } @org.junit.runner.RunWith(classOf[org.scalatest.junit.JUnitRunner]) diff --git a/akka-testkit/src/test/scala/akka/testkit/TestActorRefSpec.scala b/akka-testkit/src/test/scala/akka/testkit/TestActorRefSpec.scala index 7df30be4c7..9d88c7b111 100644 --- a/akka-testkit/src/test/scala/akka/testkit/TestActorRefSpec.scala +++ b/akka-testkit/src/test/scala/akka/testkit/TestActorRefSpec.scala @@ -117,7 +117,7 @@ class TestActorRefSpec extends AkkaSpec("disp1.type=Dispatcher") with BeforeAndA "used with TestActorRef" in { val a = TestActorRef(Props(new Actor { - val nested = TestActorRef(Props(self ⇒ { case _ ⇒ })) + val nested = TestActorRef(Props(new Actor { def receive = { case _ ⇒ } })) def receive = { case _ ⇒ sender ! nested } })) a must not be (null) @@ -128,7 +128,7 @@ class TestActorRefSpec extends AkkaSpec("disp1.type=Dispatcher") with BeforeAndA "used with ActorRef" in { val a = TestActorRef(Props(new Actor { - val nested = context.actorOf(Props(self ⇒ { case _ ⇒ })) + val nested = context.actorOf(Props(new Actor { def receive = { case _ ⇒ } })) def receive = { case _ ⇒ sender ! nested } })) a must not be (null) From 3c3c84d4d8e99395a5e4976b3250633cec3b3f36 Mon Sep 17 00:00:00 2001 From: Viktor Klang Date: Thu, 30 Aug 2012 12:03:09 +0200 Subject: [PATCH 2/2] Adding migration notes around the removal of Props(ActorContext => Actor.Receive) --- akka-docs/project/migration-guide-2.0.x-2.1.x.rst | 13 ++++++++++++- 1 file changed, 12 insertions(+), 1 deletion(-) diff --git a/akka-docs/project/migration-guide-2.0.x-2.1.x.rst b/akka-docs/project/migration-guide-2.0.x-2.1.x.rst index 772e5ea784..852ab423ee 100644 --- a/akka-docs/project/migration-guide-2.0.x-2.1.x.rst +++ b/akka-docs/project/migration-guide-2.0.x-2.1.x.rst @@ -201,7 +201,18 @@ v2.0 Java:: v2.1 Java:: - ActorRef router2 = system.actorOf(new Props().withRouter(RoundRobinRouter.create(routees))); + ActorRef router2 = system.actorOf(new Props().withRouter(RoundRobinRouter.create(routees))); + +Props: Function-based creation +============================== + +v2.0 Scala:: + + Props(context => { case someMessage => context.sender ! someMessage }) + +v2.1 Scala:: + + Props(new Actor { def receive = { case someMessage => sender ! someMessage } }) Failing Send ============