diff --git a/akka-actor-typed-tests/src/test/java/akka/actor/typed/javadsl/ActorCompile.java b/akka-actor-typed-tests/src/test/java/akka/actor/typed/javadsl/ActorCompile.java index 0c25b27ff1..47937f7807 100644 --- a/akka-actor-typed-tests/src/test/java/akka/actor/typed/javadsl/ActorCompile.java +++ b/akka-actor-typed-tests/src/test/java/akka/actor/typed/javadsl/ActorCompile.java @@ -33,8 +33,8 @@ public class ActorCompile { } } - Behavior actor1 = immutable((ctx, msg) -> stopped(), (ctx, signal) -> same()); - Behavior actor2 = immutable((ctx, msg) -> unhandled()); + Behavior actor1 = Behaviors.receive((ctx, msg) -> stopped(), (ctx, signal) -> same()); + Behavior actor2 = Behaviors.receive((ctx, msg) -> unhandled()); Behavior actor4 = empty(); Behavior actor5 = ignore(); Behavior actor6 = tap((ctx, signal) -> {}, (ctx, msg) -> {}, actor5); @@ -44,14 +44,14 @@ public class ActorCompile { return monitor(self, ignore()); }); Behavior actor9 = widened(actor7, pf -> pf.match(MyMsgA.class, x -> x)); - Behavior actor10 = immutable((ctx, msg) -> stopped(actor4), (ctx, signal) -> same()); + Behavior actor10 = Behaviors.receive((ctx, msg) -> stopped(actor4), (ctx, signal) -> same()); ActorSystem system = ActorSystem.create(actor1, "Sys"); { - Behaviors.immutable((ctx, msg) -> { + Behaviors.receive((ctx, msg) -> { if (msg instanceof MyMsgA) { - return immutable((ctx2, msg2) -> { + return Behaviors.receive((ctx2, msg2) -> { if (msg2 instanceof MyMsgB) { ((MyMsgA) msg).replyTo.tell(((MyMsgB) msg2).greeting); @@ -79,7 +79,7 @@ public class ActorCompile { } @Override - public Behavior receiveMessage(ActorContext ctx, MyMsg msg) throws Exception { + public Behavior receive(ActorContext ctx, MyMsg msg) throws Exception { ActorRef adapter = ctx.asJava().messageAdapter(String.class, s -> new MyMsgB(s.toUpperCase())); return this; } diff --git a/akka-actor-typed-tests/src/test/java/akka/actor/typed/javadsl/ActorContextAskTest.java b/akka-actor-typed-tests/src/test/java/akka/actor/typed/javadsl/ActorContextAskTest.java index f28afd81ff..8c7474d7ff 100644 --- a/akka-actor-typed-tests/src/test/java/akka/actor/typed/javadsl/ActorContextAskTest.java +++ b/akka-actor-typed-tests/src/test/java/akka/actor/typed/javadsl/ActorContextAskTest.java @@ -6,7 +6,6 @@ package akka.actor.typed.javadsl; import akka.actor.typed.ActorRef; import akka.actor.typed.Behavior; -import akka.testkit.AkkaJUnitActorSystemResource; import akka.testkit.AkkaSpec; import akka.testkit.typed.javadsl.TestKitJunitResource; import akka.testkit.typed.javadsl.TestProbe; @@ -32,7 +31,7 @@ public class ActorContextAskTest extends JUnitSuite { @Test public void provideASafeAsk() { - final Behavior pingPongBehavior = Behaviors.immutable((ActorContext context, Ping message) -> { + final Behavior pingPongBehavior = Behaviors.receive((ActorContext context, Ping message) -> { message.respondTo.tell(new Pong()); return Behaviors.same(); }); @@ -50,7 +49,7 @@ public class ActorContextAskTest extends JUnitSuite { else return exception; }); - return Behaviors.immutable((ActorContext context, Object message) -> { + return Behaviors.receive((ActorContext context, Object message) -> { probe.ref().tell(message); return Behaviors.same(); }); diff --git a/akka-actor-typed-tests/src/test/java/akka/actor/typed/javadsl/ActorLoggingTest.java b/akka-actor-typed-tests/src/test/java/akka/actor/typed/javadsl/ActorLoggingTest.java index a7b0ee8155..8313771626 100644 --- a/akka-actor-typed-tests/src/test/java/akka/actor/typed/javadsl/ActorLoggingTest.java +++ b/akka-actor-typed-tests/src/test/java/akka/actor/typed/javadsl/ActorLoggingTest.java @@ -51,7 +51,7 @@ public class ActorLoggingTest extends JUnitSuite { mdc.put("txId", msg.getTransactionId()); return mdc; }, - Behaviors.immutable(Protocol.class) + Behaviors.receive(Protocol.class) .onMessage(Message.class, (ctx, msg) -> { ctx.getLog().info(msg.toString()); return Behaviors.same(); diff --git a/akka-actor-typed-tests/src/test/java/akka/actor/typed/javadsl/AdapterTest.java b/akka-actor-typed-tests/src/test/java/akka/actor/typed/javadsl/AdapterTest.java index ad18b8e5d9..a66cc0d2da 100644 --- a/akka-actor-typed-tests/src/test/java/akka/actor/typed/javadsl/AdapterTest.java +++ b/akka-actor-typed-tests/src/test/java/akka/actor/typed/javadsl/AdapterTest.java @@ -50,7 +50,7 @@ public class AdapterTest extends JUnitSuite { static Behavior create(akka.actor.ActorRef ref, akka.actor.ActorRef probe) { Typed1 logic = new Typed1(ref, probe); - return immutable( + return receive( (ctx, msg) -> logic.onMessage(ctx, msg), (ctx, sig) -> logic.onSignal(ctx, sig)); } @@ -187,7 +187,7 @@ public class AdapterTest extends JUnitSuite { } static Behavior typed2() { - return Behaviors.immutable((ctx, msg) -> { + return Behaviors.receive((ctx, msg) -> { if (msg instanceof Ping) { ActorRef replyTo = ((Ping) msg).replyTo; replyTo.tell("pong"); diff --git a/akka-actor-typed-tests/src/test/java/akka/actor/typed/javadsl/BehaviorBuilderTest.java b/akka-actor-typed-tests/src/test/java/akka/actor/typed/javadsl/BehaviorBuilderTest.java index 17b364d0e5..40dc219122 100644 --- a/akka-actor-typed-tests/src/test/java/akka/actor/typed/javadsl/BehaviorBuilderTest.java +++ b/akka-actor-typed-tests/src/test/java/akka/actor/typed/javadsl/BehaviorBuilderTest.java @@ -33,7 +33,7 @@ public class BehaviorBuilderTest extends JUnitSuite { @Test public void shouldCompile() { - Behavior b = Behaviors.immutable(Message.class) + Behavior b = Behaviors.receive(Message.class) .onMessage(One.class, (ctx, o) -> { o.foo(); return same(); @@ -66,7 +66,7 @@ public class BehaviorBuilderTest extends JUnitSuite { } public Behavior immutableCounter(int currentValue) { - return Behaviors.immutable(CounterMessage.class) + return Behaviors.receive(CounterMessage.class) .onMessage(Increase.class, (ctx, o) -> { return immutableCounter(currentValue + 1); }) diff --git a/akka-actor-typed-tests/src/test/java/akka/actor/typed/javadsl/ReceiveBuilderTest.java b/akka-actor-typed-tests/src/test/java/akka/actor/typed/javadsl/ReceiveBuilderTest.java index 9d0f5c0fa0..20c7a94688 100644 --- a/akka-actor-typed-tests/src/test/java/akka/actor/typed/javadsl/ReceiveBuilderTest.java +++ b/akka-actor-typed-tests/src/test/java/akka/actor/typed/javadsl/ReceiveBuilderTest.java @@ -18,7 +18,7 @@ public class ReceiveBuilderTest extends JUnitSuite { @Test public void testMutableCounter() { - Behavior mutable = Behaviors.mutable(ctx -> new Behaviors.MutableBehavior() { + Behavior mutable = Behaviors.setup(ctx -> new MutableBehavior() { int currentValue = 0; private Behavior receiveIncrease(BehaviorBuilderTest.Increase msg) { @@ -41,7 +41,7 @@ public class ReceiveBuilderTest extends JUnitSuite { }); } - private static class MyMutableBehavior extends Behaviors.MutableBehavior { + private static class MyMutableBehavior extends MutableBehavior { private int value; public MyMutableBehavior(int initialValue) { @@ -59,6 +59,6 @@ public class ReceiveBuilderTest extends JUnitSuite { @Test public void testInitializationOrder() throws Exception { MyMutableBehavior mutable = new MyMutableBehavior(42); - assertEquals(Behaviors.unhandled(), mutable.receiveMessage(null, new BehaviorBuilderTest.Increase())); + assertEquals(Behaviors.unhandled(), mutable.receive(null, new BehaviorBuilderTest.Increase())); } } diff --git a/akka-actor-typed-tests/src/test/java/akka/actor/typed/javadsl/WatchTest.java b/akka-actor-typed-tests/src/test/java/akka/actor/typed/javadsl/WatchTest.java index c5574ddfab..ead701649a 100644 --- a/akka-actor-typed-tests/src/test/java/akka/actor/typed/javadsl/WatchTest.java +++ b/akka-actor-typed-tests/src/test/java/akka/actor/typed/javadsl/WatchTest.java @@ -8,11 +8,9 @@ import java.util.concurrent.CompletionStage; import java.util.concurrent.TimeUnit; import akka.Done; -import akka.testkit.AkkaSpec; import akka.testkit.typed.javadsl.TestKitJunitResource; import org.junit.ClassRule; import org.scalatest.junit.JUnitSuite; -import scala.concurrent.Await; import scala.concurrent.duration.Duration; import akka.util.Timeout; import org.junit.Test; @@ -45,13 +43,13 @@ public class WatchTest extends JUnitSuite { // final FiniteDuration fiveSeconds = FiniteDuration.create(5, TimeUnit.SECONDS); final Timeout timeout = new Timeout(Duration.create(5, TimeUnit.SECONDS)); - final Behavior exitingActor = immutable((ctx, msg) -> { + final Behavior exitingActor = receive((ctx, msg) -> { System.out.println("Stopping!"); return stopped(); }); private Behavior waitingForTermination(ActorRef replyWhenTerminated) { - return immutable( + return receive( (ctx, msg) -> unhandled(), (ctx, sig) -> { if (sig instanceof Terminated) { @@ -63,7 +61,7 @@ public class WatchTest extends JUnitSuite { } private Behavior waitingForMessage(ActorRef replyWhenReceived) { - return immutable( + return receive( (ctx, msg) -> { if (msg instanceof CustomTerminationMessage) { replyWhenReceived.tell(Done.getInstance()); @@ -77,7 +75,7 @@ public class WatchTest extends JUnitSuite { @Test public void shouldWatchTerminatingActor() throws Exception { - Behavior exiting = Behaviors.immutable(RunTest.class) + Behavior exiting = Behaviors.receive(RunTest.class) .onMessage(RunTest.class, (ctx, msg) -> { ActorRef watched = ctx.spawn(exitingActor, "exitingActor"); ctx.watch(watched); @@ -92,7 +90,7 @@ public class WatchTest extends JUnitSuite { @Test public void shouldWatchWithCustomMessage() throws Exception { - Behavior exiting = Behaviors.immutable(Message.class) + Behavior exiting = Behaviors.receive(Message.class) .onMessage(RunTest.class, (ctx, msg) -> { ActorRef watched = ctx.spawn(exitingActor, "exitingActor"); ctx.watchWith(watched, new CustomTerminationMessage()); diff --git a/akka-actor-typed-tests/src/test/java/akka/actor/typed/receptionist/ReceptionistApiTest.java b/akka-actor-typed-tests/src/test/java/akka/actor/typed/receptionist/ReceptionistApiTest.java index f0cc6bfe56..fcfde83b34 100644 --- a/akka-actor-typed-tests/src/test/java/akka/actor/typed/receptionist/ReceptionistApiTest.java +++ b/akka-actor-typed-tests/src/test/java/akka/actor/typed/receptionist/ReceptionistApiTest.java @@ -84,7 +84,7 @@ public class ReceptionistApiTest { // to cover as much of the API as possible ctx.getSystem().receptionist().tell(Receptionist.register(key, ctx.getSelf().narrow(), ctx.getSelf().narrow())); - return Behaviors.immutable(Object.class) + return Behaviors.receive(Object.class) // matching is done best using the predicate version .onMessage(Receptionist.Listing.class, listing -> listing.isForKey(key), (msgCtx, listing) -> { Set> services = listing.getServiceInstances(key); diff --git a/akka-actor-typed-tests/src/test/java/jdocs/akka/typed/FaultToleranceDocTest.java b/akka-actor-typed-tests/src/test/java/jdocs/akka/typed/FaultToleranceDocTest.java index fc03b53446..b896a2df3d 100644 --- a/akka-actor-typed-tests/src/test/java/jdocs/akka/typed/FaultToleranceDocTest.java +++ b/akka-actor-typed-tests/src/test/java/jdocs/akka/typed/FaultToleranceDocTest.java @@ -26,7 +26,7 @@ public class FaultToleranceDocTest extends JUnitSuite { @Test public void bubblingSample() { // #bubbling-example - final Behavior failingChildBehavior = Behaviors.immutable(Message.class) + final Behavior failingChildBehavior = Behaviors.receive(Message.class) .onMessage(Fail.class, (ctx, message) -> { throw new RuntimeException(message.text); }) @@ -42,7 +42,7 @@ public class FaultToleranceDocTest extends JUnitSuite { // here we don't handle Terminated at all which means that // when the child fails or stops gracefully this actor will // fail with a DeathWatchException - return Behaviors.immutable(Message.class) + return Behaviors.receive(Message.class) .onMessage(Message.class, (innerCtx, msg) -> { // just pass messages on to the child child.tell(msg); @@ -58,7 +58,7 @@ public class FaultToleranceDocTest extends JUnitSuite { // here we don't handle Terminated at all which means that // when middle management fails with a DeathWatchException // this actor will also fail - return Behaviors.immutable(Message.class) + return Behaviors.receive(Message.class) .onMessage(Message.class, (innerCtx, msg) -> { // just pass messages on to the child middleManagement.tell(msg); diff --git a/akka-actor-typed-tests/src/test/java/jdocs/akka/typed/GracefulStopDocTest.java b/akka-actor-typed-tests/src/test/java/jdocs/akka/typed/GracefulStopDocTest.java index 2ba4f42f52..870fa7f645 100644 --- a/akka-actor-typed-tests/src/test/java/jdocs/akka/typed/GracefulStopDocTest.java +++ b/akka-actor-typed-tests/src/test/java/jdocs/akka/typed/GracefulStopDocTest.java @@ -42,7 +42,7 @@ public class GracefulStopDocTest { } } - public static final Behavior mcpa = Behaviors.immutable(JobControlLanguage.class) + public static final Behavior mcpa = Behaviors.receive(JobControlLanguage.class) .onMessage(SpawnJob.class, (ctx, msg) -> { ctx.getSystem().log().info("Spawning job {}!", msg.name); ctx.spawn(Job.job(msg.name), msg.name); @@ -57,7 +57,7 @@ public class GracefulStopDocTest { // perform graceful stop, executing cleanup before final system termination // behavior executing cleanup is passed as a parameter to Actor.stopped - return Behaviors.stopped(Behaviors.onSignal((context, PostStop) -> { + return Behaviors.stopped(Behaviors.receiveSignal((context, PostStop) -> { context.getSystem().log().info("Cleanup!"); return Behaviors.same(); })); @@ -92,7 +92,7 @@ public class GracefulStopDocTest { public static class Job { public static Behavior job(String name) { - return Behaviors.onSignal((ctx, PostStop) -> { + return Behaviors.receiveSignal((ctx, PostStop) -> { ctx.getSystem().log().info("Worker {} stopped", name); return Behaviors.same(); }); diff --git a/akka-actor-typed-tests/src/test/java/jdocs/akka/typed/InteractionPatternsTest.java b/akka-actor-typed-tests/src/test/java/jdocs/akka/typed/InteractionPatternsTest.java index 2d2540ae5b..e1465e2060 100644 --- a/akka-actor-typed-tests/src/test/java/jdocs/akka/typed/InteractionPatternsTest.java +++ b/akka-actor-typed-tests/src/test/java/jdocs/akka/typed/InteractionPatternsTest.java @@ -32,7 +32,7 @@ public class InteractionPatternsTest extends JUnitSuite { } } - static final Behavior printerBehavior = Behaviors.immutable(PrintMe.class) + static final Behavior printerBehavior = Behaviors.receive(PrintMe.class) .onMessage(PrintMe.class, (ctx, printMe) -> { ctx.getLog().info(printMe.message); return Behaviors.same(); @@ -61,7 +61,7 @@ public class InteractionPatternsTest extends JUnitSuite { // #request-response-respond // actor behavior - Behaviors.immutable(Request.class) + Behaviors.receive(Request.class) .onMessage(Request.class, (ctx, request) -> { // ... process request ... request.respondTo.tell(new Response("Here's your response!")); @@ -170,7 +170,7 @@ public class InteractionPatternsTest extends JUnitSuite { } } - public static class Translator extends Behaviors.MutableBehavior { + public static class Translator extends MutableBehavior { private final ActorContext ctx; private final ActorRef backend; private final ActorRef backendResponseAdapter; @@ -290,7 +290,7 @@ public class InteractionPatternsTest extends JUnitSuite { private static Behavior idle(TimerScheduler timers, ActorRef target, FiniteDuration after, int maxSize) { - return Behaviors.immutable(Msg.class) + return Behaviors.receive(Msg.class) .onMessage(Msg.class, (ctx, msg) -> { timers.startSingleTimer(TIMER_KEY, new TimeoutMsg(), after); List buffer = new ArrayList<>(); @@ -302,7 +302,7 @@ public class InteractionPatternsTest extends JUnitSuite { private static Behavior active(List buffer, TimerScheduler timers, ActorRef target, FiniteDuration after, int maxSize) { - return Behaviors.immutable(Msg.class) + return Behaviors.receive(Msg.class) .onMessage(TimeoutMsg.class, (ctx, msg) -> { target.tell(new Batch(buffer)); return idle(timers, target, after, maxSize); @@ -359,7 +359,7 @@ public class InteractionPatternsTest extends JUnitSuite { } static final Behavior halBehavior = - Behaviors.immutable(HalCommand.class) + Behaviors.receive(HalCommand.class) .onMessage(OpenThePodBayDoorsPlease.class, (ctx, msg) -> { msg.respondTo.tell(new HalResponse("I'm sorry, Dave. I'm afraid I can't do that.")); return Behaviors.same(); @@ -417,7 +417,7 @@ public class InteractionPatternsTest extends JUnitSuite { } }); - return Behaviors.immutable(DaveProtocol.class) + return Behaviors.receive(DaveProtocol.class) // the adapted message ends up being processed like any other // message sent to the actor .onMessage(AdaptedResponse.class, (innerCtx, response) -> { @@ -511,7 +511,7 @@ public class InteractionPatternsTest extends JUnitSuite { final ActorRef keyCabinet = ctx.spawn(keyCabinetBehavior, "key-cabinet"); final ActorRef drawer = ctx.spawn(drawerBehavior, "drawer"); - return Behaviors.immutable(HomeCommand.class) + return Behaviors.receive(HomeCommand.class) .onMessage(LeaveHome.class, (innerCtx, msg) -> { ctx.spawn(new PrepareToLeaveHome(msg.who, msg.respondTo, keyCabinet, drawer), "leaving" + msg.who); return Behavior.same(); @@ -520,7 +520,7 @@ public class InteractionPatternsTest extends JUnitSuite { } // per session actor behavior - class PrepareToLeaveHome extends Behaviors.MutableBehavior { + class PrepareToLeaveHome extends MutableBehavior { private final String whoIsLeaving; private final ActorRef respondTo; private final ActorRef keyCabinet; diff --git a/akka-actor-typed-tests/src/test/java/jdocs/akka/typed/IntroTest.java b/akka-actor-typed-tests/src/test/java/jdocs/akka/typed/IntroTest.java index 64e7afe741..5785bd7f21 100644 --- a/akka-actor-typed-tests/src/test/java/jdocs/akka/typed/IntroTest.java +++ b/akka-actor-typed-tests/src/test/java/jdocs/akka/typed/IntroTest.java @@ -48,7 +48,7 @@ public class IntroTest { } } - public static final Behavior greeter = Behaviors.immutable((ctx, msg) -> { + public static final Behavior greeter = Behaviors.receive((ctx, msg) -> { System.out.println("Hello " + msg.whom + "!"); msg.replyTo.tell(new Greeted(msg.whom)); return Behaviors.same(); @@ -141,7 +141,7 @@ public class IntroTest { } private static Behavior chatRoom(List> sessions) { - return Behaviors.immutable(RoomCommand.class) + return Behaviors.receive(RoomCommand.class) .onMessage(GetSession.class, (ctx, getSession) -> { ActorRef client = getSession.replyTo; ActorRef ses = ctx.spawn( @@ -166,7 +166,7 @@ public class IntroTest { ActorRef room, String screenName, ActorRef client) { - return Behaviors.immutable(ChatRoom.SessionCommand.class) + return Behaviors.receive(ChatRoom.SessionCommand.class) .onMessage(PostMessage.class, (ctx, post) -> { // from client, publish to others via the room room.tell(new PublishSessionMessage(screenName, post.message)); @@ -190,7 +190,7 @@ public class IntroTest { } public static Behavior behavior() { - return Behaviors.immutable(ChatRoom.SessionEvent.class) + return Behaviors.receive(ChatRoom.SessionEvent.class) .onMessage(ChatRoom.SessionDenied.class, (ctx, msg) -> { System.out.println("cannot start chat room session: " + msg.reason); return Behaviors.stopped(); @@ -221,7 +221,7 @@ public class IntroTest { ctx.watch(gabbler); chatRoom.tell(new ChatRoom.GetSession("ol’ Gabbler", gabbler)); - return Behaviors.immutable(Void.class) + return Behaviors.receive(Void.class) .onSignal(Terminated.class, (c, sig) -> Behaviors.stopped()) .build(); }); diff --git a/akka-actor-typed-tests/src/test/java/jdocs/akka/typed/MutableIntroTest.java b/akka-actor-typed-tests/src/test/java/jdocs/akka/typed/MutableIntroTest.java index a02fbc378b..38b499d756 100644 --- a/akka-actor-typed-tests/src/test/java/jdocs/akka/typed/MutableIntroTest.java +++ b/akka-actor-typed-tests/src/test/java/jdocs/akka/typed/MutableIntroTest.java @@ -10,6 +10,7 @@ import akka.actor.typed.Behavior; import akka.actor.typed.javadsl.Behaviors; import akka.actor.typed.javadsl.Behaviors.Receive; import akka.actor.typed.javadsl.ActorContext; +import akka.actor.typed.javadsl.MutableBehavior; //#imports import java.net.URLEncoder; import java.nio.charset.StandardCharsets; @@ -82,10 +83,10 @@ public class MutableIntroTest { //#chatroom-behavior public static Behavior behavior() { - return Behaviors.mutable(ChatRoomBehavior::new); + return Behaviors.setup(ChatRoomBehavior::new); } - public static class ChatRoomBehavior extends Behaviors.MutableBehavior { + public static class ChatRoomBehavior extends MutableBehavior { final ActorContext ctx; final List> sessions = new ArrayList<>(); @@ -120,7 +121,7 @@ public class MutableIntroTest { ActorRef room, String screenName, ActorRef client) { - return Behaviors.immutable(ChatRoom.SessionCommand.class) + return Behaviors.receive(ChatRoom.SessionCommand.class) .onMessage(PostMessage.class, (ctx, post) -> { // from client, publish to others via the room room.tell(new PublishSessionMessage(screenName, post.message)); diff --git a/akka-actor-typed-tests/src/test/java/jdocs/akka/typed/StashDocTest.java b/akka-actor-typed-tests/src/test/java/jdocs/akka/typed/StashDocTest.java index 920fd78b47..665f9ef609 100644 --- a/akka-actor-typed-tests/src/test/java/jdocs/akka/typed/StashDocTest.java +++ b/akka-actor-typed-tests/src/test/java/jdocs/akka/typed/StashDocTest.java @@ -103,7 +103,7 @@ public class StashDocTest extends JUnitSuite { } private Behavior init() { - return Behaviors.immutable(Command.class) + return Behaviors.receive(Command.class) .onMessage(InitialState.class, (ctx, msg) -> { // now we are ready to handle stashed messages if any return buffer.unstashAll(ctx, active(msg.value)); @@ -120,7 +120,7 @@ public class StashDocTest extends JUnitSuite { } private Behavior active(String state) { - return Behaviors.immutable(Command.class) + return Behaviors.receive(Command.class) .onMessage(Get.class, (ctx, msg) -> { msg.replyTo.tell(state); return Behaviors.same(); @@ -139,7 +139,7 @@ public class StashDocTest extends JUnitSuite { } private Behavior saving(String state, ActorRef replyTo) { - return Behaviors.immutable(Command.class) + return Behaviors.receive(Command.class) .onMessageEquals(SaveSuccess.instance, ctx -> { replyTo.tell(Done.getInstance()); return buffer.unstashAll(ctx, active(state)); diff --git a/akka-actor-typed-tests/src/test/java/jdocs/akka/typed/coexistence/TypedWatchingUntypedTest.java b/akka-actor-typed-tests/src/test/java/jdocs/akka/typed/coexistence/TypedWatchingUntypedTest.java index 10a2c1b461..1dbe57c2a2 100644 --- a/akka-actor-typed-tests/src/test/java/jdocs/akka/typed/coexistence/TypedWatchingUntypedTest.java +++ b/akka-actor-typed-tests/src/test/java/jdocs/akka/typed/coexistence/TypedWatchingUntypedTest.java @@ -45,7 +45,7 @@ public class TypedWatchingUntypedTest extends JUnitSuite { second.tell(new Typed.Ping(context.getSelf().narrow()), Adapter.toUntyped(context.getSelf())); - return akka.actor.typed.javadsl.Behaviors.immutable(Typed.Command.class) + return akka.actor.typed.javadsl.Behaviors.receive(Typed.Command.class) .onMessage(Typed.Pong.class, (ctx, msg) -> { Adapter.stop(ctx, second); return same(); diff --git a/akka-actor-typed-tests/src/test/java/jdocs/akka/typed/coexistence/UntypedWatchingTypedTest.java b/akka-actor-typed-tests/src/test/java/jdocs/akka/typed/coexistence/UntypedWatchingTypedTest.java index 0d5efe24cb..8397b850f7 100644 --- a/akka-actor-typed-tests/src/test/java/jdocs/akka/typed/coexistence/UntypedWatchingTypedTest.java +++ b/akka-actor-typed-tests/src/test/java/jdocs/akka/typed/coexistence/UntypedWatchingTypedTest.java @@ -67,7 +67,7 @@ public class UntypedWatchingTypedTest extends JUnitSuite { public static class Pong { } public static Behavior behavior() { - return Behaviors.immutable(Typed.Command.class) + return Behaviors.receive(Typed.Command.class) .onMessage(Typed.Ping.class, (ctx, msg) -> { msg.replyTo.tell(new Pong()); return same(); diff --git a/akka-actor-typed-tests/src/test/scala/akka/actor/typed/ActorContextSpec.scala b/akka-actor-typed-tests/src/test/scala/akka/actor/typed/ActorContextSpec.scala index e9e7183769..7cf46596eb 100644 --- a/akka-actor-typed-tests/src/test/scala/akka/actor/typed/ActorContextSpec.scala +++ b/akka-actor-typed-tests/src/test/scala/akka/actor/typed/ActorContextSpec.scala @@ -73,7 +73,7 @@ abstract class ActorContextSpec extends ActorTestKit with TypedAkkaSpecWithShutd "canonicalize behaviors" in { val probe = TestProbe[Event]() - lazy val behavior: Behavior[Command] = Behaviors.immutable[Command] { (_, message) ⇒ + lazy val behavior: Behavior[Command] = Behaviors.receive[Command] { (_, message) ⇒ message match { case Ping ⇒ probe.ref ! Pong @@ -103,10 +103,10 @@ abstract class ActorContextSpec extends ActorTestKit with TypedAkkaSpecWithShutd "correctly wire the lifecycle hook" in { val probe = TestProbe[Event]() - val internal = (Behaviors.immutablePartial[Command] { + val internal = (Behaviors.receivePartial[Command] { case (_, Fail) ⇒ throw new TestException("Boom") - } onSignal { + } receiveSignal { case (_, signal) ⇒ probe.ref ! GotSignal(signal) Behaviors.same @@ -122,9 +122,9 @@ abstract class ActorContextSpec extends ActorTestKit with TypedAkkaSpecWithShutd val probe = TestProbe[Event]() val behavior: Behavior[Command] = ( - Behaviors.immutablePartial[Command] { + Behaviors.receivePartial[Command] { case (_, Stop) ⇒ Behaviors.stopped - } onSignal { + } receiveSignal { case (_, signal) ⇒ probe.ref ! GotSignal(signal) Behaviors.same @@ -138,12 +138,12 @@ abstract class ActorContextSpec extends ActorTestKit with TypedAkkaSpecWithShutd "restart and stop a child actor" in { val probe = TestProbe[Event]() - val child: Behavior[Command] = (Behaviors.immutablePartial[Command] { + val child: Behavior[Command] = (Behaviors.receivePartial[Command] { case (_, Fail) ⇒ throw new TestException("Boom") case (_, Ping) ⇒ probe.ref ! Pong Behaviors.same - } onSignal { + } receiveSignal { case (_, signal) ⇒ probe.ref ! GotChildSignal(signal) Behavior.stopped @@ -156,11 +156,11 @@ abstract class ActorContextSpec extends ActorTestKit with TypedAkkaSpecWithShutd ctx.watch(childRef) probe.ref ! ChildMade(childRef) - (Behaviors.immutablePartial[Command] { + (Behaviors.receivePartial[Command] { case (ctx, StopRef(ref)) ⇒ ctx.stop(ref) Behavior.same - } onSignal { + } receiveSignal { case (_, signal) ⇒ probe.ref ! GotSignal(signal) Behavior.stopped @@ -185,11 +185,11 @@ abstract class ActorContextSpec extends ActorTestKit with TypedAkkaSpecWithShutd val childRef = ctx.spawnAnonymous(child) ctx.watch(childRef) probe.ref ! ChildMade(childRef) - Behaviors.immutablePartial[Command] { + Behaviors.receivePartial[Command] { case (ctx, StopRef(ref)) ⇒ ctx.stop(ref) Behaviors.same - } onSignal { + } receiveSignal { case (_, signal) ⇒ probe.ref ! GotSignal(signal) Behavior.stopped @@ -205,7 +205,7 @@ abstract class ActorContextSpec extends ActorTestKit with TypedAkkaSpecWithShutd val probe = TestProbe[Int]() val internal = Behaviors.setup[Command](_ ⇒ { var counter = 0 - Behaviors.immutablePartial[Command] { + Behaviors.receivePartial[Command] { case (_, Ping) ⇒ counter += 1 probe.ref ! counter @@ -227,7 +227,7 @@ abstract class ActorContextSpec extends ActorTestKit with TypedAkkaSpecWithShutd val probe = TestProbe[Int]() val internal = Behaviors.setup[Command](_ ⇒ { var counter = 0 - Behaviors.immutablePartial[Command] { + Behaviors.receivePartial[Command] { case (_, Ping) ⇒ counter += 1 probe.ref ! counter @@ -247,25 +247,25 @@ abstract class ActorContextSpec extends ActorTestKit with TypedAkkaSpecWithShutd "stop upon stop" in { val probe = TestProbe[Event]() - val behavior = (Behaviors.immutablePartial[Command] { + val behavior = (Behaviors.receivePartial[Command] { case (_, Ping) ⇒ probe.ref ! Pong Behaviors.same case (_, Fail) ⇒ throw new TestException("boom") - } onSignal { + } receiveSignal { case (_, PostStop) ⇒ probe.ref ! GotSignal(PostStop) Behavior.same }).decorate val actorToWatch = spawn(behavior) val watcher: ActorRef[Command] = spawn(( - Behaviors.immutablePartial[Any] { + Behaviors.receivePartial[Any] { case (ctx, Ping) ⇒ ctx.watch(actorToWatch) probe.ref ! Pong Behavior.same - } onSignal { + } receiveSignal { case (_, signal) ⇒ probe.ref ! GotSignal(signal) Behavior.same @@ -283,7 +283,7 @@ abstract class ActorContextSpec extends ActorTestKit with TypedAkkaSpecWithShutd "not stop non-child actor" in { val probe = TestProbe[Event]() val victim = spawn(Behaviors.empty[Command]) - val actor = spawn(Behaviors.immutablePartial[Command] { + val actor = spawn(Behaviors.receivePartial[Command] { case (_, Ping) ⇒ probe.ref ! Pong Behaviors.same @@ -305,7 +305,7 @@ abstract class ActorContextSpec extends ActorTestKit with TypedAkkaSpecWithShutd "watch a child actor before its termination" in { val probe = TestProbe[Event]() - val child = Behaviors.immutablePartial[Command] { + val child = Behaviors.receivePartial[Command] { case (_, Stop) ⇒ Behaviors.stopped }.decorate @@ -314,11 +314,11 @@ abstract class ActorContextSpec extends ActorTestKit with TypedAkkaSpecWithShutd val childRef = ctx.spawn(child, "A") ctx.watch(childRef) probe.ref ! ChildMade(childRef) - Behaviors.immutablePartial[Command] { + Behaviors.receivePartial[Command] { case (_, Ping) ⇒ probe.ref ! Pong Behaviors.same - } onSignal { + } receiveSignal { case (_, signal) ⇒ probe.ref ! GotSignal(signal) Behaviors.same @@ -332,7 +332,7 @@ abstract class ActorContextSpec extends ActorTestKit with TypedAkkaSpecWithShutd "watch a child actor after its termination" in { val probe = TestProbe[Event]() - val child = Behaviors.immutablePartial[Command] { + val child = Behaviors.receivePartial[Command] { case (_, Stop) ⇒ Behaviors.stopped }.decorate @@ -340,12 +340,12 @@ abstract class ActorContextSpec extends ActorTestKit with TypedAkkaSpecWithShutd Behaviors.setup[Command](ctx ⇒ { val childRef = ctx.spawn(child, "A") probe.ref ! ChildMade(childRef) - Behaviors.immutablePartial[Command] { + Behaviors.receivePartial[Command] { case (ctx, Watch(ref)) ⇒ ctx.watch(ref) probe.ref ! Pong Behaviors.same - } onSignal { + } receiveSignal { case (_, signal) ⇒ probe.ref ! GotSignal(signal) Behaviors.same @@ -363,7 +363,7 @@ abstract class ActorContextSpec extends ActorTestKit with TypedAkkaSpecWithShutd "unwatch a child actor before its termination" in { val probe = TestProbe[Event]() - val child = Behaviors.immutablePartial[Command] { + val child = Behaviors.receivePartial[Command] { case (_, Stop) ⇒ Behaviors.stopped }.decorate @@ -371,7 +371,7 @@ abstract class ActorContextSpec extends ActorTestKit with TypedAkkaSpecWithShutd Behaviors.setup[Command](ctx ⇒ { val childRef = ctx.spawn(child, "A") probe.ref ! ChildMade(childRef) - Behaviors.immutablePartial[Command] { + Behaviors.receivePartial[Command] { case (ctx, Watch(ref)) ⇒ ctx.watch(ref) probe.ref ! Pong @@ -380,7 +380,7 @@ abstract class ActorContextSpec extends ActorTestKit with TypedAkkaSpecWithShutd ctx.unwatch(ref) probe.ref ! Pong Behaviors.same - } onSignal { + } receiveSignal { case (_, signal) ⇒ probe.ref ! GotSignal(signal) Behaviors.same @@ -398,10 +398,10 @@ abstract class ActorContextSpec extends ActorTestKit with TypedAkkaSpecWithShutd "terminate upon not handling Terminated" in { val probe = TestProbe[Event]() - val child = (Behaviors.immutablePartial[Command] { + val child = (Behaviors.receivePartial[Command] { case (_, Stop) ⇒ Behaviors.stopped - } onSignal { + } receiveSignal { case (_, signal) ⇒ probe.ref ! GotChildSignal(signal) Behavior.same @@ -411,18 +411,18 @@ abstract class ActorContextSpec extends ActorTestKit with TypedAkkaSpecWithShutd val childRef = ctx.spawn(child, "A") ctx.watch(childRef) probe.ref ! ChildMade(childRef) - Behaviors.immutablePartial[Command] { + Behaviors.receivePartial[Command] { case (_, Inert) ⇒ probe.ref ! InertEvent - Behaviors.immutable[Command] { + Behaviors.receive[Command] { case (_, _) ⇒ Behaviors.unhandled - } onSignal { + } receiveSignal { case (_, Terminated(_)) ⇒ Behaviors.unhandled case (_, signal) ⇒ probe.ref ! GotSignal(signal) Behaviors.same } - } onSignal { + } receiveSignal { case (_, signal) ⇒ probe.ref ! GotSignal(signal) Behaviors.same @@ -441,7 +441,7 @@ abstract class ActorContextSpec extends ActorTestKit with TypedAkkaSpecWithShutd "return the right context info" in { type Info = (ActorSystem[Nothing], ActorRef[String]) val probe = TestProbe[Info] - val actor = spawn(Behaviors.immutablePartial[String] { + val actor = spawn(Behaviors.receivePartial[String] { case (ctx, "info") ⇒ probe.ref ! (ctx.system → ctx.self) Behaviors.same @@ -453,7 +453,7 @@ abstract class ActorContextSpec extends ActorTestKit with TypedAkkaSpecWithShutd "return right info about children" in { type Children = Seq[ActorRef[Nothing]] val probe = TestProbe[Children]() - val actor = spawn(Behaviors.immutablePartial[String] { + val actor = spawn(Behaviors.receivePartial[String] { case (ctx, "create") ⇒ ctx.spawn(Behaviors.empty, "B") probe.ref ! ctx.child("B").toSeq @@ -477,7 +477,7 @@ abstract class ActorContextSpec extends ActorTestKit with TypedAkkaSpecWithShutd "set small receive timeout" in { val probe = TestProbe[Event]() - val actor = spawn(Behaviors.immutablePartial[Command] { + val actor = spawn(Behaviors.receivePartial[Command] { case (_, ReceiveTimeout) ⇒ probe.ref ! GotReceiveTimeout Behaviors.same @@ -493,7 +493,7 @@ abstract class ActorContextSpec extends ActorTestKit with TypedAkkaSpecWithShutd "set large receive timeout" in { val probe = TestProbe[String]() - val actor = spawn(Behaviors.immutablePartial[String] { + val actor = spawn(Behaviors.receivePartial[String] { case (ctx, "schedule") ⇒ ctx.schedule(1.second, probe.ref, "scheduled") Behaviors.same @@ -518,7 +518,7 @@ abstract class ActorContextSpec extends ActorTestKit with TypedAkkaSpecWithShutd "schedule a message" in { val probe = TestProbe[Event]() - val actor = spawn(Behaviors.immutablePartial[Command] { + val actor = spawn(Behaviors.receivePartial[Command] { case (ctx, Ping) ⇒ ctx.schedule(1.nano, probe.ref, Pong) Behaviors.same @@ -531,7 +531,7 @@ abstract class ActorContextSpec extends ActorTestKit with TypedAkkaSpecWithShutd type Envelope = (ActorRef[String], String) val messages = TestProbe[Envelope]() val probe = TestProbe[ActorRef[String]]() - val actor = spawn(Behaviors.immutablePartial[String] { + val actor = spawn(Behaviors.receivePartial[String] { case (ctx, "message") ⇒ messages.ref.tell((ctx.self, "received message")) Behaviors.same @@ -560,7 +560,7 @@ abstract class ActorContextSpec extends ActorTestKit with TypedAkkaSpecWithShutd Behaviors.setup[Command](ctx ⇒ { val child = ctx.spawnAnonymous(Behaviors.empty[Command]) probe.ref ! ChildMade(child) - Behaviors.immutablePartial[Command] { + Behaviors.receivePartial[Command] { case (ctx, StopRef(ref)) ⇒ ctx.stop(ref) probe.ref ! Pong diff --git a/akka-actor-typed-tests/src/test/scala/akka/actor/typed/AskSpec.scala b/akka-actor-typed-tests/src/test/scala/akka/actor/typed/AskSpec.scala index 8d41c2cac3..eb3e6f27ad 100644 --- a/akka-actor-typed-tests/src/test/scala/akka/actor/typed/AskSpec.scala +++ b/akka-actor-typed-tests/src/test/scala/akka/actor/typed/AskSpec.scala @@ -35,7 +35,7 @@ class AskSpec extends ActorTestKit implicit def executor: ExecutionContext = system.executionContext - val behavior: Behavior[Msg] = immutable[Msg] { + val behavior: Behavior[Msg] = receive[Msg] { case (_, foo: Foo) ⇒ foo.replyTo ! "foo" Behaviors.same @@ -119,7 +119,7 @@ class AskSpec extends ActorTestKit val probe = TestProbe[AnyRef]("probe") val behv = - Behaviors.immutable[String] { + Behaviors.receive[String] { case (ctx, "start-ask") ⇒ ctx.ask[Question, Long](probe.ref)(Question(_)) { case Success(42L) ⇒ diff --git a/akka-actor-typed-tests/src/test/scala/akka/actor/typed/BehaviorSpec.scala b/akka-actor-typed-tests/src/test/scala/akka/actor/typed/BehaviorSpec.scala index 54488e7d66..3e4f87368e 100644 --- a/akka-actor-typed-tests/src/test/scala/akka/actor/typed/BehaviorSpec.scala +++ b/akka-actor-typed-tests/src/test/scala/akka/actor/typed/BehaviorSpec.scala @@ -5,6 +5,7 @@ package akka.actor.typed import akka.actor.typed.scaladsl.{ Behaviors ⇒ SBehaviors } +import akka.actor.typed.scaladsl.{ MutableBehavior ⇒ SMutableBehavior } import akka.actor.typed.javadsl.{ ActorContext ⇒ JActorContext, Behaviors ⇒ JBehaviors } import akka.japi.function.{ Function ⇒ F1e, Function2 ⇒ F2, Procedure2 ⇒ P2 } import akka.japi.pf.{ FI, PFBuilder } @@ -138,7 +139,7 @@ object BehaviorSpec { } def mkFull(monitor: ActorRef[Event], state: State = StateA): Behavior[Command] = { - SBehaviors.immutable[Command] { + SBehaviors.receive[Command] { case (ctx, GetSelf) ⇒ monitor ! Self(ctx.self) SBehaviors.same @@ -159,7 +160,7 @@ object BehaviorSpec { SBehaviors.same case (_, Stop) ⇒ SBehaviors.stopped case (_, _) ⇒ SBehaviors.unhandled - } onSignal { + } receiveSignal { case (_, signal) ⇒ monitor ! GotSignal(signal) SBehaviors.same @@ -343,10 +344,10 @@ class FullBehaviorSpec extends TypedAkkaSpec with Messages with BecomeWithLifecy override def behavior(monitor: ActorRef[Event]): (Behavior[Command], Aux) = mkFull(monitor) → null } -class ImmutableBehaviorSpec extends Messages with BecomeWithLifecycle with Stoppable { +class ReceiveBehaviorSpec extends Messages with BecomeWithLifecycle with Stoppable { override def behavior(monitor: ActorRef[Event]): (Behavior[Command], Aux) = behv(monitor, StateA) → null private def behv(monitor: ActorRef[Event], state: State): Behavior[Command] = { - SBehaviors.immutable[Command] { + SBehaviors.receive[Command] { case (ctx, GetSelf) ⇒ monitor ! Self(ctx.self) SBehaviors.same @@ -367,7 +368,7 @@ class ImmutableBehaviorSpec extends Messages with BecomeWithLifecycle with Stopp SBehaviors.same case (_, Stop) ⇒ SBehaviors.stopped case (_, _: AuxPing) ⇒ SBehaviors.unhandled - } onSignal { + } receiveSignal { case (_, signal) ⇒ monitor ! GotSignal(signal) SBehaviors.same @@ -380,7 +381,7 @@ class ImmutableWithSignalScalaBehaviorSpec extends TypedAkkaSpec with Messages w override def behavior(monitor: ActorRef[Event]): (Behavior[Command], Aux) = behv(monitor) → null def behv(monitor: ActorRef[Event], state: State = StateA): Behavior[Command] = - SBehaviors.immutable[Command] { + SBehaviors.receive[Command] { (ctx, msg) ⇒ msg match { case GetSelf ⇒ @@ -404,7 +405,7 @@ class ImmutableWithSignalScalaBehaviorSpec extends TypedAkkaSpec with Messages w case Stop ⇒ SBehaviors.stopped case _: AuxPing ⇒ SBehaviors.unhandled } - } onSignal { + } receiveSignal { case (_, sig) ⇒ monitor ! GotSignal(sig) SBehaviors.same @@ -416,7 +417,7 @@ class ImmutableScalaBehaviorSpec extends Messages with Become with Stoppable { override def behavior(monitor: ActorRef[Event]): (Behavior[Command], Aux) = behv(monitor, StateA) → null def behv(monitor: ActorRef[Event], state: State): Behavior[Command] = - SBehaviors.immutable[Command] { (ctx, msg) ⇒ + SBehaviors.receive[Command] { (ctx, msg) ⇒ msg match { case GetSelf ⇒ monitor ! Self(ctx.self) @@ -447,8 +448,8 @@ class MutableScalaBehaviorSpec extends Messages with Become with Stoppable { override def behavior(monitor: ActorRef[Event]): (Behavior[Command], Aux) = behv(monitor) → null def behv(monitor: ActorRef[Event]): Behavior[Command] = - SBehaviors.mutable[Command] { ctx ⇒ - new SBehaviors.MutableBehavior[Command] { + SBehaviors.setup[Command] { ctx ⇒ + new SMutableBehavior[Command] { private var state: State = StateA override def onMessage(msg: Command): Behavior[Command] = { @@ -519,7 +520,7 @@ class RestarterScalaBehaviorSpec extends ImmutableWithSignalScalaBehaviorSpec wi class ImmutableWithSignalJavaBehaviorSpec extends Messages with BecomeWithLifecycle with Stoppable { override def behavior(monitor: ActorRef[Event]): (Behavior[Command], Aux) = behv(monitor) → null def behv(monitor: ActorRef[Event], state: State = StateA): Behavior[Command] = - JBehaviors.immutable( + JBehaviors.receive( fc((ctx, msg) ⇒ msg match { case GetSelf ⇒ monitor ! Self(ctx.getSelf) @@ -551,7 +552,7 @@ class ImmutableWithSignalJavaBehaviorSpec extends Messages with BecomeWithLifecy class ImmutableJavaBehaviorSpec extends Messages with Become with Stoppable { override def behavior(monitor: ActorRef[Event]): (Behavior[Command], Aux) = behv(monitor, StateA) → null def behv(monitor: ActorRef[Event], state: State): Behavior[Command] = - JBehaviors.immutable { + JBehaviors.receive { fc((ctx, msg) ⇒ msg match { case GetSelf ⇒ diff --git a/akka-actor-typed-tests/src/test/scala/akka/actor/typed/DeferredSpec.scala b/akka-actor-typed-tests/src/test/scala/akka/actor/typed/DeferredSpec.scala index 55a6423c9b..8b76b8f63c 100644 --- a/akka-actor-typed-tests/src/test/scala/akka/actor/typed/DeferredSpec.scala +++ b/akka-actor-typed-tests/src/test/scala/akka/actor/typed/DeferredSpec.scala @@ -19,7 +19,7 @@ object DeferredSpec { case object Started extends Event def target(monitor: ActorRef[Event]): Behavior[Command] = - Behaviors.immutable((_, cmd) ⇒ cmd match { + Behaviors.receive((_, cmd) ⇒ cmd match { case Ping ⇒ monitor ! Pong Behaviors.same @@ -52,7 +52,7 @@ class DeferredSpec extends ActorTestKit with TypedAkkaSpecWithShutdown { throw new RuntimeException("simulated exc from factory") with NoStackTrace }) ctx.watch(child) - Behaviors.immutable[Command]((_, _) ⇒ Behaviors.same).onSignal { + Behaviors.receive[Command]((_, _) ⇒ Behaviors.same).receiveSignal { case (_, Terminated(`child`)) ⇒ probe.ref ! Pong Behaviors.stopped @@ -68,7 +68,7 @@ class DeferredSpec extends ActorTestKit with TypedAkkaSpecWithShutdown { val behv = Behaviors.setup[Command] { ctx ⇒ val child = ctx.spawnAnonymous(Behaviors.setup[Command](_ ⇒ Behaviors.stopped)) ctx.watch(child) - Behaviors.immutable[Command]((_, _) ⇒ Behaviors.same).onSignal { + Behaviors.receive[Command]((_, _) ⇒ Behaviors.same).receiveSignal { case (_, Terminated(`child`)) ⇒ probe.ref ! Pong Behaviors.stopped diff --git a/akka-actor-typed-tests/src/test/scala/akka/actor/typed/SupervisionSpec.scala b/akka-actor-typed-tests/src/test/scala/akka/actor/typed/SupervisionSpec.scala index ab349e1ed6..c53ea36df2 100644 --- a/akka-actor-typed-tests/src/test/scala/akka/actor/typed/SupervisionSpec.scala +++ b/akka-actor-typed-tests/src/test/scala/akka/actor/typed/SupervisionSpec.scala @@ -8,7 +8,7 @@ import java.io.IOException import java.util.concurrent.atomic.AtomicInteger import akka.actor.ActorInitializationException -import akka.actor.typed.scaladsl.Behaviors +import akka.actor.typed.scaladsl.{ Behaviors, MutableBehavior } import akka.actor.typed.scaladsl.Behaviors._ import akka.testkit.EventFilter import akka.testkit.typed.scaladsl._ @@ -40,7 +40,7 @@ object SupervisionSpec { class Exc3(msg: String = "exc-3") extends RuntimeException(msg) with NoStackTrace def targetBehavior(monitor: ActorRef[Event], state: State = State(0, Map.empty)): Behavior[Command] = - immutable[Command] { (ctx, cmd) ⇒ + receive[Command] { (ctx, cmd) ⇒ cmd match { case Ping ⇒ monitor ! Pong @@ -57,7 +57,7 @@ object SupervisionSpec { case Throw(e) ⇒ throw e } - } onSignal { + } receiveSignal { case (_, sig) ⇒ monitor ! GotSignal(sig) Behaviors.same @@ -623,7 +623,7 @@ class SupervisionSpec extends ActorTestKit with TypedAkkaSpecWithShutdown { "fail when exception from MutableBehavior constructor" in new FailingConstructorTestSetup(failCount = 1) { val probe = TestProbe[Event]("evt") - val behv = supervise(mutable[Command](_ ⇒ new FailingConstructor(probe.ref))) + val behv = supervise(setup[Command](_ ⇒ new FailingConstructor(probe.ref))) .onFailure[Exception](SupervisorStrategy.restart) EventFilter[ActorInitializationException](occurrences = 1).intercept { diff --git a/akka-actor-typed-tests/src/test/scala/akka/actor/typed/TimerSpec.scala b/akka-actor-typed-tests/src/test/scala/akka/actor/typed/TimerSpec.scala index a33d687b9d..dfae427540 100644 --- a/akka-actor-typed-tests/src/test/scala/akka/actor/typed/TimerSpec.scala +++ b/akka-actor-typed-tests/src/test/scala/akka/actor/typed/TimerSpec.scala @@ -45,7 +45,7 @@ class TimerSpec extends ActorTestKit with WordSpecLike with TypedAkkaSpecWithShu target(monitor, timer, nextCount) } - Behaviors.immutable[Command] { (ctx, cmd) ⇒ + Behaviors.receive[Command] { (ctx, cmd) ⇒ cmd match { case Tick(n) ⇒ monitor ! Tock(n) @@ -67,7 +67,7 @@ class TimerSpec extends ActorTestKit with WordSpecLike with TypedAkkaSpecWithShu latch.await(10, TimeUnit.SECONDS) throw e } - } onSignal { + } receiveSignal { case (ctx, PreRestart) ⇒ monitor ! GotPreRestart(timer.isTimerActive("T")) Behaviors.same diff --git a/akka-actor-typed-tests/src/test/scala/akka/actor/typed/WatchSpec.scala b/akka-actor-typed-tests/src/test/scala/akka/actor/typed/WatchSpec.scala index 0f3a48082a..615d8bc24f 100644 --- a/akka-actor-typed-tests/src/test/scala/akka/actor/typed/WatchSpec.scala +++ b/akka-actor-typed-tests/src/test/scala/akka/actor/typed/WatchSpec.scala @@ -6,7 +6,7 @@ package akka.actor.typed import akka.Done import akka.actor.typed.scaladsl.Behaviors -import akka.actor.typed.scaladsl.Behaviors.MutableBehavior +import akka.actor.typed.scaladsl.MutableBehavior import akka.actor.typed.scaladsl.adapter._ import akka.testkit.EventFilter import akka.testkit.typed.scaladsl.{ ActorTestKit, TestProbe } @@ -21,7 +21,7 @@ object WatchSpec { case object Stop val terminatorBehavior = - Behaviors.immutable[Stop.type] { + Behaviors.receive[Stop.type] { case (_, Stop) ⇒ Behaviors.stopped } @@ -54,12 +54,12 @@ class WatchSpec extends ActorTestKit val watcher = systemActor( Behaviors.supervise( - Behaviors.immutable[StartWatching] { + Behaviors.receive[StartWatching] { case (ctx, StartWatching(watchee)) ⇒ ctx.watch(watchee) watchProbe.ref ! Done Behaviors.same - }.onSignal { + }.receiveSignal { case (_, t: Terminated) ⇒ receivedTerminationSignal.success(t) Behaviors.stopped @@ -83,15 +83,15 @@ class WatchSpec extends ActorTestKit val probe = TestProbe[Any]() val ex = new TestException("boom") val parent = spawn(Behaviors.setup[Any] { ctx ⇒ - val child = ctx.spawn(Behaviors.immutable[Any]((ctx, msg) ⇒ + val child = ctx.spawn(Behaviors.receive[Any]((ctx, msg) ⇒ throw ex ), "child") ctx.watch(child) - Behaviors.immutable[Any] { (ctx, msg) ⇒ + Behaviors.receive[Any] { (ctx, msg) ⇒ child ! msg Behaviors.same - }.onSignal { + }.receiveSignal { case (_, t: Terminated) ⇒ probe.ref ! Failed(t) Behaviors.same @@ -110,12 +110,12 @@ class WatchSpec extends ActorTestKit val ex = new TestException("boom") val grossoBosso = spawn(Behaviors.setup[Any] { ctx ⇒ val middleManagement = ctx.spawn(Behaviors.setup[Any] { ctx ⇒ - val sixPackJoe = ctx.spawn(Behaviors.immutable[Any]((ctx, msg) ⇒ + val sixPackJoe = ctx.spawn(Behaviors.receive[Any]((ctx, msg) ⇒ throw ex ), "joe") ctx.watch(sixPackJoe) - Behaviors.immutable[Any] { (ctx, msg) ⇒ + Behaviors.receive[Any] { (ctx, msg) ⇒ sixPackJoe ! msg Behaviors.same } // no handling of terminated, even though we watched!!! @@ -123,10 +123,10 @@ class WatchSpec extends ActorTestKit ctx.watch(middleManagement) - Behaviors.immutable[Any] { (ctx, msg) ⇒ + Behaviors.receive[Any] { (ctx, msg) ⇒ middleManagement ! msg Behaviors.same - }.onSignal { + }.receiveSignal { case (_, t: Terminated) ⇒ probe.ref ! Failed(t) Behaviors.stopped @@ -161,7 +161,7 @@ class WatchSpec extends ActorTestKit val watcher = systemActor( Behaviors.supervise( - Behaviors.immutable[Message] { + Behaviors.receive[Message] { case (ctx, StartWatchingWith(watchee, msg)) ⇒ ctx.watchWith(watchee, msg) watchProbe.ref ! Done @@ -197,7 +197,7 @@ class WatchSpec extends ActorTestKit val watcher = systemActor( Behaviors.supervise( - Behaviors.immutable[Message] { + Behaviors.receive[Message] { case (ctx, StartWatching(watchee)) ⇒ ctx.watch(watchee) Behaviors.same @@ -227,7 +227,7 @@ class WatchSpec extends ActorTestKit val watcher = systemActor( Behaviors.supervise( - Behaviors.immutable[Message] { + Behaviors.receive[Message] { case (ctx, StartWatchingWith(watchee, msg)) ⇒ ctx.unwatch(watchee) ctx.watchWith(watchee, msg) @@ -253,7 +253,7 @@ class WatchSpec extends ActorTestKit val watcher = systemActor( Behaviors.supervise( - Behaviors.immutable[Message] { + Behaviors.receive[Message] { case (ctx, StartWatchingWith(watchee, msg)) ⇒ ctx.watchWith(watchee, msg) Behaviors.same @@ -262,7 +262,7 @@ class WatchSpec extends ActorTestKit Behaviors.same case (_, msg) ⇒ Behaviors.stopped - }.onSignal { + }.receiveSignal { case (_, PostStop) ⇒ Behaviors.stopped } diff --git a/akka-actor-typed-tests/src/test/scala/akka/actor/typed/internal/ActorSystemSpec.scala b/akka-actor-typed-tests/src/test/scala/akka/actor/typed/internal/ActorSystemSpec.scala index d2594243a5..2a8e5a77a4 100644 --- a/akka-actor-typed-tests/src/test/scala/akka/actor/typed/internal/ActorSystemSpec.scala +++ b/akka-actor-typed-tests/src/test/scala/akka/actor/typed/internal/ActorSystemSpec.scala @@ -41,7 +41,7 @@ class ActorSystemSpec extends WordSpec with Matchers with BeforeAndAfterAll "start the guardian actor and terminate when it terminates" in { val t = withSystem( "a", - Behaviors.immutable[Probe] { case (_, p) ⇒ p.replyTo ! p.msg; Behaviors.stopped }, doTerminate = false) { sys ⇒ + Behaviors.receive[Probe] { case (_, p) ⇒ p.replyTo ! p.msg; Behaviors.stopped }, doTerminate = false) { sys ⇒ val inbox = TestInbox[String]("a") sys ! Probe("hello", inbox.ref) eventually { @@ -57,7 +57,7 @@ class ActorSystemSpec extends WordSpec with Matchers with BeforeAndAfterAll // see issue #24172 "shutdown if guardian shuts down immediately" in { val stoppable = - Behaviors.immutable[Done] { + Behaviors.receive[Done] { case (ctx, Done) ⇒ Behaviors.stopped } withSystem("shutdown", stoppable, doTerminate = false) { sys: ActorSystem[Done] ⇒ @@ -69,9 +69,9 @@ class ActorSystemSpec extends WordSpec with Matchers with BeforeAndAfterAll "terminate the guardian actor" in { val inbox = TestInbox[String]("terminate") val sys = system( - Behaviors.immutable[Probe] { + Behaviors.receive[Probe] { case (_, _) ⇒ Behaviors.unhandled - } onSignal { + } receiveSignal { case (_, PostStop) ⇒ inbox.ref ! "done" Behaviors.same diff --git a/akka-actor-typed-tests/src/test/scala/akka/actor/typed/internal/receptionist/LocalReceptionistSpec.scala b/akka-actor-typed-tests/src/test/scala/akka/actor/typed/internal/receptionist/LocalReceptionistSpec.scala index 9c29fec995..8ed04bd7e0 100644 --- a/akka-actor-typed-tests/src/test/scala/akka/actor/typed/internal/receptionist/LocalReceptionistSpec.scala +++ b/akka-actor-typed-tests/src/test/scala/akka/actor/typed/internal/receptionist/LocalReceptionistSpec.scala @@ -26,7 +26,7 @@ class LocalReceptionistSpec extends ActorTestKit with TypedAkkaSpecWithShutdown val behaviorB = Behaviors.empty[ServiceB] case object Stop extends ServiceA with ServiceB - val stoppableBehavior = Behaviors.immutable[Any] { (_, msg) ⇒ + val stoppableBehavior = Behaviors.receive[Any] { (_, msg) ⇒ msg match { case Stop ⇒ Behavior.stopped case _ ⇒ Behavior.same diff --git a/akka-actor-typed-tests/src/test/scala/akka/actor/typed/receptionist/ReceptionistApiSpec.scala b/akka-actor-typed-tests/src/test/scala/akka/actor/typed/receptionist/ReceptionistApiSpec.scala index d194ba73d8..98d0c4a21f 100644 --- a/akka-actor-typed-tests/src/test/scala/akka/actor/typed/receptionist/ReceptionistApiSpec.scala +++ b/akka-actor-typed-tests/src/test/scala/akka/actor/typed/receptionist/ReceptionistApiSpec.scala @@ -67,7 +67,7 @@ object ReceptionistApiSpec { // to cover as much of the API as possible ctx.system.receptionist ! Receptionist.Register(key, ctx.self.narrow, ctx.self.narrow) - Behaviors.immutable { (ctx, msg) ⇒ + Behaviors.receive { (ctx, msg) ⇒ msg match { case key.Listing(services) ⇒ services.foreach(_ ! "woho") diff --git a/akka-actor-typed-tests/src/test/scala/akka/actor/typed/scaladsl/ActorContextAskSpec.scala b/akka-actor-typed-tests/src/test/scala/akka/actor/typed/scaladsl/ActorContextAskSpec.scala index 8720e056c8..5cf800fe81 100644 --- a/akka-actor-typed-tests/src/test/scala/akka/actor/typed/scaladsl/ActorContextAskSpec.scala +++ b/akka-actor-typed-tests/src/test/scala/akka/actor/typed/scaladsl/ActorContextAskSpec.scala @@ -42,7 +42,7 @@ class ActorContextAskSpec extends ActorTestKit with TypedAkkaSpecWithShutdown { case class Ping(sender: ActorRef[Pong]) case class Pong(selfName: String, threadName: String) - val pingPong = spawn(Behaviors.immutable[Ping] { (ctx, msg) ⇒ + val pingPong = spawn(Behaviors.receive[Ping] { (ctx, msg) ⇒ msg.sender ! Pong(ctx.self.path.name, Thread.currentThread().getName) Behaviors.same }, "ping-pong", Props.empty.withDispatcherFromConfig("ping-pong-dispatcher")) @@ -58,7 +58,7 @@ class ActorContextAskSpec extends ActorTestKit with TypedAkkaSpecWithShutdown { case Failure(ex) ⇒ throw ex } - Behaviors.immutable { + Behaviors.receive { case (ctx, pong: Pong) ⇒ probe.ref ! pong Behaviors.same @@ -80,7 +80,7 @@ class ActorContextAskSpec extends ActorTestKit with TypedAkkaSpecWithShutdown { case class Ping(respondTo: ActorRef[Pong.type]) extends Protocol case object Pong extends Protocol - val pingPong = spawn(Behaviors.immutable[Protocol]((_, msg) ⇒ + val pingPong = spawn(Behaviors.receive[Protocol]((_, msg) ⇒ msg match { case Ping(respondTo) ⇒ respondTo ! Pong @@ -94,11 +94,11 @@ class ActorContextAskSpec extends ActorTestKit with TypedAkkaSpecWithShutdown { case Failure(x) ⇒ x } - Behaviors.immutable[AnyRef] { + Behaviors.receive[AnyRef] { case (_, msg) ⇒ probe.ref ! msg Behaviors.same - }.onSignal { + }.receiveSignal { case (_, PostStop) ⇒ probe.ref ! "stopped" @@ -123,7 +123,7 @@ class ActorContextAskSpec extends ActorTestKit with TypedAkkaSpecWithShutdown { case Failure(x) ⇒ x }(20.millis, implicitly[ClassTag[String]]) - Behaviors.immutable { + Behaviors.receive { case (_, msg) ⇒ probe.ref ! msg Behaviors.same diff --git a/akka-actor-typed-tests/src/test/scala/akka/actor/typed/scaladsl/ActorLoggingSpec.scala b/akka-actor-typed-tests/src/test/scala/akka/actor/typed/scaladsl/ActorLoggingSpec.scala index 5b3667369a..e3fe56c623 100644 --- a/akka-actor-typed-tests/src/test/scala/akka/actor/typed/scaladsl/ActorLoggingSpec.scala +++ b/akka-actor-typed-tests/src/test/scala/akka/actor/typed/scaladsl/ActorLoggingSpec.scala @@ -34,7 +34,7 @@ class ActorLoggingSpec extends ActorTestKit with TypedAkkaSpec { spawn(Behaviors.setup[String] { ctx ⇒ ctx.log.info("Started") - Behaviors.immutable { (ctx, msg) ⇒ + Behaviors.receive { (ctx, msg) ⇒ ctx.log.info("got message {}", msg) Behaviors.same } @@ -204,7 +204,7 @@ class ActorLoggingSpec extends ActorTestKit with TypedAkkaSpec { }, Behaviors.setup { ctx ⇒ ctx.log.info("Starting") - Behaviors.immutable { (ctx, msg) ⇒ + Behaviors.receive { (ctx, msg) ⇒ ctx.log.info("Got message!") Behaviors.same } diff --git a/akka-actor-typed-tests/src/test/scala/akka/actor/typed/scaladsl/GracefulStopSpec.scala b/akka-actor-typed-tests/src/test/scala/akka/actor/typed/scaladsl/GracefulStopSpec.scala index 5501b7d0a3..0bccc496ed 100644 --- a/akka-actor-typed-tests/src/test/scala/akka/actor/typed/scaladsl/GracefulStopSpec.scala +++ b/akka-actor-typed-tests/src/test/scala/akka/actor/typed/scaladsl/GracefulStopSpec.scala @@ -18,20 +18,20 @@ final class GracefulStopSpec extends ActorTestKit with TypedAkkaSpecWithShutdown val behavior = Behaviors.setup[akka.NotUsed] { context ⇒ - val c1 = context.spawn[NotUsed](Behaviors.onSignal { + val c1 = context.spawn[NotUsed](Behaviors.receiveSignal { case (_, PostStop) ⇒ probe.ref ! "child-done" Behaviors.stopped }, "child1") - val c2 = context.spawn[NotUsed](Behaviors.onSignal { + val c2 = context.spawn[NotUsed](Behaviors.receiveSignal { case (_, PostStop) ⇒ probe.ref ! "child-done" Behaviors.stopped }, "child2") Behaviors.stopped { - Behaviors.onSignal { + Behaviors.receiveSignal { case (ctx, PostStop) ⇒ // cleanup function body probe.ref ! "parent-done" @@ -53,7 +53,7 @@ final class GracefulStopSpec extends ActorTestKit with TypedAkkaSpecWithShutdown Behaviors.setup[akka.NotUsed] { context ⇒ // do not spawn any children Behaviors.stopped { - Behaviors.onSignal { + Behaviors.receiveSignal { case (ctx, PostStop) ⇒ // cleanup function body probe.ref ! Done diff --git a/akka-actor-typed-tests/src/test/scala/akka/actor/typed/scaladsl/MessageAdapterSpec.scala b/akka-actor-typed-tests/src/test/scala/akka/actor/typed/scaladsl/MessageAdapterSpec.scala index 0362c4bb5f..76861a4ae0 100644 --- a/akka-actor-typed-tests/src/test/scala/akka/actor/typed/scaladsl/MessageAdapterSpec.scala +++ b/akka-actor-typed-tests/src/test/scala/akka/actor/typed/scaladsl/MessageAdapterSpec.scala @@ -49,7 +49,7 @@ class MessageAdapterSpec extends ActorTestKit with TypedAkkaSpecWithShutdown { case class AnotherPong(selfName: String, threadName: String) - val pingPong = spawn(Behaviors.immutable[Ping] { (ctx, msg) ⇒ + val pingPong = spawn(Behaviors.receive[Ping] { (ctx, msg) ⇒ msg.sender ! Pong(ctx.self.path.name, Thread.currentThread().getName) Behaviors.same }, "ping-pong", Props.empty.withDispatcherFromConfig("ping-pong-dispatcher")) @@ -67,7 +67,7 @@ class MessageAdapterSpec extends ActorTestKit with TypedAkkaSpecWithShutdown { AnotherPong(ctx.self.path.name, Thread.currentThread().getName)) pingPong ! Ping(replyTo2) - Behaviors.immutable { + Behaviors.receive { case (_, anotherPong: AnotherPong) ⇒ probe.ref ! anotherPong Behaviors.same @@ -96,7 +96,7 @@ class MessageAdapterSpec extends ActorTestKit with TypedAkkaSpecWithShutdown { case class Wrapped(qualifier: String, response: Response) - val pingPong = spawn(Behaviors.immutable[Ping] { (_, msg) ⇒ + val pingPong = spawn(Behaviors.receive[Ping] { (_, msg) ⇒ msg match { case Ping1(sender) ⇒ sender ! Pong1("hello-1") @@ -117,7 +117,7 @@ class MessageAdapterSpec extends ActorTestKit with TypedAkkaSpecWithShutdown { pingPong ! Ping1(replyTo1) pingPong ! Ping2(replyTo2) - Behaviors.immutable { + Behaviors.receive { case (_, wrapped) ⇒ probe.ref ! wrapped Behaviors.same @@ -140,7 +140,7 @@ class MessageAdapterSpec extends ActorTestKit with TypedAkkaSpecWithShutdown { case class Wrapped(qualifier: String, response: Response) - val pingPong = spawn(Behaviors.immutable[Ping] { (_, msg) ⇒ + val pingPong = spawn(Behaviors.receive[Ping] { (_, msg) ⇒ msg match { case Ping1(sender) ⇒ sender ! Pong1("hello-1") @@ -163,7 +163,7 @@ class MessageAdapterSpec extends ActorTestKit with TypedAkkaSpecWithShutdown { pingPong ! Ping2(replyTo1.asInstanceOf[ActorRef[Pong2]]) pingPong ! Ping1(replyTo1) - Behaviors.immutable { + Behaviors.receive { case (_, wrapped) ⇒ probe.ref ! wrapped Behaviors.same @@ -184,7 +184,7 @@ class MessageAdapterSpec extends ActorTestKit with TypedAkkaSpecWithShutdown { case class Pong(greeting: String) case class Wrapped(count: Int, response: Pong) - val pingPong = spawn(Behaviors.immutable[Ping] { (_, ping) ⇒ + val pingPong = spawn(Behaviors.receive[Ping] { (_, ping) ⇒ ping.sender ! Pong("hello") Behaviors.same }) @@ -203,11 +203,11 @@ class MessageAdapterSpec extends ActorTestKit with TypedAkkaSpecWithShutdown { pingPong ! Ping(replyTo) } - Behaviors.immutable[Wrapped] { + Behaviors.receive[Wrapped] { case (_, wrapped) ⇒ probe.ref ! wrapped Behaviors.same - }.onSignal { + }.receiveSignal { case (_, PostStop) ⇒ probe.ref ! "stopped" Behaviors.same diff --git a/akka-actor-typed-tests/src/test/scala/akka/actor/typed/scaladsl/OnSignalSpec.scala b/akka-actor-typed-tests/src/test/scala/akka/actor/typed/scaladsl/OnSignalSpec.scala index ae5c44e699..9c7e465e24 100644 --- a/akka-actor-typed-tests/src/test/scala/akka/actor/typed/scaladsl/OnSignalSpec.scala +++ b/akka-actor-typed-tests/src/test/scala/akka/actor/typed/scaladsl/OnSignalSpec.scala @@ -17,7 +17,7 @@ final class OnSignalSpec extends ActorTestKit with TypedAkkaSpecWithShutdown { Behaviors.setup[Nothing] { context ⇒ val stoppedChild = context.spawn(Behaviors.stopped, "stopped-child") context.watch(stoppedChild) - Behaviors.onSignal[Nothing] { + Behaviors.receiveSignal[Nothing] { case (_, Terminated(`stoppedChild`)) ⇒ probe.ref ! Done Behaviors.stopped diff --git a/akka-actor-typed-tests/src/test/scala/akka/actor/typed/scaladsl/ImmutablePartialSpec.scala b/akka-actor-typed-tests/src/test/scala/akka/actor/typed/scaladsl/ReceivePartialSpec.scala similarity index 53% rename from akka-actor-typed-tests/src/test/scala/akka/actor/typed/scaladsl/ImmutablePartialSpec.scala rename to akka-actor-typed-tests/src/test/scala/akka/actor/typed/scaladsl/ReceivePartialSpec.scala index 1097be58c8..cdc7e90d5c 100644 --- a/akka-actor-typed-tests/src/test/scala/akka/actor/typed/scaladsl/ImmutablePartialSpec.scala +++ b/akka-actor-typed-tests/src/test/scala/akka/actor/typed/scaladsl/ReceivePartialSpec.scala @@ -7,14 +7,31 @@ package scaladsl import akka.testkit.typed.scaladsl.{ ActorTestKit, TestProbe } -class ImmutablePartialSpec extends ActorTestKit with TypedAkkaSpecWithShutdown { +class ReceivePartialSpec extends ActorTestKit with TypedAkkaSpecWithShutdown { "An immutable partial" must { - "correctly install the message handler" in { + "correctly install the receiveMessage handler" in { val probe = TestProbe[Command]("probe") val behavior = - Behaviors.immutablePartial[Command] { + Behaviors.receiveMessagePartial[Command] { + case Command2 ⇒ + probe.ref ! Command2 + Behaviors.same + } + val actor = spawn(behavior) + + actor ! Command1 + probe.expectNoMessage() + + actor ! Command2 + probe.expectMessage(Command2) + } + + "correctly install the receive handler" in { + val probe = TestProbe[Command]("probe") + val behavior = + Behaviors.receivePartial[Command] { case (_, Command2) ⇒ probe.ref ! Command2 Behaviors.same diff --git a/akka-actor-typed-tests/src/test/scala/akka/actor/typed/scaladsl/StashBufferSpec.scala b/akka-actor-typed-tests/src/test/scala/akka/actor/typed/scaladsl/StashBufferSpec.scala index abdc3d0dbc..da42d76416 100644 --- a/akka-actor-typed-tests/src/test/scala/akka/actor/typed/scaladsl/StashBufferSpec.scala +++ b/akka-actor-typed-tests/src/test/scala/akka/actor/typed/scaladsl/StashBufferSpec.scala @@ -85,7 +85,7 @@ class StashBufferSpec extends WordSpec with Matchers { val valueInbox = TestInbox[String]() def behavior(state: String): Behavior[String] = - Behaviors.immutable[String] { (_, msg) ⇒ + Behaviors.receive[String] { (_, msg) ⇒ if (msg == "get") { valueInbox.ref ! state Behaviors.same @@ -108,7 +108,7 @@ class StashBufferSpec extends WordSpec with Matchers { val valueInbox = TestInbox[String]() def behavior(state: String): Behavior[String] = - Behaviors.immutable[String] { (_, msg) ⇒ + Behaviors.receive[String] { (_, msg) ⇒ if (msg == "get") { valueInbox.ref ! state Behaviors.same @@ -131,7 +131,7 @@ class StashBufferSpec extends WordSpec with Matchers { val valueInbox = TestInbox[String]() def behavior(state: String): Behavior[String] = - Behaviors.immutable[String] { (_, msg) ⇒ + Behaviors.receive[String] { (_, msg) ⇒ if (msg == "get") { valueInbox.ref ! state Behaviors.same diff --git a/akka-actor-typed-tests/src/test/scala/akka/actor/typed/scaladsl/StashSpec.scala b/akka-actor-typed-tests/src/test/scala/akka/actor/typed/scaladsl/StashSpec.scala index 626567a9c6..1f0b0b054e 100644 --- a/akka-actor-typed-tests/src/test/scala/akka/actor/typed/scaladsl/StashSpec.scala +++ b/akka-actor-typed-tests/src/test/scala/akka/actor/typed/scaladsl/StashSpec.scala @@ -23,7 +23,7 @@ object StashSpec { val buffer = StashBuffer[Command](capacity = 10) def active(processed: Vector[String]): Behavior[Command] = - Behaviors.immutable { (ctx, cmd) ⇒ + Behaviors.receive { (ctx, cmd) ⇒ cmd match { case msg: Msg ⇒ active(processed :+ msg.s) @@ -45,7 +45,7 @@ object StashSpec { } def stashing(processed: Vector[String]): Behavior[Command] = - Behaviors.immutable { (ctx, cmd) ⇒ + Behaviors.receive { (ctx, cmd) ⇒ cmd match { case msg: Msg ⇒ buffer.stash(msg) @@ -76,7 +76,7 @@ object StashSpec { } def unstashing(processed: Vector[String]): Behavior[Command] = - Behaviors.immutable { (ctx, cmd) ⇒ + Behaviors.receive { (ctx, cmd) ⇒ cmd match { case Unstashed(msg: Msg) ⇒ ctx.log.debug(s"unstashed $msg") @@ -118,7 +118,7 @@ object StashSpec { active(Vector.empty) } - class MutableStash(ctx: ActorContext[Command]) extends Behaviors.MutableBehavior[Command] { + class MutableStash(ctx: ActorContext[Command]) extends MutableBehavior[Command] { private val buffer = StashBuffer.apply[Command](capacity = 10) private var stashing = false @@ -183,7 +183,7 @@ class ImmutableStashSpec extends StashSpec { class MutableStashSpec extends StashSpec { import StashSpec._ def testQualifier: String = "mutable behavior" - def behaviorUnderTest: Behavior[Command] = Behaviors.mutable(ctx ⇒ new MutableStash(ctx)) + def behaviorUnderTest: Behavior[Command] = Behaviors.setup(ctx ⇒ new MutableStash(ctx)) } abstract class StashSpec extends ActorTestKit with TypedAkkaSpecWithShutdown { diff --git a/akka-actor-typed-tests/src/test/scala/akka/actor/typed/scaladsl/adapter/AdapterSpec.scala b/akka-actor-typed-tests/src/test/scala/akka/actor/typed/scaladsl/adapter/AdapterSpec.scala index 325f11299c..e55544d2b2 100644 --- a/akka-actor-typed-tests/src/test/scala/akka/actor/typed/scaladsl/adapter/AdapterSpec.scala +++ b/akka-actor-typed-tests/src/test/scala/akka/actor/typed/scaladsl/adapter/AdapterSpec.scala @@ -34,7 +34,7 @@ object AdapterSpec { } def typed1(ref: untyped.ActorRef, probe: ActorRef[String]): Behavior[String] = - Behaviors.immutable[String] { + Behaviors.receive[String] { (ctx, msg) ⇒ msg match { case "send" ⇒ @@ -63,7 +63,7 @@ object AdapterSpec { ctx.stop(child) Behaviors.same } - } onSignal { + } receiveSignal { case (ctx, Terminated(ref)) ⇒ probe ! "terminated" Behaviors.same @@ -132,7 +132,7 @@ object AdapterSpec { } def typed2: Behavior[Typed2Msg] = - Behaviors.immutable { (ctx, msg) ⇒ + Behaviors.receive { (ctx, msg) ⇒ msg match { case Ping(replyTo) ⇒ replyTo ! "pong" @@ -172,7 +172,7 @@ class AdapterSpec extends AkkaSpec { for { _ ← 0 to 10 } { var system: akka.actor.typed.ActorSystem[Done] = null try { - system = ActorSystem.create(Behaviors.immutable[Done] { (ctx, msg) ⇒ + system = ActorSystem.create(Behaviors.receive[Done] { (ctx, msg) ⇒ ctx.self ! Done msg match { case Done ⇒ Behaviors.stopped diff --git a/akka-actor-typed-tests/src/test/scala/docs/akka/typed/FaultToleranceDocSpec.scala b/akka-actor-typed-tests/src/test/scala/docs/akka/typed/FaultToleranceDocSpec.scala index 1a386c8576..78900d7d45 100644 --- a/akka-actor-typed-tests/src/test/scala/docs/akka/typed/FaultToleranceDocSpec.scala +++ b/akka-actor-typed-tests/src/test/scala/docs/akka/typed/FaultToleranceDocSpec.scala @@ -27,7 +27,7 @@ class FaultToleranceDocSpec extends ActorTestKit with TypedAkkaSpecWithShutdown sealed trait Message case class Fail(text: String) extends Message - val worker = Behaviors.immutable[Message] { (ctx, msg) ⇒ + val worker = Behaviors.receive[Message] { (ctx, msg) ⇒ msg match { case Fail(text) ⇒ throw new RuntimeException(text) } @@ -41,7 +41,7 @@ class FaultToleranceDocSpec extends ActorTestKit with TypedAkkaSpecWithShutdown // here we don't handle Terminated at all which means that // when the child fails or stops gracefully this actor will // fail with a DeathWatchException - Behaviors.immutable[Message] { (ctx, msg) ⇒ + Behaviors.receive[Message] { (ctx, msg) ⇒ child ! msg Behaviors.same } @@ -55,7 +55,7 @@ class FaultToleranceDocSpec extends ActorTestKit with TypedAkkaSpecWithShutdown // here we don't handle Terminated at all which means that // when middle management fails with a DeathWatchException // this actor will also fail - Behaviors.immutable[Message] { (ctx, msg) ⇒ + Behaviors.receive[Message] { (ctx, msg) ⇒ middleManagment ! msg Behaviors.same } diff --git a/akka-actor-typed-tests/src/test/scala/docs/akka/typed/GracefulStopDocSpec.scala b/akka-actor-typed-tests/src/test/scala/docs/akka/typed/GracefulStopDocSpec.scala index b1ee1c91b4..c313fceec3 100644 --- a/akka-actor-typed-tests/src/test/scala/docs/akka/typed/GracefulStopDocSpec.scala +++ b/akka-actor-typed-tests/src/test/scala/docs/akka/typed/GracefulStopDocSpec.scala @@ -27,7 +27,7 @@ object GracefulStopDocSpec { // Predefined cleanup operation def cleanup(log: Logger): Unit = log.info("Cleaning up!") - val mcpa = Behaviors.immutable[JobControlLanguage] { (ctx, msg) ⇒ + val mcpa = Behaviors.receive[JobControlLanguage] { (ctx, msg) ⇒ msg match { case SpawnJob(jobName) ⇒ ctx.log.info("Spawning job {}!", jobName) @@ -38,14 +38,14 @@ object GracefulStopDocSpec { // perform graceful stop, executing cleanup before final system termination // behavior executing cleanup is passed as a parameter to Actor.stopped Behaviors.stopped { - Behaviors.onSignal { + Behaviors.receiveSignal { case (context, PostStop) ⇒ cleanup(context.system.log) Behaviors.same } } } - }.onSignal { + }.receiveSignal { case (ctx, PostStop) ⇒ ctx.log.info("MCPA stopped") Behaviors.same @@ -58,7 +58,7 @@ object GracefulStopDocSpec { object Job { import GracefulStopDocSpec.MasterControlProgramActor.JobControlLanguage - def job(name: String) = Behaviors.onSignal[JobControlLanguage] { + def job(name: String) = Behaviors.receiveSignal[JobControlLanguage] { case (ctx, PostStop) ⇒ ctx.log.info("Worker {} stopped", name) Behaviors.same diff --git a/akka-actor-typed-tests/src/test/scala/docs/akka/typed/InteractionPatternsSpec.scala b/akka-actor-typed-tests/src/test/scala/docs/akka/typed/InteractionPatternsSpec.scala index 382f9ad213..6356c55509 100644 --- a/akka-actor-typed-tests/src/test/scala/docs/akka/typed/InteractionPatternsSpec.scala +++ b/akka-actor-typed-tests/src/test/scala/docs/akka/typed/InteractionPatternsSpec.scala @@ -24,7 +24,7 @@ class InteractionPatternsSpec extends ActorTestKit with TypedAkkaSpecWithShutdow // #fire-and-forget-definition case class PrintMe(message: String) - val printerBehavior: Behavior[PrintMe] = Behaviors.immutable { + val printerBehavior: Behavior[PrintMe] = Behaviors.receive { case (ctx, PrintMe(message)) ⇒ ctx.log.info(message) Behaviors.same @@ -53,7 +53,7 @@ class InteractionPatternsSpec extends ActorTestKit with TypedAkkaSpecWithShutdow // #request-response-protocol // #request-response-respond - val otherBehavior = Behaviors.immutable[Request] { (ctx, msg) ⇒ + val otherBehavior = Behaviors.receive[Request] { (ctx, msg) ⇒ msg match { case Request(query, respondTo) ⇒ // ... process query ... @@ -108,7 +108,7 @@ class InteractionPatternsSpec extends ActorTestKit with TypedAkkaSpecWithShutdow def active( inProgress: Map[Int, ActorRef[URI]], count: Int): Behavior[Command] = { - Behaviors.immutable[Command] { (_, msg) ⇒ + Behaviors.receive[Command] { (_, msg) ⇒ msg match { case Translate(site, replyTo) ⇒ val taskId = count + 1 @@ -136,7 +136,7 @@ class InteractionPatternsSpec extends ActorTestKit with TypedAkkaSpecWithShutdow } // #adapted-response - val backend = spawn(Behaviors.immutable[Backend.Request] { (_, msg) ⇒ + val backend = spawn(Behaviors.receive[Backend.Request] { (_, msg) ⇒ msg match { case Backend.StartTranslationJob(taskId, site, replyTo) ⇒ replyTo ! Backend.JobStarted(taskId) @@ -172,7 +172,7 @@ class InteractionPatternsSpec extends ActorTestKit with TypedAkkaSpecWithShutdow def idle(timers: TimerScheduler[Msg], target: ActorRef[Batch], after: FiniteDuration, maxSize: Int): Behavior[Msg] = { - Behaviors.immutable[Msg] { (ctx, msg) ⇒ + Behaviors.receive[Msg] { (ctx, msg) ⇒ timers.startSingleTimer(TimerKey, Timeout, after) active(Vector(msg), timers, target, after, maxSize) } @@ -180,7 +180,7 @@ class InteractionPatternsSpec extends ActorTestKit with TypedAkkaSpecWithShutdow def active(buffer: Vector[Msg], timers: TimerScheduler[Msg], target: ActorRef[Batch], after: FiniteDuration, maxSize: Int): Behavior[Msg] = { - Behaviors.immutable[Msg] { (_, msg) ⇒ + Behaviors.receive[Msg] { (_, msg) ⇒ msg match { case Timeout ⇒ target ! Batch(buffer) @@ -212,7 +212,7 @@ class InteractionPatternsSpec extends ActorTestKit with TypedAkkaSpecWithShutdow case class OpenThePodBayDoorsPlease(respondTo: ActorRef[HalResponse]) extends HalCommand case class HalResponse(message: String) - val halBehavior = Behaviors.immutable[HalCommand] { (ctx, msg) ⇒ + val halBehavior = Behaviors.receive[HalCommand] { (ctx, msg) ⇒ msg match { case OpenThePodBayDoorsPlease(respondTo) ⇒ respondTo ! HalResponse("I'm sorry, Dave. I'm afraid I can't do that.") @@ -249,7 +249,7 @@ class InteractionPatternsSpec extends ActorTestKit with TypedAkkaSpecWithShutdow case Failure(ex) ⇒ AdaptedResponse(s"$requestId: Request failed") } - Behaviors.immutable { (ctx, msg) ⇒ + Behaviors.receive { (ctx, msg) ⇒ msg match { // the adapted message ends up being processed like any other // message sent to the actor @@ -277,14 +277,14 @@ class InteractionPatternsSpec extends ActorTestKit with TypedAkkaSpecWithShutdow // #per-session-child - val keyCabinetBehavior: Behavior[GetKeys] = Behaviors.immutable { (ctx, msg) ⇒ + val keyCabinetBehavior: Behavior[GetKeys] = Behaviors.receive { (ctx, msg) ⇒ msg match { case GetKeys(_, respondTo) ⇒ respondTo ! Keys() Behaviors.same } } - val drawerBehavior: Behavior[GetWallet] = Behaviors.immutable { (ctx, msg) ⇒ + val drawerBehavior: Behavior[GetWallet] = Behaviors.receive { (ctx, msg) ⇒ msg match { case GetWallet(_, respondTo) ⇒ respondTo ! Wallet() @@ -301,7 +301,7 @@ class InteractionPatternsSpec extends ActorTestKit with TypedAkkaSpecWithShutdow case class GetKeys(whoseKeys: String, respondTo: ActorRef[Keys]) case class GetWallet(whoseWallet: String, respondTo: ActorRef[Wallet]) - def homeBehavior = Behaviors.immutable[HomeCommand] { (ctx, msg) ⇒ + def homeBehavior = Behaviors.receive[HomeCommand] { (ctx, msg) ⇒ val keyCabinet: ActorRef[GetKeys] = ctx.spawn(keyCabinetBehavior, "key-cabinet") val drawer: ActorRef[GetWallet] = ctx.spawn(drawerBehavior, "drawer") @@ -340,7 +340,7 @@ class InteractionPatternsSpec extends ActorTestKit with TypedAkkaSpecWithShutdow Behavior.same } - Behaviors.immutable((ctx, msg) ⇒ { + Behaviors.receive((ctx, msg) ⇒ { msg match { case w: Wallet ⇒ wallet = Some(w) @@ -370,7 +370,7 @@ class InteractionPatternsSpec extends ActorTestKit with TypedAkkaSpecWithShutdow // #standalone-ask // keep this out of the sample as it uses the testkit spawn - val cookieActorRef = spawn(Behaviors.immutable[GiveMeCookies] { (ctx, msg) ⇒ + val cookieActorRef = spawn(Behaviors.receive[GiveMeCookies] { (ctx, msg) ⇒ msg.replyTo ! Cookies(5) Behaviors.same }) diff --git a/akka-actor-typed-tests/src/test/scala/docs/akka/typed/IntroSpec.scala b/akka-actor-typed-tests/src/test/scala/docs/akka/typed/IntroSpec.scala index eab85384ce..5195e63709 100644 --- a/akka-actor-typed-tests/src/test/scala/docs/akka/typed/IntroSpec.scala +++ b/akka-actor-typed-tests/src/test/scala/docs/akka/typed/IntroSpec.scala @@ -27,7 +27,7 @@ object IntroSpec { final case class Greet(whom: String, replyTo: ActorRef[Greeted]) final case class Greeted(whom: String) - val greeter = Behaviors.immutable[Greet] { (_, msg) ⇒ + val greeter = Behaviors.receive[Greet] { (_, msg) ⇒ println(s"Hello ${msg.whom}!") msg.replyTo ! Greeted(msg.whom) Behaviors.same @@ -63,7 +63,7 @@ object IntroSpec { chatRoom(List.empty) private def chatRoom(sessions: List[ActorRef[SessionCommand]]): Behavior[RoomCommand] = - Behaviors.immutable[RoomCommand] { (ctx, msg) ⇒ + Behaviors.receive[RoomCommand] { (ctx, msg) ⇒ msg match { case GetSession(screenName, client) ⇒ // create a child actor for further interaction with the client @@ -83,7 +83,7 @@ object IntroSpec { room: ActorRef[PublishSessionMessage], screenName: String, client: ActorRef[SessionEvent]): Behavior[SessionCommand] = - Behaviors.immutable { (ctx, msg) ⇒ + Behaviors.receive { (ctx, msg) ⇒ msg match { case PostMessage(message) ⇒ // from client, publish to others via the room @@ -132,7 +132,7 @@ class IntroSpec extends ActorTestKit with TypedAkkaSpecWithShutdown { import ChatRoom._ val gabbler = - Behaviors.immutable[SessionEvent] { (_, msg) ⇒ + Behaviors.receive[SessionEvent] { (_, msg) ⇒ msg match { //#chatroom-gabbler // We document that the compiler warns about the missing handler for `SessionDenied` @@ -158,7 +158,7 @@ class IntroSpec extends ActorTestKit with TypedAkkaSpecWithShutdown { ctx.watch(gabblerRef) chatRoom ! GetSession("ol’ Gabbler", gabblerRef) - Behaviors.onSignal { + Behaviors.receiveSignal { case (_, Terminated(ref)) ⇒ Behaviors.stopped } diff --git a/akka-actor-typed-tests/src/test/scala/docs/akka/typed/MutableIntroSpec.scala b/akka-actor-typed-tests/src/test/scala/docs/akka/typed/MutableIntroSpec.scala index 394ebf6e88..835f3d0eae 100644 --- a/akka-actor-typed-tests/src/test/scala/docs/akka/typed/MutableIntroSpec.scala +++ b/akka-actor-typed-tests/src/test/scala/docs/akka/typed/MutableIntroSpec.scala @@ -9,8 +9,7 @@ import java.net.URLEncoder import java.nio.charset.StandardCharsets import akka.actor.typed._ -import akka.actor.typed.scaladsl.Behaviors -import akka.actor.typed.scaladsl.ActorContext +import akka.actor.typed.scaladsl.{ ActorContext, Behaviors, MutableBehavior } import akka.testkit.typed.scaladsl.ActorTestKit import scala.concurrent.duration._ @@ -44,9 +43,9 @@ object MutableIntroSpec { //#chatroom-behavior def behavior(): Behavior[RoomCommand] = - Behaviors.mutable[RoomCommand](ctx ⇒ new ChatRoomBehavior(ctx)) + Behaviors.setup[RoomCommand](ctx ⇒ new ChatRoomBehavior(ctx)) - class ChatRoomBehavior(ctx: ActorContext[RoomCommand]) extends Behaviors.MutableBehavior[RoomCommand] { + class ChatRoomBehavior(ctx: ActorContext[RoomCommand]) extends MutableBehavior[RoomCommand] { private var sessions: List[ActorRef[SessionCommand]] = List.empty override def onMessage(msg: RoomCommand): Behavior[RoomCommand] = { @@ -71,17 +70,15 @@ object MutableIntroSpec { room: ActorRef[PublishSessionMessage], screenName: String, client: ActorRef[SessionEvent]): Behavior[SessionCommand] = - Behaviors.immutable { (ctx, msg) ⇒ - msg match { - case PostMessage(message) ⇒ - // from client, publish to others via the room - room ! PublishSessionMessage(screenName, message) - Behaviors.same - case NotifyClient(message) ⇒ - // published from the room - client ! message - Behaviors.same - } + Behaviors.receiveMessage { + case PostMessage(message) ⇒ + // from client, publish to others via the room + room ! PublishSessionMessage(screenName, message) + Behaviors.same + case NotifyClient(message) ⇒ + // published from the room + client ! message + Behaviors.same } //#chatroom-behavior } @@ -99,18 +96,16 @@ class MutableIntroSpec extends ActorTestKit with TypedAkkaSpecWithShutdown { import ChatRoom._ val gabbler = - Behaviors.immutable[SessionEvent] { (_, msg) ⇒ - msg match { - case SessionDenied(reason) ⇒ - println(s"cannot start chat room session: $reason") - Behaviors.stopped - case SessionGranted(handle) ⇒ - handle ! PostMessage("Hello World!") - Behaviors.same - case MessagePosted(screenName, message) ⇒ - println(s"message has been posted by '$screenName': $message") - Behaviors.stopped - } + Behaviors.receiveMessage[SessionEvent] { + case SessionDenied(reason) ⇒ + println(s"cannot start chat room session: $reason") + Behaviors.stopped + case SessionGranted(handle) ⇒ + handle ! PostMessage("Hello World!") + Behaviors.same + case MessagePosted(screenName, message) ⇒ + println(s"message has been posted by '$screenName': $message") + Behaviors.stopped } //#chatroom-gabbler @@ -121,11 +116,11 @@ class MutableIntroSpec extends ActorTestKit with TypedAkkaSpecWithShutdown { val gabblerRef = ctx.spawn(gabbler, "gabbler") ctx.watch(gabblerRef) - Behaviors.immutablePartial[String] { - case (_, "go") ⇒ + Behaviors.receiveMessagePartial[String] { + case "go" ⇒ chatRoom ! GetSession("ol’ Gabbler", gabblerRef) Behaviors.same - } onSignal { + } receiveSignal { case (_, Terminated(_)) ⇒ println("Stopping guardian") Behaviors.stopped diff --git a/akka-actor-typed-tests/src/test/scala/docs/akka/typed/StashDocSpec.scala b/akka-actor-typed-tests/src/test/scala/docs/akka/typed/StashDocSpec.scala index 4f6aeba465..1294dd5219 100644 --- a/akka-actor-typed-tests/src/test/scala/docs/akka/typed/StashDocSpec.scala +++ b/akka-actor-typed-tests/src/test/scala/docs/akka/typed/StashDocSpec.scala @@ -38,7 +38,7 @@ object StashDocSpec { val buffer = StashBuffer[Command](capacity = 100) def init(): Behavior[Command] = - Behaviors.immutable[Command] { (ctx, msg) ⇒ + Behaviors.receive[Command] { (ctx, msg) ⇒ msg match { case InitialState(value) ⇒ // now we are ready to handle stashed messages if any @@ -53,7 +53,7 @@ object StashDocSpec { } def active(state: String): Behavior[Command] = - Behaviors.immutable { (ctx, msg) ⇒ + Behaviors.receive { (ctx, msg) ⇒ msg match { case Get(replyTo) ⇒ replyTo ! state @@ -69,7 +69,7 @@ object StashDocSpec { } def saving(state: String, replyTo: ActorRef[Done]): Behavior[Command] = - Behaviors.immutable[Command] { (ctx, msg) ⇒ + Behaviors.receive[Command] { (ctx, msg) ⇒ msg match { case SaveSuccess ⇒ replyTo ! Done diff --git a/akka-actor-typed-tests/src/test/scala/docs/akka/typed/coexistence/TypedWatchingUntypedSpec.scala b/akka-actor-typed-tests/src/test/scala/docs/akka/typed/coexistence/TypedWatchingUntypedSpec.scala index 2b89cce378..12d0cf72b9 100644 --- a/akka-actor-typed-tests/src/test/scala/docs/akka/typed/coexistence/TypedWatchingUntypedSpec.scala +++ b/akka-actor-typed-tests/src/test/scala/docs/akka/typed/coexistence/TypedWatchingUntypedSpec.scala @@ -37,13 +37,13 @@ object TypedWatchingUntypedSpec { // illustrating how to pass sender, toUntyped is an implicit extension method untyped.tell(Typed.Ping(context.self), context.self.toUntyped) - Behaviors.immutablePartial[Command] { + Behaviors.receivePartial[Command] { case (ctx, Pong) ⇒ // it's not possible to get the sender, that must be sent in message // context.stop is an implicit extension method ctx.stop(untyped) Behaviors.same - } onSignal { + } receiveSignal { case (_, akka.actor.typed.Terminated(_)) ⇒ Behaviors.stopped } diff --git a/akka-actor-typed-tests/src/test/scala/docs/akka/typed/coexistence/UntypedWatchingTypedSpec.scala b/akka-actor-typed-tests/src/test/scala/docs/akka/typed/coexistence/UntypedWatchingTypedSpec.scala index dcc577edba..44961b6775 100644 --- a/akka-actor-typed-tests/src/test/scala/docs/akka/typed/coexistence/UntypedWatchingTypedSpec.scala +++ b/akka-actor-typed-tests/src/test/scala/docs/akka/typed/coexistence/UntypedWatchingTypedSpec.scala @@ -57,7 +57,7 @@ object UntypedWatchingTypedSpec { case object Pong val behavior: Behavior[Command] = - Behaviors.immutable { (ctx, msg) ⇒ + Behaviors.receive { (ctx, msg) ⇒ msg match { case Ping(replyTo) ⇒ println(s"${ctx.self} got Ping from $replyTo") diff --git a/akka-actor-typed/src/main/scala/akka/actor/typed/Behavior.scala b/akka-actor-typed/src/main/scala/akka/actor/typed/Behavior.scala index d04e6ad059..a2d817d7a7 100644 --- a/akka-actor-typed/src/main/scala/akka/actor/typed/Behavior.scala +++ b/akka-actor-typed/src/main/scala/akka/actor/typed/Behavior.scala @@ -50,6 +50,22 @@ sealed abstract class Behavior[T] { behavior ⇒ * set of behaviors available through the DSLs in [[akka.actor.typed.scaladsl.Behaviors]] and [[akka.actor.typed.javadsl.Behaviors]] */ abstract class ExtensibleBehavior[T] extends Behavior[T] { + /** + * Process an incoming message and return the next behavior. + * + * The returned behavior can in addition to normal behaviors be one of the + * canned special objects: + * + * * returning `stopped` will terminate this Behavior + * * returning `same` designates to reuse the current Behavior + * * returning `unhandled` keeps the same Behavior and signals that the message was not yet handled + * + * Code calling this method should use [[Behavior$]] `canonicalize` to replace + * the special objects with real Behaviors. + */ + @throws(classOf[Exception]) + def receive(ctx: ActorContext[T], msg: T): Behavior[T] + /** * Process an incoming [[Signal]] and return the next behavior. This means * that all lifecycle hooks, ReceiveTimeout, Terminated and Failed messages @@ -67,23 +83,6 @@ abstract class ExtensibleBehavior[T] extends Behavior[T] { */ @throws(classOf[Exception]) def receiveSignal(ctx: ActorContext[T], msg: Signal): Behavior[T] - - /** - * Process an incoming message and return the next behavior. - * - * The returned behavior can in addition to normal behaviors be one of the - * canned special objects: - * - * * returning `stopped` will terminate this Behavior - * * returning `same` designates to reuse the current Behavior - * * returning `unhandled` keeps the same Behavior and signals that the message was not yet handled - * - * Code calling this method should use [[Behavior$]] `canonicalize` to replace - * the special objects with real Behaviors. - */ - @throws(classOf[Exception]) - def receiveMessage(ctx: ActorContext[T], msg: T): Behavior[T] - } object Behavior { @@ -97,7 +96,7 @@ object Behavior { * * Example: * {{{ - * immutable[String] { (ctx, msg) => println(msg); same }.widen[Number] { + * receive[String] { (ctx, msg) => println(msg); same }.widen[Number] { * case b: BigDecimal => s"BigDecimal($b)" * case i: BigInteger => s"BigInteger($i)" * // drop all other kinds of Number @@ -341,7 +340,7 @@ object Behavior { case ext: ExtensibleBehavior[T] ⇒ val possiblyDeferredResult = msg match { case signal: Signal ⇒ ext.receiveSignal(ctx, signal) - case m ⇒ ext.receiveMessage(ctx, m.asInstanceOf[T]) + case m ⇒ ext.receive(ctx, m.asInstanceOf[T]) } start(possiblyDeferredResult, ctx) } diff --git a/akka-actor-typed/src/main/scala/akka/actor/typed/internal/BehaviorImpl.scala b/akka-actor-typed/src/main/scala/akka/actor/typed/internal/BehaviorImpl.scala index 89a5c80df9..16fe86162d 100644 --- a/akka-actor-typed/src/main/scala/akka/actor/typed/internal/BehaviorImpl.scala +++ b/akka-actor-typed/src/main/scala/akka/actor/typed/internal/BehaviorImpl.scala @@ -5,7 +5,7 @@ package akka.actor.typed package internal -import akka.util.LineNumbers +import akka.util.{ ConstantFun, LineNumbers } import akka.annotation.InternalApi import akka.actor.typed.{ ActorContext ⇒ AC } import akka.actor.typed.scaladsl.{ ActorContext ⇒ SAC } @@ -18,8 +18,8 @@ import scala.reflect.ClassTag @InternalApi private[akka] object BehaviorImpl { import Behavior._ - private val _nullFun = (_: Any) ⇒ null - private def nullFun[T] = _nullFun.asInstanceOf[Any ⇒ T] + private[this] final val _any2null = (_: Any) ⇒ null + private[this] final def any2null[T] = _any2null.asInstanceOf[Any ⇒ T] implicit class ContextAs[T](val ctx: AC[T]) extends AnyVal { def as[U]: AC[U] = ctx.asInstanceOf[AC[U]] @@ -46,8 +46,8 @@ import scala.reflect.ClassTag override def receiveSignal(ctx: AC[U], signal: Signal): Behavior[U] = widen(Behavior.interpretSignal(behavior, ctx.as[T], signal), ctx.as[T]) - override def receiveMessage(ctx: AC[U], msg: U): Behavior[U] = - matcher.applyOrElse(msg, nullFun) match { + override def receive(ctx: AC[U], msg: U): Behavior[U] = + matcher.applyOrElse(msg, any2null) match { case null ⇒ unhandled case transformed ⇒ widen(Behavior.interpretMessage(behavior, ctx.as[T], transformed), ctx.as[T]) } @@ -55,7 +55,7 @@ import scala.reflect.ClassTag override def toString: String = s"${behavior.toString}.widen(${LineNumbers(matcher)})" } - class ImmutableBehavior[T]( + class ReceiveBehavior[T]( val onMessage: (SAC[T], T) ⇒ Behavior[T], onSignal: PartialFunction[(SAC[T], Signal), Behavior[T]] = Behavior.unhandledSignal.asInstanceOf[PartialFunction[(SAC[T], Signal), Behavior[T]]]) extends ExtensibleBehavior[T] { @@ -63,9 +63,27 @@ import scala.reflect.ClassTag override def receiveSignal(ctx: AC[T], msg: Signal): Behavior[T] = onSignal.applyOrElse((ctx.asScala, msg), Behavior.unhandledSignal.asInstanceOf[PartialFunction[(SAC[T], Signal), Behavior[T]]]) - override def receiveMessage(ctx: AC[T], msg: T) = onMessage(ctx.asScala, msg) + override def receive(ctx: AC[T], msg: T) = onMessage(ctx.asScala, msg) - override def toString = s"Immutable(${LineNumbers(onMessage)})" + override def toString = s"Receive(${LineNumbers(onMessage)})" + } + + /** + * Similar to [[ReceiveBehavior]] however `onMessage` does not accept context. + * We implement it separately in order to be able to avoid wrapping each function in + * another function which drops the context parameter. + */ + class ReceiveMessageBehavior[T]( + val onMessage: T ⇒ Behavior[T], + onSignal: PartialFunction[(SAC[T], Signal), Behavior[T]] = Behavior.unhandledSignal.asInstanceOf[PartialFunction[(SAC[T], Signal), Behavior[T]]]) + extends ExtensibleBehavior[T] { + + override def receive(ctx: AC[T], msg: T) = onMessage(msg) + + override def receiveSignal(ctx: AC[T], msg: Signal): Behavior[T] = + onSignal.applyOrElse((ctx.asScala, msg), Behavior.unhandledSignal.asInstanceOf[PartialFunction[(SAC[T], Signal), Behavior[T]]]) + + override def toString = s"ReceiveMessage(${LineNumbers(onMessage)})" } def tap[T]( @@ -81,8 +99,8 @@ import scala.reflect.ClassTag onSignal(ctx, sig) true }, - afterMessage = (_, _, b) ⇒ b, // TODO optimize by using more ConstantFun - afterSignal = (_, _, b) ⇒ b, + afterMessage = ConstantFun.scalaAnyThreeToThird, + afterSignal = ConstantFun.scalaAnyThreeToThird, behavior)(ClassTag(classOf[Any])) } @@ -141,7 +159,7 @@ import scala.reflect.ClassTag intercept(afterSignal(ctx.asScala, signal, next), ctx) } - override def receiveMessage(ctx: AC[T], msg: T): Behavior[T] = { + override def receive(ctx: AC[T], msg: T): Behavior[T] = { msg match { case m: U ⇒ val msg2 = beforeOnMessage(ctx.asScala.asInstanceOf[SAC[U]], m) diff --git a/akka-actor-typed/src/main/scala/akka/actor/typed/internal/Restarter.scala b/akka-actor-typed/src/main/scala/akka/actor/typed/internal/Restarter.scala index 6c51b146bf..9a5707cdc8 100644 --- a/akka-actor-typed/src/main/scala/akka/actor/typed/internal/Restarter.scala +++ b/akka-actor-typed/src/main/scala/akka/actor/typed/internal/Restarter.scala @@ -92,7 +92,7 @@ import scala.util.control.NonFatal } catch handleException(ctx, behavior) } - override def receiveMessage(ctx: ActorContext[T], msg: T): Behavior[T] = { + override def receive(ctx: ActorContext[T], msg: T): Behavior[T] = { try { val b = Behavior.interpretMessage(behavior, ctx, msg) supervise(b, ctx) @@ -272,7 +272,7 @@ import scala.util.control.NonFatal super.receiveSignal(ctx, signal) } - override def receiveMessage(ctx: ActorContext[Any], msg: Any): Behavior[Any] = { + override def receive(ctx: ActorContext[Any], msg: Any): Behavior[Any] = { // intercept the scheduled messages and drop incoming messages if we are in backoff mode msg match { case ScheduledRestart ⇒ @@ -290,7 +290,7 @@ import scala.util.control.NonFatal ctx.asScala.system.toUntyped.eventStream.publish(Dropped(msg, ctx.asScala.self)) Behavior.same } else - super.receiveMessage(ctx, msg) + super.receive(ctx, msg) } } diff --git a/akka-actor-typed/src/main/scala/akka/actor/typed/internal/receptionist/LocalReceptionist.scala b/akka-actor-typed/src/main/scala/akka/actor/typed/internal/receptionist/LocalReceptionist.scala index b9b8b04cc4..2ec0179d23 100644 --- a/akka-actor-typed/src/main/scala/akka/actor/typed/internal/receptionist/LocalReceptionist.scala +++ b/akka-actor-typed/src/main/scala/akka/actor/typed/internal/receptionist/LocalReceptionist.scala @@ -8,7 +8,7 @@ import akka.actor.typed.{ ActorRef, Behavior, Terminated } import akka.actor.typed.receptionist.Receptionist._ import akka.actor.typed.receptionist.ServiceKey import akka.actor.typed.scaladsl.{ ActorContext, Behaviors } -import akka.actor.typed.scaladsl.Behaviors.{ immutable, same } +import akka.actor.typed.scaladsl.Behaviors.{ receive, same } import akka.annotation.InternalApi import akka.util.TypedMultiMap @@ -55,12 +55,11 @@ private[akka] object LocalReceptionist extends ReceptionistBehaviorProvider { def watchWith(ctx: ActorContext[Any], target: ActorRef[_], msg: InternalCommand): Unit = ctx.spawnAnonymous[Nothing](Behaviors.setup[Nothing] { innerCtx ⇒ innerCtx.watch(target) - Behaviors.immutable[Nothing]((_, _) ⇒ Behaviors.same) - .onSignal { - case (_, Terminated(`target`)) ⇒ - ctx.self ! msg - Behaviors.stopped - } + Behaviors.receiveSignal[Nothing] { + case (_, Terminated(`target`)) ⇒ + ctx.self ! msg + Behaviors.stopped + } }) // Helper that makes sure that subscribers are notified when an entry is changed @@ -111,7 +110,7 @@ private[akka] object LocalReceptionist extends ReceptionistBehaviorProvider { next(newSubscriptions = subscriptions.removed(key)(subscriber)) } - immutable[Any] { (ctx, msg) ⇒ + receive[Any] { (ctx, msg) ⇒ msg match { case cmd: Command ⇒ onCommand(ctx, cmd) case cmd: InternalCommand ⇒ onInternal(ctx, cmd) diff --git a/akka-actor-typed/src/main/scala/akka/actor/typed/javadsl/BehaviorBuilder.scala b/akka-actor-typed/src/main/scala/akka/actor/typed/javadsl/BehaviorBuilder.scala index 587d0187c5..b02ee2972b 100644 --- a/akka-actor-typed/src/main/scala/akka/actor/typed/javadsl/BehaviorBuilder.scala +++ b/akka-actor-typed/src/main/scala/akka/actor/typed/javadsl/BehaviorBuilder.scala @@ -251,7 +251,7 @@ private class BuiltBehavior[T]( private val signalHandlers: List[Case[T, Signal]] ) extends ExtensibleBehavior[T] { - override def receiveMessage(ctx: typed.ActorContext[T], msg: T): Behavior[T] = receive[T](ctx.asJava, msg, messageHandlers) + override def receive(ctx: typed.ActorContext[T], msg: T): Behavior[T] = receive[T](ctx.asJava, msg, messageHandlers) override def receiveSignal(ctx: typed.ActorContext[T], msg: Signal): Behavior[T] = receive[Signal](ctx.asJava, msg, signalHandlers) diff --git a/akka-actor-typed/src/main/scala/akka/actor/typed/javadsl/Behaviors.scala b/akka-actor-typed/src/main/scala/akka/actor/typed/javadsl/Behaviors.scala index 4a165fff30..6dfff4c1c3 100644 --- a/akka-actor-typed/src/main/scala/akka/actor/typed/javadsl/Behaviors.scala +++ b/akka-actor-typed/src/main/scala/akka/actor/typed/javadsl/Behaviors.scala @@ -7,18 +7,18 @@ package akka.actor.typed.javadsl import java.util.function.{ Function ⇒ JFunction } import scala.reflect.ClassTag -import akka.util.OptionVal +import akka.util.ConstantFun import akka.japi.function.{ Function2 ⇒ JapiFunction2 } -import akka.japi.function.{ Procedure, Procedure2 } +import akka.japi.function.Procedure2 import akka.japi.pf.PFBuilder import akka.actor.typed.Behavior import akka.actor.typed.ExtensibleBehavior import akka.actor.typed.Signal import akka.actor.typed.ActorRef import akka.actor.typed.SupervisorStrategy -import akka.actor.typed.scaladsl.{ ActorContext ⇒ SAC } import akka.actor.typed.internal.{ BehaviorImpl, LoggingBehaviorImpl, Supervisor, TimerSchedulerImpl } -import akka.annotation.ApiMayChange +import akka.annotation.{ ApiMayChange, DoNotInherit } + import scala.collection.JavaConverters._ /** * Factories for [[akka.actor.typed.Behavior]]. @@ -26,12 +26,14 @@ import scala.collection.JavaConverters._ @ApiMayChange object Behaviors { - private val _unitFunction = (_: SAC[Any], _: Any) ⇒ () - private def unitFunction[T] = _unitFunction.asInstanceOf[((SAC[T], Signal) ⇒ Unit)] + private[this] val _two2same = new JapiFunction2[ActorContext[Any], Any, Behavior[Any]] { + override def apply(context: ActorContext[Any], msg: Any): Behavior[Any] = same + } + private[this] def two2same[T] = _two2same.asInstanceOf[JapiFunction2[ActorContext[T], T, Behavior[T]]] /** * `setup` is a factory for a behavior. Creation of the behavior instance is deferred until - * the actor is started, as opposed to [[Behaviors#immutable]] that creates the behavior instance + * the actor is started, as opposed to [[Behaviors#receive]] that creates the behavior instance * immediately before the actor is running. The `factory` function pass the `ActorContext` * as parameter and that can for example be used for spawning child actors. * @@ -43,55 +45,6 @@ object Behaviors { def setup[T](factory: akka.japi.function.Function[ActorContext[T], Behavior[T]]): Behavior[T] = Behavior.DeferredBehavior(ctx ⇒ factory.apply(ctx.asJava)) - /** - * Factory for creating a [[MutableBehavior]] that typically holds mutable state as - * instance variables in the concrete [[MutableBehavior]] implementation class. - * - * Creation of the behavior instance is deferred, i.e. it is created via the `factory` - * function. The reason for the deferred creation is to avoid sharing the same instance in - * multiple actors, and to create a new instance when the actor is restarted. - * - * @param factory - * behavior factory that takes the child actor’s context as argument - * @return the deferred behavior - */ - def mutable[T](factory: akka.japi.function.Function[ActorContext[T], MutableBehavior[T]]): Behavior[T] = - setup(factory) - - /** - * Mutable behavior can be implemented by extending this class and implement the - * abstract method [[MutableBehavior#onMessage]] and optionally override - * [[MutableBehavior#onSignal]]. - * - * Instances of this behavior should be created via [[Behaviors#mutable]] and if - * the [[ActorContext]] is needed it can be passed as a constructor parameter - * from the factory function. - * - * @see [[Behaviors#mutable]] - */ - abstract class MutableBehavior[T] extends ExtensibleBehavior[T] { - private var _receive: OptionVal[Receive[T]] = OptionVal.None - private def receive: Receive[T] = _receive match { - case OptionVal.None ⇒ - val receive = createReceive - _receive = OptionVal.Some(receive) - receive - case OptionVal.Some(r) ⇒ r - } - - @throws(classOf[Exception]) - override final def receiveMessage(ctx: akka.actor.typed.ActorContext[T], msg: T): Behavior[T] = - receive.receiveMessage(msg) - - @throws(classOf[Exception]) - override final def receiveSignal(ctx: akka.actor.typed.ActorContext[T], msg: Signal): Behavior[T] = - receive.receiveSignal(msg) - - def createReceive: Receive[T] - - def receiveBuilder: ReceiveBuilder[T] = ReceiveBuilder.create - } - /** * Return this behavior from message processing in order to advise the * system to reuse the previous behavior. This is provided in order to @@ -153,8 +106,28 @@ object Behaviors { * State is updated by returning a new behavior that holds the new immutable * state. */ - def immutable[T](onMessage: JapiFunction2[ActorContext[T], T, Behavior[T]]): Behavior[T] = - new BehaviorImpl.ImmutableBehavior((ctx, msg) ⇒ onMessage.apply(ctx.asJava, msg)) + def receive[T](onMessage: JapiFunction2[ActorContext[T], T, Behavior[T]]): Behavior[T] = + new BehaviorImpl.ReceiveBehavior((ctx, msg) ⇒ onMessage.apply(ctx.asJava, msg)) + + /** + * Simplified version of [[receive]] with only a single argument - the message + * to be handled. Useful for when the context is already accessible by other means, + * like being wrapped in an [[setup]] or similar. + * + * Construct an actor behavior that can react to incoming messages but not to + * lifecycle signals. After spawning this actor from another actor (or as the + * guardian of an [[akka.actor.typed.ActorSystem]]) it will be executed within an + * [[ActorContext]] that allows access to the system, spawning and watching + * other actors, etc. + * + * This constructor is called immutable because the behavior instance doesn't + * have or close over any mutable state. Processing the next message + * results in a new behavior that can potentially be different from this one. + * State is updated by returning a new behavior that holds the new immutable + * state. + */ + def receiveMessage[T](onMessage: akka.japi.Function[T, Behavior[T]]): Behavior[T] = + new BehaviorImpl.ReceiveBehavior((_, msg) ⇒ onMessage.apply(msg)) /** * Construct an actor behavior that can react to both incoming messages and @@ -169,10 +142,10 @@ object Behaviors { * State is updated by returning a new behavior that holds the new immutable * state. */ - def immutable[T]( + def receive[T]( onMessage: JapiFunction2[ActorContext[T], T, Behavior[T]], onSignal: JapiFunction2[ActorContext[T], Signal, Behavior[T]]): Behavior[T] = { - new BehaviorImpl.ImmutableBehavior( + new BehaviorImpl.ReceiveBehavior( (ctx, msg) ⇒ onMessage.apply(ctx.asJava, msg), { case (ctx, sig) ⇒ onSignal.apply(ctx.asJava, sig) }) } @@ -189,16 +162,13 @@ object Behaviors { * @param type the supertype of all messages accepted by this behavior * @return the behavior builder */ - def immutable[T](`type`: Class[T]): BehaviorBuilder[T] = BehaviorBuilder.create[T] + def receive[T](`type`: Class[T]): BehaviorBuilder[T] = BehaviorBuilder.create[T] /** * Construct an actor behavior that can react to lifecycle signals only. */ - def onSignal[T](handler: JapiFunction2[ActorContext[T], Signal, Behavior[T]]): Behavior[T] = { - val jSame = new JapiFunction2[ActorContext[T], T, Behavior[T]] { - override def apply(ctx: ActorContext[T], msg: T) = same - } - immutable(jSame, handler) + def receiveSignal[T](handler: JapiFunction2[ActorContext[T], Signal, Behavior[T]]): Behavior[T] = { + receive(two2same, handler) } /** @@ -225,7 +195,7 @@ object Behaviors { def monitor[T](monitor: ActorRef[T], behavior: Behavior[T]): Behavior[T] = { BehaviorImpl.tap( (ctx, msg) ⇒ monitor ! msg, - unitFunction, + ConstantFun.scalaAnyTwoToUnit, behavior) } @@ -313,10 +283,9 @@ object Behaviors { def withTimers[T](factory: akka.japi.function.Function[TimerScheduler[T], Behavior[T]]): Behavior[T] = TimerSchedulerImpl.withTimers(timers ⇒ factory.apply(timers)) - trait Receive[T] { - def receiveMessage(msg: T): Behavior[T] - def receiveSignal(msg: Signal): Behavior[T] - } + /** A specialized "receive" behavior that is implemented using message matching builders. */ + @DoNotInherit + trait Receive[T] extends ExtensibleBehavior[T] /** * Provide a MDC ("Mapped Diagnostic Context") for logging from the actor. diff --git a/akka-actor-typed/src/main/scala/akka/actor/typed/javadsl/MutableBehavior.scala b/akka-actor-typed/src/main/scala/akka/actor/typed/javadsl/MutableBehavior.scala new file mode 100644 index 0000000000..0c56170041 --- /dev/null +++ b/akka-actor-typed/src/main/scala/akka/actor/typed/javadsl/MutableBehavior.scala @@ -0,0 +1,43 @@ +/* + * Copyright (C) 2018 Lightbend Inc. + */ + +package akka.actor.typed.javadsl + +import akka.actor.typed.{ Behavior, ExtensibleBehavior, Signal } +import akka.actor.typed.javadsl.Behaviors.Receive +import akka.util.OptionVal + +/** + * Mutable behavior can be implemented by extending this class and implement the + * abstract method [[MutableBehavior#onMessage]] and optionally override + * [[MutableBehavior#onSignal]]. + * + * Instances of this behavior should be created via [[Behaviors#setup]] and if + * the [[ActorContext]] is needed it can be passed as a constructor parameter + * from the factory function. + * + * @see [[Behaviors#setup]] + */ +abstract class MutableBehavior[T] extends ExtensibleBehavior[T] { + private var _receive: OptionVal[Receive[T]] = OptionVal.None + private def receive: Receive[T] = _receive match { + case OptionVal.None ⇒ + val receive = createReceive + _receive = OptionVal.Some(receive) + receive + case OptionVal.Some(r) ⇒ r + } + + @throws(classOf[Exception]) + override final def receive(ctx: akka.actor.typed.ActorContext[T], msg: T): Behavior[T] = + receive.receive(ctx, msg) + + @throws(classOf[Exception]) + override final def receiveSignal(ctx: akka.actor.typed.ActorContext[T], msg: Signal): Behavior[T] = + receive.receiveSignal(ctx, msg) + + def createReceive: Receive[T] + + def receiveBuilder: ReceiveBuilder[T] = ReceiveBuilder.create +} diff --git a/akka-actor-typed/src/main/scala/akka/actor/typed/javadsl/ReceiveBuilder.scala b/akka-actor-typed/src/main/scala/akka/actor/typed/javadsl/ReceiveBuilder.scala index 661b9f85ea..55132f4133 100644 --- a/akka-actor-typed/src/main/scala/akka/actor/typed/javadsl/ReceiveBuilder.scala +++ b/akka-actor-typed/src/main/scala/akka/actor/typed/javadsl/ReceiveBuilder.scala @@ -9,10 +9,11 @@ import akka.japi.function.{ Creator, Function, Predicate } import akka.actor.typed.javadsl.Behaviors.Receive import akka.actor.typed.{ Behavior, Signal } import ReceiveBuilder._ +import akka.actor.typed import akka.annotation.InternalApi /** - * Used when implementing [[Behaviors.MutableBehavior]]. + * Used when implementing [[MutableBehavior]]. * * When handling a message or signal, this [[Behavior]] will consider all handlers in the order they were added, * looking for the first handler for which both the type and the (optional) predicate match. @@ -145,16 +146,18 @@ object ReceiveBuilder { } /** - * Receive type for [[Behaviors.MutableBehavior]] + * Receive type for [[MutableBehavior]] */ -private class BuiltReceive[T]( +private final class BuiltReceive[T]( private val messageHandlers: List[Case[T, T]], private val signalHandlers: List[Case[T, Signal]] ) extends Receive[T] { - override def receiveMessage(msg: T): Behavior[T] = receive[T](msg, messageHandlers) + override def receive(ctx: typed.ActorContext[T], msg: T): Behavior[T] = receive[T](msg, messageHandlers) + // override def receiveMessage(msg: T): Behavior[T] = receive[T](msg, messageHandlers) - override def receiveSignal(msg: Signal): Behavior[T] = receive[Signal](msg, signalHandlers) + override def receiveSignal(ctx: typed.ActorContext[T], msg: Signal): Behavior[T] = receive[Signal](msg, signalHandlers) + // override def receiveSignal(msg: Signal): Behavior[T] = receive[Signal](msg, signalHandlers) @tailrec private def receive[M](msg: M, handlers: List[Case[T, M]]): Behavior[T] = diff --git a/akka-actor-typed/src/main/scala/akka/actor/typed/scaladsl/Behaviors.scala b/akka-actor-typed/src/main/scala/akka/actor/typed/scaladsl/Behaviors.scala index 76c58c819d..b1e5c24aec 100644 --- a/akka-actor-typed/src/main/scala/akka/actor/typed/scaladsl/Behaviors.scala +++ b/akka-actor-typed/src/main/scala/akka/actor/typed/scaladsl/Behaviors.scala @@ -5,11 +5,10 @@ package akka.actor.typed package scaladsl -import akka.annotation.{ ApiMayChange, InternalApi } import akka.actor.typed.internal.{ BehaviorImpl, LoggingBehaviorImpl, Supervisor, TimerSchedulerImpl } +import akka.annotation.{ ApiMayChange, DoNotInherit, InternalApi } import scala.reflect.ClassTag -import scala.util.control.Exception.Catcher /** * Factories for [[akka.actor.typed.Behavior]]. @@ -22,7 +21,7 @@ object Behaviors { /** * `setup` is a factory for a behavior. Creation of the behavior instance is deferred until - * the actor is started, as opposed to [[Behaviors.immutable]] that creates the behavior instance + * the actor is started, as opposed to [[Behaviors.receive]] that creates the behavior instance * immediately before the actor is running. The `factory` function pass the `ActorContext` * as parameter and that can for example be used for spawning child actors. * @@ -34,71 +33,6 @@ object Behaviors { def setup[T](factory: ActorContext[T] ⇒ Behavior[T]): Behavior[T] = Behavior.DeferredBehavior(factory) - /** - * Factory for creating a [[MutableBehavior]] that typically holds mutable state as - * instance variables in the concrete [[MutableBehavior]] implementation class. - * - * Creation of the behavior instance is deferred, i.e. it is created via the `factory` - * function. The reason for the deferred creation is to avoid sharing the same instance in - * multiple actors, and to create a new instance when the actor is restarted. - * - * @param factory behavior factory that takes the child actor’s context as argument - * @return the deferred behavior - */ - def mutable[T](factory: ActorContext[T] ⇒ MutableBehavior[T]): Behavior[T] = - setup(factory) - - /** - * Mutable behavior can be implemented by extending this class and implement the - * abstract method [[MutableBehavior#onMessage]] and optionally override - * [[MutableBehavior#onSignal]]. - * - * Instances of this behavior should be created via [[Behaviors#Mutable]] and if - * the [[ActorContext]] is needed it can be passed as a constructor parameter - * from the factory function. - * - * @see [[Behaviors#Mutable]] - */ - abstract class MutableBehavior[T] extends ExtensibleBehavior[T] { - @throws(classOf[Exception]) - override final def receiveMessage(ctx: akka.actor.typed.ActorContext[T], msg: T): Behavior[T] = - onMessage(msg) - - /** - * Implement this method to process an incoming message and return the next behavior. - * - * The returned behavior can in addition to normal behaviors be one of the canned special objects: - *
    - *
  • returning `stopped` will terminate this Behavior
  • - *
  • returning `this` or `same` designates to reuse the current Behavior
  • - *
  • returning `unhandled` keeps the same Behavior and signals that the message was not yet handled
  • - *
- * - */ - @throws(classOf[Exception]) - def onMessage(msg: T): Behavior[T] - - @throws(classOf[Exception]) - override final def receiveSignal(ctx: akka.actor.typed.ActorContext[T], msg: Signal): Behavior[T] = - onSignal.applyOrElse(msg, { case msg ⇒ Behavior.unhandled }: PartialFunction[Signal, Behavior[T]]) - - /** - * Override this method to process an incoming [[akka.actor.typed.Signal]] and return the next behavior. - * This means that all lifecycle hooks, ReceiveTimeout, Terminated and Failed messages - * can initiate a behavior change. - * - * The returned behavior can in addition to normal behaviors be one of the canned special objects: - * - * * returning `stopped` will terminate this Behavior - * * returning `this` or `same` designates to reuse the current Behavior - * * returning `unhandled` keeps the same Behavior and signals that the message was not yet handled - * - * By default, partial function is empty and does not handle any signals. - */ - @throws(classOf[Exception]) - def onSignal: PartialFunction[Signal, Behavior[T]] = PartialFunction.empty - } - /** * Return this behavior from message processing in order to advise the * system to reuse the previous behavior. This is provided in order to @@ -158,27 +92,48 @@ object Behaviors { * need and in fact should not use (close over) mutable variables, but instead * return a potentially different behavior encapsulating any state changes. */ - def immutable[T](onMessage: (ActorContext[T], T) ⇒ Behavior[T]): Immutable[T] = - new Immutable(onMessage) + def receive[T](onMessage: (ActorContext[T], T) ⇒ Behavior[T]): Receive[T] = + new ReceiveImpl(onMessage) - final class Immutable[T](onMessage: (ActorContext[T], T) ⇒ Behavior[T]) - extends BehaviorImpl.ImmutableBehavior[T](onMessage) { - - def onSignal(onSignal: PartialFunction[(ActorContext[T], Signal), Behavior[T]]): Behavior[T] = - new BehaviorImpl.ImmutableBehavior(onMessage, onSignal) - } + /** + * Simplified version of [[Receive]] with only a single argument - the message + * to be handled. Useful for when the context is already accessible by other means, + * like being wrapped in an [[setup]] or similar. + * + * Construct an actor behavior that can react to both incoming messages and + * lifecycle signals. After spawning this actor from another actor (or as the + * guardian of an [[akka.actor.typed.ActorSystem]]) it will be executed within an + * [[ActorContext]] that allows access to the system, spawning and watching + * other actors, etc. + * + * This constructor is called immutable because the behavior instance does not + * need and in fact should not use (close over) mutable variables, but instead + * return a potentially different behavior encapsulating any state changes. + */ + def receiveMessage[T](onMessage: T ⇒ Behavior[T]): Receive[T] = + new ReceiveMessageImpl(onMessage) /** * Construct an immutable actor behavior from a partial message handler which treats undefined messages as unhandled. */ - def immutablePartial[T](onMessage: PartialFunction[(ActorContext[T], T), Behavior[T]]): Immutable[T] = - Behaviors.immutable[T] { (ctx, t) ⇒ onMessage.applyOrElse((ctx, t), (_: (ActorContext[T], T)) ⇒ Behaviors.unhandled[T]) } + def receivePartial[T](onMessage: PartialFunction[(ActorContext[T], T), Behavior[T]]): Receive[T] = + Behaviors.receive[T] { (ctx, t) ⇒ + onMessage.applyOrElse((ctx, t), (_: (ActorContext[T], T)) ⇒ Behaviors.unhandled[T]) + } + + /** + * Construct an immutable actor behavior from a partial message handler which treats undefined messages as unhandled. + */ + def receiveMessagePartial[T](onMessage: PartialFunction[T, Behavior[T]]): Receive[T] = + Behaviors.receive[T] { (_, t) ⇒ + onMessage.applyOrElse(t, (_: T) ⇒ Behaviors.unhandled[T]) + } /** * Construct an actor behavior that can react to lifecycle signals only. */ - def onSignal[T](handler: PartialFunction[(ActorContext[T], Signal), Behavior[T]]): Behavior[T] = - immutable[T]((_, _) ⇒ same).onSignal(handler) + def receiveSignal[T](handler: PartialFunction[(ActorContext[T], Signal), Behavior[T]]): Behavior[T] = + receive[T]((_, _) ⇒ same).receiveSignal(handler) /** * This type of Behavior wraps another Behavior while allowing you to perform @@ -263,4 +218,30 @@ object Behaviors { // TODO // final case class Selective[T](timeout: FiniteDuration, selector: PartialFunction[T, Behavior[T]], onTimeout: () ⇒ Behavior[T]) + /** + * Immutable behavior that exposes additional fluent DSL methods + * to further change the message or signal reception behavior. + */ + @DoNotInherit + trait Receive[T] extends ExtensibleBehavior[T] { + def receiveSignal(onSignal: PartialFunction[(ActorContext[T], Signal), Behavior[T]]): Behavior[T] + + // TODO orElse can be defined here + } + + @InternalApi + private[akka] final class ReceiveImpl[T](onMessage: (ActorContext[T], T) ⇒ Behavior[T]) + extends BehaviorImpl.ReceiveBehavior[T](onMessage) with Receive[T] { + + override def receiveSignal(onSignal: PartialFunction[(ActorContext[T], Signal), Behavior[T]]): Behavior[T] = + new BehaviorImpl.ReceiveBehavior(onMessage, onSignal) + } + @InternalApi + private[akka] final class ReceiveMessageImpl[T](onMessage: T ⇒ Behavior[T]) + extends BehaviorImpl.ReceiveMessageBehavior[T](onMessage) with Receive[T] { + + override def receiveSignal(onSignal: PartialFunction[(ActorContext[T], Signal), Behavior[T]]): Behavior[T] = + new BehaviorImpl.ReceiveMessageBehavior[T](onMessage, onSignal) + } + } diff --git a/akka-actor-typed/src/main/scala/akka/actor/typed/scaladsl/MutableBehavior.scala b/akka-actor-typed/src/main/scala/akka/actor/typed/scaladsl/MutableBehavior.scala new file mode 100644 index 0000000000..94dc90b604 --- /dev/null +++ b/akka-actor-typed/src/main/scala/akka/actor/typed/scaladsl/MutableBehavior.scala @@ -0,0 +1,58 @@ +/* + * Copyright (C) 2018 Lightbend Inc. + */ + +package akka.actor.typed.scaladsl + +import akka.actor.typed.{ Behavior, ExtensibleBehavior, Signal } + +/** + * Mutable behavior can be implemented by extending this class and implement the + * abstract method [[MutableBehavior#onMessage]] and optionally override + * [[MutableBehavior#onSignal]]. + * + * Instances of this behavior should be created via [[Behaviors#setup]] and if + * the [[ActorContext]] is needed it can be passed as a constructor parameter + * from the factory function. + * + * @see [[Behaviors#setup]] + */ +abstract class MutableBehavior[T] extends ExtensibleBehavior[T] { + @throws(classOf[Exception]) + override final def receive(ctx: akka.actor.typed.ActorContext[T], msg: T): Behavior[T] = + onMessage(msg) + + /** + * Implement this method to process an incoming message and return the next behavior. + * + * The returned behavior can in addition to normal behaviors be one of the canned special objects: + *
    + *
  • returning `stopped` will terminate this Behavior
  • + *
  • returning `this` or `same` designates to reuse the current Behavior
  • + *
  • returning `unhandled` keeps the same Behavior and signals that the message was not yet handled
  • + *
+ * + */ + @throws(classOf[Exception]) + def onMessage(msg: T): Behavior[T] + + @throws(classOf[Exception]) + override final def receiveSignal(ctx: akka.actor.typed.ActorContext[T], msg: Signal): Behavior[T] = + onSignal.applyOrElse(msg, { case _ ⇒ Behavior.unhandled }: PartialFunction[Signal, Behavior[T]]) + + /** + * Override this method to process an incoming [[akka.actor.typed.Signal]] and return the next behavior. + * This means that all lifecycle hooks, ReceiveTimeout, Terminated and Failed messages + * can initiate a behavior change. + * + * The returned behavior can in addition to normal behaviors be one of the canned special objects: + * + * * returning `stopped` will terminate this Behavior + * * returning `this` or `same` designates to reuse the current Behavior + * * returning `unhandled` keeps the same Behavior and signals that the message was not yet handled + * + * By default, partial function is empty and does not handle any signals. + */ + @throws(classOf[Exception]) + def onSignal: PartialFunction[Signal, Behavior[T]] = PartialFunction.empty +} diff --git a/akka-actor/src/main/scala/akka/actor/AbstractActor.scala b/akka-actor/src/main/scala/akka/actor/AbstractActor.scala index b56776d5ee..8041ac8a2a 100644 --- a/akka-actor/src/main/scala/akka/actor/AbstractActor.scala +++ b/akka-actor/src/main/scala/akka/actor/AbstractActor.scala @@ -28,7 +28,8 @@ object AbstractActor { * Composes this `Receive` with a fallback which gets applied * where this partial function is not defined. */ - def orElse(other: Receive): Receive = new Receive(onMessage.orElse(other.onMessage)) + def orElse(other: Receive): Receive = + new Receive(onMessage.orElse(other.onMessage)) } /** diff --git a/akka-actor/src/main/scala/akka/util/ConstantFun.scala b/akka-actor/src/main/scala/akka/util/ConstantFun.scala index bea14d6a7d..8f82aeafc9 100644 --- a/akka-actor/src/main/scala/akka/util/ConstantFun.scala +++ b/akka-actor/src/main/scala/akka/util/ConstantFun.scala @@ -31,6 +31,7 @@ import akka.japi.{ Pair ⇒ JPair } def scalaAnyTwoToUnit[A, B]: (A, B) ⇒ Unit = two2unit def scalaAnyTwoToTrue[A, B]: (A, B) ⇒ Boolean = two2true def scalaAnyThreeToFalse[A, B, C]: (A, B, C) ⇒ Boolean = three2false + def scalaAnyThreeToThird[A, B, C]: (A, B, C) ⇒ C = three2third.asInstanceOf[(A, B, C) ⇒ C] def javaAnyToNone[A, B]: A ⇒ Option[B] = none def nullFun[T] = _nullFun.asInstanceOf[Any ⇒ T] @@ -54,4 +55,6 @@ import akka.japi.{ Pair ⇒ JPair } private val three2false = (_: Any, _: Any, _: Any) ⇒ false + private val three2third = (_: Any, _: Any, third: Any) ⇒ third + } diff --git a/akka-cluster-sharding-typed/src/test/java/jdoc/akka/cluster/sharding/typed/ShardingCompileOnlyTest.java b/akka-cluster-sharding-typed/src/test/java/jdoc/akka/cluster/sharding/typed/ShardingCompileOnlyTest.java index a96592e247..2cae52a2a4 100644 --- a/akka-cluster-sharding-typed/src/test/java/jdoc/akka/cluster/sharding/typed/ShardingCompileOnlyTest.java +++ b/akka-cluster-sharding-typed/src/test/java/jdoc/akka/cluster/sharding/typed/ShardingCompileOnlyTest.java @@ -36,7 +36,7 @@ public class ShardingCompileOnlyTest { } public static Behavior counter(String entityId, Integer value) { - return Behaviors.immutable(CounterCommand.class) + return Behaviors.receive(CounterCommand.class) .onMessage(Increment.class, (ctx, msg) -> { return counter(entityId,value + 1); }) diff --git a/akka-cluster-sharding-typed/src/test/scala/akka/cluster/sharding/typed/scaladsl/ClusterShardingPersistenceSpec.scala b/akka-cluster-sharding-typed/src/test/scala/akka/cluster/sharding/typed/scaladsl/ClusterShardingPersistenceSpec.scala index 2a73996683..f2329de73e 100644 --- a/akka-cluster-sharding-typed/src/test/scala/akka/cluster/sharding/typed/scaladsl/ClusterShardingPersistenceSpec.scala +++ b/akka-cluster-sharding-typed/src/test/scala/akka/cluster/sharding/typed/scaladsl/ClusterShardingPersistenceSpec.scala @@ -45,7 +45,7 @@ object ClusterShardingPersistenceSpec { import PersistentBehaviors._ def persistentActor(entityId: String): Behavior[Command] = - PersistentBehaviors.immutable[Command, String, String]( + PersistentBehaviors.receive[Command, String, String]( entityId, initialState = "", commandHandler = (_, state, cmd) ⇒ cmd match { diff --git a/akka-cluster-sharding-typed/src/test/scala/akka/cluster/sharding/typed/scaladsl/ClusterShardingSpec.scala b/akka-cluster-sharding-typed/src/test/scala/akka/cluster/sharding/typed/scaladsl/ClusterShardingSpec.scala index 2c015212a0..e14e65f5d1 100644 --- a/akka-cluster-sharding-typed/src/test/scala/akka/cluster/sharding/typed/scaladsl/ClusterShardingSpec.scala +++ b/akka-cluster-sharding-typed/src/test/scala/akka/cluster/sharding/typed/scaladsl/ClusterShardingSpec.scala @@ -135,7 +135,7 @@ class ClusterShardingSpec extends ActorTestKit with TypedAkkaSpecWithShutdown { } private val typeKey = EntityTypeKey[TestProtocol]("envelope-shard") - private val behavior = Behaviors.immutable[TestProtocol] { + private val behavior = Behaviors.receive[TestProtocol] { case (_, StopPlz()) ⇒ Behaviors.stopped @@ -150,7 +150,7 @@ class ClusterShardingSpec extends ActorTestKit with TypedAkkaSpecWithShutdown { } private val typeKey2 = EntityTypeKey[IdTestProtocol]("no-envelope-shard") - private val behaviorWithId = Behaviors.immutable[IdTestProtocol] { + private val behaviorWithId = Behaviors.receive[IdTestProtocol] { case (_, IdStopPlz()) ⇒ Behaviors.stopped diff --git a/akka-cluster-sharding-typed/src/test/scala/doc/akka/cluster/sharding/typed/ShardingCompileOnlySpec.scala b/akka-cluster-sharding-typed/src/test/scala/doc/akka/cluster/sharding/typed/ShardingCompileOnlySpec.scala index 8a2cefc18b..de2e170afe 100644 --- a/akka-cluster-sharding-typed/src/test/scala/doc/akka/cluster/sharding/typed/ShardingCompileOnlySpec.scala +++ b/akka-cluster-sharding-typed/src/test/scala/doc/akka/cluster/sharding/typed/ShardingCompileOnlySpec.scala @@ -30,7 +30,7 @@ object ShardingCompileOnlySpec { final case class GetValue(replyTo: ActorRef[Int]) extends CounterCommand case object GoodByeCounter extends CounterCommand - def counter(entityId: String, value: Int): Behavior[CounterCommand] = Behaviors.immutable[CounterCommand] { + def counter(entityId: String, value: Int): Behavior[CounterCommand] = Behaviors.receive[CounterCommand] { case (ctx, Increment) ⇒ counter(entityId, value + 1) case (ctx, GetValue(replyTo)) ⇒ diff --git a/akka-cluster-typed/src/main/scala/akka/cluster/ddata/typed/internal/ReplicatorBehavior.scala b/akka-cluster-typed/src/main/scala/akka/cluster/ddata/typed/internal/ReplicatorBehavior.scala index bfec589598..24ea440f2e 100644 --- a/akka-cluster-typed/src/main/scala/akka/cluster/ddata/typed/internal/ReplicatorBehavior.scala +++ b/akka-cluster-typed/src/main/scala/akka/cluster/ddata/typed/internal/ReplicatorBehavior.scala @@ -59,7 +59,7 @@ import akka.actor.typed.Terminated } } - Behaviors.immutable[SReplicator.Command] { (ctx, msg) ⇒ + Behaviors.receive[SReplicator.Command] { (ctx, msg) ⇒ msg match { case cmd: SReplicator.Get[_] ⇒ untypedReplicator.tell( @@ -184,7 +184,7 @@ import akka.actor.typed.Terminated } } - .onSignal { + .receiveSignal { case (ctx, Terminated(ref: ActorRef[JReplicator.Changed[ReplicatedData]] @unchecked)) ⇒ stopSubscribeAdapter(ref) } diff --git a/akka-cluster-typed/src/main/scala/akka/cluster/typed/internal/AdaptedClusterImpl.scala b/akka-cluster-typed/src/main/scala/akka/cluster/typed/internal/AdaptedClusterImpl.scala index eb1b4c7bc0..a4ce329e4d 100644 --- a/akka-cluster-typed/src/main/scala/akka/cluster/typed/internal/AdaptedClusterImpl.scala +++ b/akka-cluster-typed/src/main/scala/akka/cluster/typed/internal/AdaptedClusterImpl.scala @@ -52,7 +52,7 @@ private[akka] object AdapterClusterImpl { } } - Behaviors.immutable[AnyRef] { (ctx, msg) ⇒ + Behaviors.receive[AnyRef] { (ctx, msg) ⇒ msg match { case Subscribe(subscriber: ActorRef[SelfUp] @unchecked, clazz) if clazz == classOf[SelfUp] ⇒ @@ -94,7 +94,7 @@ private[akka] object AdapterClusterImpl { Behaviors.same } - }.onSignal { + }.receiveSignal { case (_, Terminated(ref)) ⇒ upSubscribers = upSubscribers.filterNot(_ == ref) @@ -104,7 +104,7 @@ private[akka] object AdapterClusterImpl { }.narrow[ClusterStateSubscription] } - private def managerBehavior(adaptedCluster: akka.cluster.Cluster) = Behaviors.immutable[ClusterCommand]((ctx, msg) ⇒ + private def managerBehavior(adaptedCluster: akka.cluster.Cluster) = Behaviors.receive[ClusterCommand]((ctx, msg) ⇒ msg match { case Join(address) ⇒ adaptedCluster.join(address) diff --git a/akka-cluster-typed/src/main/scala/akka/cluster/typed/internal/receptionist/ClusterReceptionist.scala b/akka-cluster-typed/src/main/scala/akka/cluster/typed/internal/receptionist/ClusterReceptionist.scala index f4bba1bef8..fec22d115d 100644 --- a/akka-cluster-typed/src/main/scala/akka/cluster/typed/internal/receptionist/ClusterReceptionist.scala +++ b/akka-cluster-typed/src/main/scala/akka/cluster/typed/internal/receptionist/ClusterReceptionist.scala @@ -136,8 +136,8 @@ private[typed] object ClusterReceptionist extends ReceptionistBehaviorProvider { def watchWith(ctx: ActorContext[Any], target: ActorRef[_], msg: InternalCommand): Unit = ctx.spawnAnonymous[Nothing](Behaviors.setup[Nothing] { innerCtx ⇒ innerCtx.watch(target) - Behaviors.immutable[Nothing]((_, _) ⇒ Behaviors.same) - .onSignal { + Behaviors.receive[Nothing]((_, _) ⇒ Behaviors.same) + .receiveSignal { case (_, Terminated(`target`)) ⇒ ctx.self ! msg Behaviors.stopped @@ -263,7 +263,7 @@ private[typed] object ClusterReceptionist extends ReceptionistBehaviorProvider { Behavior.same } - Behaviors.immutable[Any] { (ctx, msg) ⇒ + Behaviors.receive[Any] { (ctx, msg) ⇒ msg match { // support two heterogenous types of messages without union types case cmd: Command ⇒ onCommand(cmd) diff --git a/akka-cluster-typed/src/test/java/akka/cluster/ddata/typed/javadsl/ReplicatorTest.java b/akka-cluster-typed/src/test/java/akka/cluster/ddata/typed/javadsl/ReplicatorTest.java index cb4dafb21f..ab59c5119e 100644 --- a/akka-cluster-typed/src/test/java/akka/cluster/ddata/typed/javadsl/ReplicatorTest.java +++ b/akka-cluster-typed/src/test/java/akka/cluster/ddata/typed/javadsl/ReplicatorTest.java @@ -26,7 +26,7 @@ import akka.actor.typed.Behavior; import akka.cluster.ddata.typed.javadsl.Replicator.Command; import akka.actor.typed.javadsl.Behaviors; import akka.actor.typed.javadsl.Adapter; -import akka.actor.typed.javadsl.Behaviors.MutableBehavior; +import akka.actor.typed.javadsl.MutableBehavior; import akka.actor.typed.javadsl.ActorContext; public class ReplicatorTest extends JUnitSuite { @@ -111,7 +111,7 @@ public class ReplicatorTest extends JUnitSuite { } public static Behavior create(ActorRef replicator, Cluster node) { - return Behaviors.mutable(ctx -> new Client(replicator, node, ctx)); + return Behaviors.setup(ctx -> new Client(replicator, node, ctx)); } @Override diff --git a/akka-cluster-typed/src/test/java/jdocs/akka/cluster/typed/ReceptionistExampleTest.java b/akka-cluster-typed/src/test/java/jdocs/akka/cluster/typed/ReceptionistExampleTest.java index 1388e8fe33..8e6f82510b 100644 --- a/akka-cluster-typed/src/test/java/jdocs/akka/cluster/typed/ReceptionistExampleTest.java +++ b/akka-cluster-typed/src/test/java/jdocs/akka/cluster/typed/ReceptionistExampleTest.java @@ -10,6 +10,7 @@ import akka.actor.typed.ActorSystem; import akka.actor.typed.Behavior; import akka.actor.typed.javadsl.ActorContext; import akka.actor.typed.javadsl.Behaviors; +import akka.actor.typed.javadsl.MutableBehavior; import akka.actor.typed.receptionist.Receptionist; import akka.actor.typed.receptionist.ServiceKey; import akka.cluster.ClusterEvent; @@ -31,7 +32,7 @@ public class ReceptionistExampleTest extends JUnitSuite { static class RandomRouter { - private static class RouterBehavior extends Behaviors.MutableBehavior { + private static class RouterBehavior extends MutableBehavior { private final Class messageClass; private final ServiceKey serviceKey; private final List> routees = new ArrayList<>(); @@ -58,7 +59,7 @@ public class ReceptionistExampleTest extends JUnitSuite { } public static Behavior router(ServiceKey serviceKey, Class messageClass) { - return Behaviors.mutable(ctx -> new RouterBehavior(ctx, messageClass, serviceKey)).narrow(); + return Behaviors.setup(ctx -> new RouterBehavior(ctx, messageClass, serviceKey)).narrow(); } } @@ -73,7 +74,7 @@ public class ReceptionistExampleTest extends JUnitSuite { } } - private static class ClusterRouterBehavior extends Behaviors.MutableBehavior { + private static class ClusterRouterBehavior extends MutableBehavior { private final Class messageClass; private final ServiceKey serviceKey; private final List> routees = new ArrayList<>(); @@ -133,7 +134,7 @@ public class ReceptionistExampleTest extends JUnitSuite { } public static Behavior clusterRouter(ServiceKey serviceKey, Class messageClass) { - return Behaviors.mutable((ctx) -> new ClusterRouterBehavior(ctx, messageClass, serviceKey)).narrow(); + return Behaviors.setup((ctx) -> new ClusterRouterBehavior(ctx, messageClass, serviceKey)).narrow(); } } @@ -155,7 +156,7 @@ public class ReceptionistExampleTest extends JUnitSuite { return Behaviors.setup((ctx) -> { ctx.getSystem().receptionist() .tell(Receptionist.register(PingServiceKey, ctx.getSelf())); - return Behaviors.immutable(Ping.class) + return Behaviors.receive(Ping.class) .onMessage(Ping.class, (c, msg) -> { msg.replyTo.tell(new Pong()); return Behaviors.same(); @@ -168,7 +169,7 @@ public class ReceptionistExampleTest extends JUnitSuite { static Behavior pinger(ActorRef pingService) { return Behaviors.setup((ctx) -> { pingService.tell(new Ping(ctx.getSelf())); - return Behaviors.immutable(Pong.class) + return Behaviors.receive(Pong.class) .onMessage(Pong.class, (c, msg) -> { System.out.println("I was ponged! " + msg); return Behaviors.same(); @@ -184,7 +185,7 @@ public class ReceptionistExampleTest extends JUnitSuite { .tell(Receptionist.subscribe(PingServiceKey, ctx.getSelf().narrow())); ActorRef ps = ctx.spawnAnonymous(pingService()); ctx.watch(ps); - return Behaviors.immutable(Object.class) + return Behaviors.receive(Object.class) .onMessage(Receptionist.Listing.class, listing -> listing.isForKey(PingServiceKey), (c, msg) -> { msg.getServiceInstances(PingServiceKey).forEach(ar -> ctx.spawnAnonymous(pinger(ar))); return Behaviors.same(); diff --git a/akka-cluster-typed/src/test/scala/akka/cluster/ddata/typed/scaladsl/ReplicatorSpec.scala b/akka-cluster-typed/src/test/scala/akka/cluster/ddata/typed/scaladsl/ReplicatorSpec.scala index 01f8e1f5fc..4cca8197a0 100644 --- a/akka-cluster-typed/src/test/scala/akka/cluster/ddata/typed/scaladsl/ReplicatorSpec.scala +++ b/akka-cluster-typed/src/test/scala/akka/cluster/ddata/typed/scaladsl/ReplicatorSpec.scala @@ -58,7 +58,7 @@ object ReplicatorSpec { replicator ! Replicator.Subscribe(Key, changedAdapter) def behavior(cachedValue: Int): Behavior[ClientCommand] = { - Behaviors.immutable[ClientCommand] { (ctx, msg) ⇒ + Behaviors.receive[ClientCommand] { (ctx, msg) ⇒ msg match { case Increment ⇒ replicator ! Replicator.Update(Key, GCounter.empty, Replicator.WriteLocal, updateResponseAdapter)(_ + 1) diff --git a/akka-cluster-typed/src/test/scala/akka/cluster/typed/ActorSystemSpec.scala b/akka-cluster-typed/src/test/scala/akka/cluster/typed/ActorSystemSpec.scala index b6186858aa..fdc1a49c88 100644 --- a/akka-cluster-typed/src/test/scala/akka/cluster/typed/ActorSystemSpec.scala +++ b/akka-cluster-typed/src/test/scala/akka/cluster/typed/ActorSystemSpec.scala @@ -45,7 +45,7 @@ class ActorSystemSpec extends WordSpec with Matchers with BeforeAndAfterAll "start the guardian actor and terminate when it terminates" in { val t = withSystem( "a", - Behaviors.immutable[Probe] { case (_, p) ⇒ p.replyTo ! p.msg; Behaviors.stopped }, doTerminate = false) { sys ⇒ + Behaviors.receive[Probe] { case (_, p) ⇒ p.replyTo ! p.msg; Behaviors.stopped }, doTerminate = false) { sys ⇒ val inbox = TestInbox[String]("a") sys ! Probe("hello", inbox.ref) eventually { @@ -69,9 +69,9 @@ class ActorSystemSpec extends WordSpec with Matchers with BeforeAndAfterAll "terminate the guardian actor" in { val inbox = TestInbox[String]("terminate") val sys = system( - Behaviors.immutable[Probe] { + Behaviors.receive[Probe] { case (_, _) ⇒ Behaviors.unhandled - } onSignal { + } receiveSignal { case (_, PostStop) ⇒ inbox.ref ! "done" Behaviors.same diff --git a/akka-cluster-typed/src/test/scala/akka/cluster/typed/ClusterSingletonApiSpec.scala b/akka-cluster-typed/src/test/scala/akka/cluster/typed/ClusterSingletonApiSpec.scala index 8124e61db9..c3895c3270 100644 --- a/akka-cluster-typed/src/test/scala/akka/cluster/typed/ClusterSingletonApiSpec.scala +++ b/akka-cluster-typed/src/test/scala/akka/cluster/typed/ClusterSingletonApiSpec.scala @@ -48,7 +48,7 @@ object ClusterSingletonApiSpec { case object Perish extends PingProtocol - val pingPong = Behaviors.immutable[PingProtocol] { (_, msg) ⇒ + val pingPong = Behaviors.receive[PingProtocol] { (_, msg) ⇒ msg match { case Ping(respondTo) ⇒ diff --git a/akka-cluster-typed/src/test/scala/akka/cluster/typed/ClusterSingletonPersistenceSpec.scala b/akka-cluster-typed/src/test/scala/akka/cluster/typed/ClusterSingletonPersistenceSpec.scala index 3187d461c7..e94cb819d0 100644 --- a/akka-cluster-typed/src/test/scala/akka/cluster/typed/ClusterSingletonPersistenceSpec.scala +++ b/akka-cluster-typed/src/test/scala/akka/cluster/typed/ClusterSingletonPersistenceSpec.scala @@ -35,7 +35,7 @@ object ClusterSingletonPersistenceSpec { private final case object StopPlz extends Command val persistentActor: Behavior[Command] = - PersistentBehaviors.immutable[Command, String, String]( + PersistentBehaviors.receive[Command, String, String]( persistenceId = "TheSingleton", initialState = "", commandHandler = (_, state, cmd) ⇒ cmd match { diff --git a/akka-cluster-typed/src/test/scala/akka/cluster/typed/RemoteContextAskSpec.scala b/akka-cluster-typed/src/test/scala/akka/cluster/typed/RemoteContextAskSpec.scala index 3743b1eac5..2d3e9b9c9e 100644 --- a/akka-cluster-typed/src/test/scala/akka/cluster/typed/RemoteContextAskSpec.scala +++ b/akka-cluster-typed/src/test/scala/akka/cluster/typed/RemoteContextAskSpec.scala @@ -71,7 +71,7 @@ object RemoteContextAskSpec { case object Pong case class Ping(respondTo: ActorRef[Pong.type]) - def pingPong = Behaviors.immutable[Ping] { (_, msg) ⇒ + def pingPong = Behaviors.receive[Ping] { (_, msg) ⇒ msg match { case Ping(sender) ⇒ sender ! Pong @@ -118,7 +118,7 @@ class RemoteContextAskSpec extends ActorTestKit with TypedAkkaSpecWithShutdown { case Failure(ex) ⇒ ex } - Behaviors.immutable { (_, msg) ⇒ + Behaviors.receive { (_, msg) ⇒ node1Probe.ref ! msg Behaviors.same } diff --git a/akka-cluster-typed/src/test/scala/akka/cluster/typed/RemoteDeployNotAllowedSpec.scala b/akka-cluster-typed/src/test/scala/akka/cluster/typed/RemoteDeployNotAllowedSpec.scala index b4a037a948..3224a17f59 100644 --- a/akka-cluster-typed/src/test/scala/akka/cluster/typed/RemoteDeployNotAllowedSpec.scala +++ b/akka-cluster-typed/src/test/scala/akka/cluster/typed/RemoteDeployNotAllowedSpec.scala @@ -58,7 +58,7 @@ class RemoteDeployNotAllowedSpec extends ActorTestKit with TypedAkkaSpecWithShut case class SpawnChild(name: String) extends GuardianProtocol case object SpawnAnonymous extends GuardianProtocol - val guardianBehavior = Behaviors.immutable[GuardianProtocol] { (ctx, msg) ⇒ + val guardianBehavior = Behaviors.receive[GuardianProtocol] { (ctx, msg) ⇒ msg match { case SpawnChild(name) ⇒ diff --git a/akka-cluster-typed/src/test/scala/akka/cluster/typed/RemoteMessageSpec.scala b/akka-cluster-typed/src/test/scala/akka/cluster/typed/RemoteMessageSpec.scala index fdb9f483ad..da93578144 100644 --- a/akka-cluster-typed/src/test/scala/akka/cluster/typed/RemoteMessageSpec.scala +++ b/akka-cluster-typed/src/test/scala/akka/cluster/typed/RemoteMessageSpec.scala @@ -71,7 +71,7 @@ class RemoteMessageSpec extends AkkaSpec(RemoteMessageSpec.config) { "something something" in { val pingPromise = Promise[Done]() - val ponger = Behaviors.immutable[Ping]((_, msg) ⇒ + val ponger = Behaviors.receive[Ping]((_, msg) ⇒ msg match { case Ping(sender) ⇒ pingPromise.success(Done) @@ -92,7 +92,7 @@ class RemoteMessageSpec extends AkkaSpec(RemoteMessageSpec.config) { ActorRefResolver(typedSystem2).resolveActorRef[Ping](remoteRefStr) val pongPromise = Promise[Done]() - val recipient = system2.spawn(Behaviors.immutable[String] { (_, _) ⇒ + val recipient = system2.spawn(Behaviors.receive[String] { (_, _) ⇒ pongPromise.success(Done) Behaviors.stopped }, "recipient") diff --git a/akka-cluster-typed/src/test/scala/akka/cluster/typed/internal/receptionist/ClusterReceptionistSpec.scala b/akka-cluster-typed/src/test/scala/akka/cluster/typed/internal/receptionist/ClusterReceptionistSpec.scala index 83520c200a..dbfbfe0189 100644 --- a/akka-cluster-typed/src/test/scala/akka/cluster/typed/internal/receptionist/ClusterReceptionistSpec.scala +++ b/akka-cluster-typed/src/test/scala/akka/cluster/typed/internal/receptionist/ClusterReceptionistSpec.scala @@ -56,7 +56,7 @@ object ClusterReceptionistSpec { case class Ping(respondTo: ActorRef[Pong.type]) extends PingProtocol case object Perish extends PingProtocol - val pingPongBehavior = Behaviors.immutable[PingProtocol] { (_, msg) ⇒ + val pingPongBehavior = Behaviors.receive[PingProtocol] { (_, msg) ⇒ msg match { case Ping(respondTo) ⇒ respondTo ! Pong diff --git a/akka-cluster-typed/src/test/scala/docs/akka/cluster/typed/ReceptionistExampleSpec.scala b/akka-cluster-typed/src/test/scala/docs/akka/cluster/typed/ReceptionistExampleSpec.scala index 83134c2e97..3184dd4db1 100644 --- a/akka-cluster-typed/src/test/scala/docs/akka/cluster/typed/ReceptionistExampleSpec.scala +++ b/akka-cluster-typed/src/test/scala/docs/akka/cluster/typed/ReceptionistExampleSpec.scala @@ -27,7 +27,7 @@ object RandomRouter { ctx.system.receptionist ! Receptionist.Subscribe(serviceKey, ctx.self) def routingBehavior(routees: Vector[ActorRef[T]]): Behavior[Any] = - Behaviors.immutable { (_, msg) ⇒ + Behaviors.receive { (_, msg) ⇒ msg match { case serviceKey.Listing(services) ⇒ routingBehavior(services.toVector) @@ -60,7 +60,7 @@ object RandomRouter { cluster.subscriptions ! Subscribe(reachabilityAdapter, classOf[ReachabilityEvent]) def routingBehavior(routees: Vector[ActorRef[T]], unreachable: Set[Address]): Behavior[Any] = - Behaviors.immutable { (_, msg) ⇒ + Behaviors.receive { (_, msg) ⇒ msg match { case serviceKey.Listing(services: Set[ActorRef[T]]) ⇒ routingBehavior(services.toVector, unreachable) @@ -100,7 +100,7 @@ object PingPongExample { val pingService: Behavior[Ping] = Behaviors.setup { ctx ⇒ ctx.system.receptionist ! Receptionist.Register(PingServiceKey, ctx.self) - Behaviors.immutable[Ping] { (_, msg) ⇒ + Behaviors.receive[Ping] { (_, msg) ⇒ msg match { case Ping(replyTo) ⇒ replyTo ! Pong @@ -113,7 +113,7 @@ object PingPongExample { //#pinger def pinger(pingService: ActorRef[Ping]) = Behaviors.setup[Pong.type] { ctx ⇒ pingService ! Ping(ctx.self) - Behaviors.immutable { (_, msg) ⇒ + Behaviors.receive { (_, msg) ⇒ println("I was ponged!!" + msg) Behaviors.same } @@ -125,11 +125,11 @@ object PingPongExample { ctx.system.receptionist ! Receptionist.Subscribe(PingServiceKey, ctx.self) val ps = ctx.spawnAnonymous(pingService) ctx.watch(ps) - Behaviors.immutablePartial[Listing] { - case (_, PingServiceKey.Listing(listings)) if listings.nonEmpty ⇒ + Behaviors.receiveMessagePartial[Listing] { + case PingServiceKey.Listing(listings) if listings.nonEmpty ⇒ listings.foreach(ps ⇒ ctx.spawnAnonymous(pinger(ps))) Behaviors.same - } onSignal { + } receiveSignal { case (_, Terminated(`ps`)) ⇒ println("Ping service has shut down") Behaviors.stopped @@ -141,11 +141,11 @@ object PingPongExample { val guardianJustPingService: Behavior[Nothing] = Behaviors.setup[Listing] { ctx ⇒ val ps = ctx.spawnAnonymous(pingService) ctx.watch(ps) - Behaviors.immutablePartial[Listing] { - case (c, PingServiceKey.Listing(listings)) if listings.nonEmpty ⇒ + Behaviors.receiveMessagePartial[Listing] { + case PingServiceKey.Listing(listings) if listings.nonEmpty ⇒ listings.foreach(ps ⇒ ctx.spawnAnonymous(pinger(ps))) Behaviors.same - } onSignal { + } receiveSignal { case (_, Terminated(`ps`)) ⇒ println("Ping service has shut down") Behaviors.stopped @@ -156,8 +156,8 @@ object PingPongExample { //#pinger-guardian-just-pinger val guardianJustPinger: Behavior[Nothing] = Behaviors.setup[Listing] { ctx ⇒ ctx.system.receptionist ! Receptionist.Subscribe(PingServiceKey, ctx.self) - Behaviors.immutablePartial[Listing] { - case (c, PingServiceKey.Listing(listings)) if listings.nonEmpty ⇒ + Behaviors.receiveMessagePartial[Listing] { + case PingServiceKey.Listing(listings) if listings.nonEmpty ⇒ listings.foreach(ps ⇒ ctx.spawnAnonymous(pinger(ps))) Behaviors.same } diff --git a/akka-docs/src/main/paradox/typed/actors.md b/akka-docs/src/main/paradox/typed/actors.md index dc5fed093c..4776033532 100644 --- a/akka-docs/src/main/paradox/typed/actors.md +++ b/akka-docs/src/main/paradox/typed/actors.md @@ -50,12 +50,11 @@ supplies so that the `HelloWorld` Actor can send back the confirmation message. The behavior of the Actor is defined as the `greeter` value with the help -of the `immutable` behavior constructor. This constructor is called -immutable because the behavior instance doesn't have or close over any mutable -state. Processing the next message may result in a new behavior that can -potentially be different from this one. State is updated by returning a new -behavior that holds the new immutable state. In this case we don't need to -update any state, so we return `Same`. +of the `receive` behavior factory. Processing the next message then results +in a new behavior that can potentially be different from this one. State is +updated by returning a new behavior that holds the new immutable state. In this +case we don't need to update any state, so we return `Same`, which means +the next behavior is "the same as the current one". The type of the messages handled by this behavior is declared to be of class `Greet`, meaning that `msg` argument is @@ -248,14 +247,14 @@ Actor will perform its job on its own accord, we do not need to send messages from the outside, so we declare it to be of type @scala[`NotUsed`]@java[`Void`]. Actors receive not only external messages, they also are notified of certain system events, so-called Signals. In order to get access to those we choose to implement this -particular one using the `immutable` behavior decorator. The +particular one using the `receive` behavior decorator. The provided `onSignal` function will be invoked for signals (subclasses of `Signal`) or the `onMessage` function for user messages. -This particular `main` Actor is created using `Behaviors.onStart`, which is like a factory for a behavior. -Creation of the behavior instance is deferred until the actor is started, as opposed to `Behaviors.immutable` +This particular `main` Actor is created using `Behaviors.setup`, which is like a factory for a behavior. +Creation of the behavior instance is deferred until the actor is started, as opposed to `Behaviors.receive` that creates the behavior instance immediately before the actor is running. The factory function in -`onStart` is passed the `ActorContext` as parameter and that can for example be used for spawning child actors. +`setup` is passed the `ActorContext` as parameter and that can for example be used for spawning child actors. This `main` Actor creates the chat room and the gabbler and the session between them is initiated, and when the gabbler is finished we will receive the `Terminated` event due to having called `ctx.watch` for it. This allows us to shut down the Actor system: when diff --git a/akka-docs/src/main/paradox/typed/interaction-patterns.md b/akka-docs/src/main/paradox/typed/interaction-patterns.md index 4d77b512e9..27ae60f88b 100644 --- a/akka-docs/src/main/paradox/typed/interaction-patterns.md +++ b/akka-docs/src/main/paradox/typed/interaction-patterns.md @@ -224,7 +224,8 @@ Java There are a few things worth noting here: -* To get access to the timers you start with `Behaviors.withTimers` that will pass a `TimerScheduler` instance to the function. This can be used with any type of `Behavior`, such as `immutable` or `mutable`. +* To get access to the timers you start with `Behaviors.withTimers` that will pass a `TimerScheduler` instance to the function. +This can be used with any type of `Behavior`, including `receive`, `receiveMessage`, but also `setup` or any other behavior. * Each timer has a key and if a new timer with same key is started the previous is cancelled and it's guaranteed that a message from the previous timer is not received, even though it might already be enqueued in the mailbox when the new timer is started. * Both periodic and single message timers are supported. * The `TimerScheduler` is mutable in itself, because it performs and manages the side effects of registering the scheduled tasks. diff --git a/akka-persistence-typed/src/main/scala/akka/persistence/typed/internal/EventsourcedReplayingEvents.scala b/akka-persistence-typed/src/main/scala/akka/persistence/typed/internal/EventsourcedReplayingEvents.scala index ee6973d603..cd7758fc0a 100644 --- a/akka-persistence-typed/src/main/scala/akka/persistence/typed/internal/EventsourcedReplayingEvents.scala +++ b/akka-persistence-typed/src/main/scala/akka/persistence/typed/internal/EventsourcedReplayingEvents.scala @@ -68,13 +68,13 @@ private[persistence] class EventsourcedReplayingEvents[C, E, S](override val set private def stay(state: ReplayingState[S]): Behavior[InternalProtocol] = withMdc(setup, MDC.ReplayingEvents) { - Behaviors.immutable[InternalProtocol] { - case (_, JournalResponse(r)) ⇒ onJournalResponse(state, r) - case (_, SnapshotterResponse(r)) ⇒ onSnapshotterResponse(r) - case (_, RecoveryTickEvent(snap)) ⇒ onRecoveryTick(state, snap) - case (_, cmd: IncomingCommand[C]) ⇒ onCommand(cmd) - case (_, RecoveryPermitGranted) ⇒ Behaviors.unhandled // should not happen, we already have the permit - }.onSignal(returnPermitOnStop) + Behaviors.receiveMessage[InternalProtocol] { + case JournalResponse(r) ⇒ onJournalResponse(state, r) + case SnapshotterResponse(r) ⇒ onSnapshotterResponse(r) + case RecoveryTickEvent(snap) ⇒ onRecoveryTick(state, snap) + case cmd: IncomingCommand[C] ⇒ onCommand(cmd) + case RecoveryPermitGranted ⇒ Behaviors.unhandled // should not happen, we already have the permit + }.receiveSignal(returnPermitOnStop) } private def onJournalResponse( diff --git a/akka-persistence-typed/src/main/scala/akka/persistence/typed/internal/EventsourcedReplayingSnapshot.scala b/akka-persistence-typed/src/main/scala/akka/persistence/typed/internal/EventsourcedReplayingSnapshot.scala index ea94c83a0e..c87bcb00c5 100644 --- a/akka-persistence-typed/src/main/scala/akka/persistence/typed/internal/EventsourcedReplayingSnapshot.scala +++ b/akka-persistence-typed/src/main/scala/akka/persistence/typed/internal/EventsourcedReplayingSnapshot.scala @@ -46,13 +46,13 @@ private[akka] class EventsourcedReplayingSnapshot[C, E, S](override val setup: E loadSnapshot(setup.recovery.fromSnapshot, setup.recovery.toSequenceNr) withMdc(setup, MDC.ReplayingSnapshot) { - Behaviors.immutable[InternalProtocol] { - case (_, SnapshotterResponse(r)) ⇒ onSnapshotterResponse(r) - case (_, JournalResponse(r)) ⇒ onJournalResponse(r) - case (_, RecoveryTickEvent(snapshot)) ⇒ onRecoveryTick(snapshot) - case (_, cmd: IncomingCommand[C]) ⇒ onCommand(cmd) - case (_, RecoveryPermitGranted) ⇒ Behaviors.unhandled // should not happen, we already have the permit - }.onSignal(returnPermitOnStop) + Behaviors.receiveMessage[InternalProtocol] { + case SnapshotterResponse(r) ⇒ onSnapshotterResponse(r) + case JournalResponse(r) ⇒ onJournalResponse(r) + case RecoveryTickEvent(snapshot) ⇒ onRecoveryTick(snapshot) + case cmd: IncomingCommand[C] ⇒ onCommand(cmd) + case RecoveryPermitGranted ⇒ Behaviors.unhandled // should not happen, we already have the permit + }.receiveSignal(returnPermitOnStop) } } diff --git a/akka-persistence-typed/src/main/scala/akka/persistence/typed/internal/EventsourcedRequestingRecoveryPermit.scala b/akka-persistence-typed/src/main/scala/akka/persistence/typed/internal/EventsourcedRequestingRecoveryPermit.scala index 61781458ed..c74a5b067b 100644 --- a/akka-persistence-typed/src/main/scala/akka/persistence/typed/internal/EventsourcedRequestingRecoveryPermit.scala +++ b/akka-persistence-typed/src/main/scala/akka/persistence/typed/internal/EventsourcedRequestingRecoveryPermit.scala @@ -37,7 +37,7 @@ private[akka] class EventsourcedRequestingRecoveryPermit[C, E, S](override val s requestRecoveryPermit() withMdc { - Behaviors.immutable[InternalProtocol] { + Behaviors.receive[InternalProtocol] { case (_, InternalProtocol.RecoveryPermitGranted) ⇒ becomeReplaying() diff --git a/akka-persistence-typed/src/main/scala/akka/persistence/typed/internal/EventsourcedRunning.scala b/akka-persistence-typed/src/main/scala/akka/persistence/typed/internal/EventsourcedRunning.scala index e6d6b8db6d..b689d9d4f4 100644 --- a/akka-persistence-typed/src/main/scala/akka/persistence/typed/internal/EventsourcedRunning.scala +++ b/akka-persistence-typed/src/main/scala/akka/persistence/typed/internal/EventsourcedRunning.scala @@ -7,7 +7,7 @@ package akka.persistence.typed.internal import akka.actor.typed.{ Behavior, Signal } import akka.actor.typed.Behavior.StoppedBehavior import akka.actor.typed.scaladsl.Behaviors -import akka.actor.typed.scaladsl.Behaviors.MutableBehavior +import akka.actor.typed.scaladsl.MutableBehavior import akka.annotation.InternalApi import akka.persistence.JournalProtocol._ import akka.persistence._ @@ -148,9 +148,9 @@ private[akka] object EventsourcedRunning { } withMdc(setup, MDC.RunningCmds) { - Behaviors.immutable[EventsourcedBehavior.InternalProtocol] { - case (_, IncomingCommand(c: C @unchecked)) ⇒ onCommand(state, c) - case _ ⇒ Behaviors.unhandled + Behaviors.receiveMessage[EventsourcedBehavior.InternalProtocol] { + case IncomingCommand(c: C @unchecked) ⇒ onCommand(state, c) + case _ ⇒ Behaviors.unhandled } } diff --git a/akka-persistence-typed/src/main/scala/akka/persistence/typed/javadsl/PersistentBehavior.scala b/akka-persistence-typed/src/main/scala/akka/persistence/typed/javadsl/PersistentBehavior.scala index f743646e40..7325e18003 100644 --- a/akka-persistence-typed/src/main/scala/akka/persistence/typed/javadsl/PersistentBehavior.scala +++ b/akka-persistence-typed/src/main/scala/akka/persistence/typed/javadsl/PersistentBehavior.scala @@ -92,7 +92,7 @@ abstract class PersistentBehavior[Command, Event, State >: Null](val persistence /** INTERNAL API */ @InternalApi private[akka] override def untypedProps(props: akka.actor.typed.Props): akka.actor.Props = { - val behaviorImpl = scaladsl.PersistentBehaviors.immutable[Command, Event, State]( + val behaviorImpl = scaladsl.PersistentBehaviors.receive[Command, Event, State]( persistenceId, initialState, (c, state, cmd) ⇒ commandHandler()(c.asJava, state, cmd).asInstanceOf[EffectImpl[Event, State]], diff --git a/akka-persistence-typed/src/main/scala/akka/persistence/typed/scaladsl/PersistentBehaviors.scala b/akka-persistence-typed/src/main/scala/akka/persistence/typed/scaladsl/PersistentBehaviors.scala index 6ec2ac5470..9258173691 100644 --- a/akka-persistence-typed/src/main/scala/akka/persistence/typed/scaladsl/PersistentBehaviors.scala +++ b/akka-persistence-typed/src/main/scala/akka/persistence/typed/scaladsl/PersistentBehaviors.scala @@ -18,7 +18,7 @@ object PersistentBehaviors { /** * Create a `Behavior` for a persistent actor. */ - def immutable[Command, Event, State]( + def receive[Command, Event, State]( persistenceId: String, initialState: State, commandHandler: CommandHandler[Command, Event, State], diff --git a/akka-persistence-typed/src/test/java/akka/persistence/typed/javadsl/PersistentActorTest.java b/akka-persistence-typed/src/test/java/akka/persistence/typed/javadsl/PersistentActorTest.java index da847415e0..f9677dac97 100644 --- a/akka-persistence-typed/src/test/java/akka/persistence/typed/javadsl/PersistentActorTest.java +++ b/akka-persistence-typed/src/test/java/akka/persistence/typed/javadsl/PersistentActorTest.java @@ -8,11 +8,8 @@ import akka.actor.typed.ActorRef; import akka.actor.typed.javadsl.Behaviors; import akka.japi.Pair; import akka.japi.function.Function3; -import akka.persistence.typed.scaladsl.PersistentBehaviorSpec; import akka.persistence.typed.scaladsl.PersistentBehaviorSpec$; -import akka.testkit.AkkaJUnitActorSystemResource; import akka.testkit.typed.javadsl.TestKitJunitResource; -import akka.testkit.typed.scaladsl.ActorTestKit; import akka.testkit.typed.javadsl.TestProbe; import org.junit.ClassRule; import org.junit.Test; @@ -204,7 +201,7 @@ public class PersistentActorTest { .matchCommand(IncrementLater.class, (ctx, state, command) -> { ActorRef delay = ctx.spawnAnonymous(Behaviors.withTimers(timers -> { timers.startSingleTimer(Tick.instance, Tick.instance, FiniteDuration.create(10, TimeUnit.MILLISECONDS)); - return Behaviors.immutable((context, o) -> Behaviors.stopped()); + return Behaviors.receive((context, o) -> Behaviors.stopped()); })); ctx.watchWith(delay, new DelayFinished()); return Effect().none(); diff --git a/akka-persistence-typed/src/test/scala/akka/persistence/typed/internal/RecoveryPermitterSpec.scala b/akka-persistence-typed/src/test/scala/akka/persistence/typed/internal/RecoveryPermitterSpec.scala index 72e68b9df2..7df4567cb1 100644 --- a/akka-persistence-typed/src/test/scala/akka/persistence/typed/internal/RecoveryPermitterSpec.scala +++ b/akka-persistence-typed/src/test/scala/akka/persistence/typed/internal/RecoveryPermitterSpec.scala @@ -42,7 +42,7 @@ object RecoveryPermitterSpec { commandProbe: TestProbe[Any], eventProbe: TestProbe[Any], throwOnRecovery: Boolean = false): Behavior[Command] = - PersistentBehaviors.immutable[Command, Event, State]( + PersistentBehaviors.receive[Command, Event, State]( persistenceId = name, initialState = EmptyState, commandHandler = CommandHandler.command { @@ -57,7 +57,7 @@ object RecoveryPermitterSpec { } def forwardingBehavior(target: TestProbe[Any]): Behavior[Any] = - Behaviors.immutable[Any] { + Behaviors.receive[Any] { (_, any) ⇒ target.ref ! any; Behaviors.same } } @@ -188,7 +188,7 @@ class RecoveryPermitterSpec extends ActorTestKit with TypedAkkaSpecWithShutdown val persistentActor = ctx.spawnAnonymous(persistentBehavior("p3", p3, p3, throwOnRecovery = true)) ctx.watch(persistentActor) - Behaviors.immutable[Command] { + Behaviors.receive[Command] { case (_, StopActor) ⇒ stopProbe.ref ! persistentActor ctx.stop(persistentActor) diff --git a/akka-persistence-typed/src/test/scala/akka/persistence/typed/scaladsl/PersistentActorCompileOnlyTest.scala b/akka-persistence-typed/src/test/scala/akka/persistence/typed/scaladsl/PersistentActorCompileOnlyTest.scala index 6a1a90a6b4..48ec8a05ec 100644 --- a/akka-persistence-typed/src/test/scala/akka/persistence/typed/scaladsl/PersistentActorCompileOnlyTest.scala +++ b/akka-persistence-typed/src/test/scala/akka/persistence/typed/scaladsl/PersistentActorCompileOnlyTest.scala @@ -43,7 +43,7 @@ object PersistentActorCompileOnlyTest { //#behavior val simpleBehavior: PersistentBehavior[SimpleCommand, SimpleEvent, ExampleState] = - PersistentBehaviors.immutable[SimpleCommand, SimpleEvent, ExampleState]( + PersistentBehaviors.receive[SimpleCommand, SimpleEvent, ExampleState]( persistenceId = "sample-id-1", initialState = ExampleState(Nil), commandHandler = commandHandler, @@ -63,7 +63,7 @@ object PersistentActorCompileOnlyTest { case class ExampleState(events: List[String] = Nil) - PersistentBehaviors.immutable[MyCommand, MyEvent, ExampleState]( + PersistentBehaviors.receive[MyCommand, MyEvent, ExampleState]( persistenceId = "sample-id-1", initialState = ExampleState(Nil), @@ -107,7 +107,7 @@ object PersistentActorCompileOnlyTest { .foreach(sender ! _) } - PersistentBehaviors.immutable[Command, Event, EventsInFlight]( + PersistentBehaviors.receive[Command, Event, EventsInFlight]( persistenceId = "recovery-complete-id", initialState = EventsInFlight(0, Map.empty), @@ -149,7 +149,7 @@ object PersistentActorCompileOnlyTest { sealed trait Event case class MoodChanged(to: Mood) extends Event - val b: Behavior[Command] = PersistentBehaviors.immutable[Command, Event, Mood]( + val b: Behavior[Command] = PersistentBehaviors.receive[Command, Event, Mood]( persistenceId = "myPersistenceId", initialState = Happy, commandHandler = CommandHandler.byState { @@ -190,7 +190,7 @@ object PersistentActorCompileOnlyTest { case class State(tasksInFlight: List[Task]) - PersistentBehaviors.immutable[Command, Event, State]( + PersistentBehaviors.receive[Command, Event, State]( persistenceId = "asdf", initialState = State(Nil), commandHandler = CommandHandler.command { @@ -217,7 +217,7 @@ object PersistentActorCompileOnlyTest { def worker(task: Task): Behavior[Nothing] = ??? - PersistentBehaviors.immutable[Command, Event, State]( + PersistentBehaviors.receive[Command, Event, State]( persistenceId = "asdf", initialState = State(Nil), commandHandler = (ctx, _, cmd) ⇒ cmd match { @@ -280,7 +280,7 @@ object PersistentActorCompileOnlyTest { .persist[Event, List[Id]](ItemAdded(id)) .andThen(metadataRegistry ! GetMetaData(id, adapt)) - PersistentBehaviors.immutable[Command, Event, List[Id]]( + PersistentBehaviors.receive[Command, Event, List[Id]]( persistenceId = "basket-1", initialState = Nil, commandHandler = @@ -341,7 +341,7 @@ object PersistentActorCompileOnlyTest { if (currentState == newMood) Effect.none else Effect.persist(MoodChanged(newMood)) - PersistentBehaviors.immutable[Command, Event, Mood]( + PersistentBehaviors.receive[Command, Event, Mood]( persistenceId = "myPersistenceId", initialState = Sad, commandHandler = (_, state, cmd) ⇒ @@ -377,7 +377,7 @@ object PersistentActorCompileOnlyTest { class State - PersistentBehaviors.immutable[Command, Event, State]( + PersistentBehaviors.receive[Command, Event, State]( persistenceId = "myPersistenceId", initialState = new State, commandHandler = CommandHandler.command { diff --git a/akka-persistence-typed/src/test/scala/akka/persistence/typed/scaladsl/PersistentBehaviorSpec.scala b/akka-persistence-typed/src/test/scala/akka/persistence/typed/scaladsl/PersistentBehaviorSpec.scala index 896eade33d..a8e4fa54ff 100644 --- a/akka-persistence-typed/src/test/scala/akka/persistence/typed/scaladsl/PersistentBehaviorSpec.scala +++ b/akka-persistence-typed/src/test/scala/akka/persistence/typed/scaladsl/PersistentBehaviorSpec.scala @@ -84,7 +84,7 @@ object PersistentBehaviorSpec { persistenceId: String, loggingActor: ActorRef[String], probe: ActorRef[(State, Event)]): PersistentBehavior[Command, Event, State] = { - PersistentBehaviors.immutable[Command, Event, State]( + PersistentBehaviors.receive[Command, Event, State]( persistenceId, initialState = State(0, Vector.empty), commandHandler = (ctx, state, cmd) ⇒ cmd match { @@ -116,7 +116,7 @@ object PersistentBehaviorSpec { // purpose is to test signals val delay = ctx.spawnAnonymous(Behaviors.withTimers[Tick.type] { timers ⇒ timers.startSingleTimer(Tick, Tick, 10.millis) - Behaviors.immutable((_, msg) ⇒ msg match { + Behaviors.receive((_, msg) ⇒ msg match { case Tick ⇒ Behaviors.stopped }) }) @@ -412,8 +412,8 @@ class PersistentBehaviorSpec extends ActorTestKit with TypedAkkaSpecWithShutdown val probe = TestProbe[String]() val w = Behaviors.setup[Any] { (ctx) ⇒ ctx.watch(toWatch) - Behaviors.immutable[Any] { (_, _) ⇒ Behaviors.same } - .onSignal { + Behaviors.receive[Any] { (_, _) ⇒ Behaviors.same } + .receiveSignal { case (_, s: Terminated) ⇒ probe.ref ! "Terminated" Behaviors.stopped diff --git a/akka-persistence-typed/src/test/scala/docs/akka/persistence/typed/BasicPersistentBehaviorsSpec.scala b/akka-persistence-typed/src/test/scala/docs/akka/persistence/typed/BasicPersistentBehaviorsSpec.scala index 66e7738f9a..e4ca1f0b0e 100644 --- a/akka-persistence-typed/src/test/scala/docs/akka/persistence/typed/BasicPersistentBehaviorsSpec.scala +++ b/akka-persistence-typed/src/test/scala/docs/akka/persistence/typed/BasicPersistentBehaviorsSpec.scala @@ -15,7 +15,7 @@ object BasicPersistentBehaviorsSpec { case class State() val behavior: Behavior[Command] = - PersistentBehaviors.immutable[Command, Event, State]( + PersistentBehaviors.receive[Command, Event, State]( persistenceId = "abc", initialState = State(), commandHandler = (ctx, state, cmd) ⇒ ???, @@ -24,7 +24,7 @@ object BasicPersistentBehaviorsSpec { //#recovery val recoveryBehavior: Behavior[Command] = - PersistentBehaviors.immutable[Command, Event, State]( + PersistentBehaviors.receive[Command, Event, State]( persistenceId = "abc", initialState = State(), commandHandler = (ctx, state, cmd) ⇒ ???, @@ -36,7 +36,7 @@ object BasicPersistentBehaviorsSpec { //#tagging val taggingBehavior: Behavior[Command] = - PersistentBehaviors.immutable[Command, Event, State]( + PersistentBehaviors.receive[Command, Event, State]( persistenceId = "abc", initialState = State(), commandHandler = (ctx, state, cmd) ⇒ ???, diff --git a/akka-persistence-typed/src/test/scala/docs/akka/persistence/typed/InDepthPersistentBehaviorSpec.scala b/akka-persistence-typed/src/test/scala/docs/akka/persistence/typed/InDepthPersistentBehaviorSpec.scala index a7b5a4565f..892463f326 100644 --- a/akka-persistence-typed/src/test/scala/docs/akka/persistence/typed/InDepthPersistentBehaviorSpec.scala +++ b/akka-persistence-typed/src/test/scala/docs/akka/persistence/typed/InDepthPersistentBehaviorSpec.scala @@ -119,7 +119,7 @@ object InDepthPersistentBehaviorSpec { //#behavior def behavior(entityId: String): Behavior[BlogCommand] = - PersistentBehaviors.immutable[BlogCommand, BlogEvent, BlogState]( + PersistentBehaviors.receive[BlogCommand, BlogEvent, BlogState]( persistenceId = "Blog-" + entityId, initialState = BlogState.empty, commandHandler, diff --git a/akka-stream-typed/src/test/scala/akka/stream/typed/scaladsl/ActorSourceSinkSpec.scala b/akka-stream-typed/src/test/scala/akka/stream/typed/scaladsl/ActorSourceSinkSpec.scala index 88eaaee076..72cfc8360a 100644 --- a/akka-stream-typed/src/test/scala/akka/stream/typed/scaladsl/ActorSourceSinkSpec.scala +++ b/akka-stream-typed/src/test/scala/akka/stream/typed/scaladsl/ActorSourceSinkSpec.scala @@ -48,7 +48,7 @@ class ActorSourceSinkSpec extends ActorTestKit with TypedAkkaSpecWithShutdown { "obey protocol" in { val p = TestProbe[AckProto]() - val autoPilot = Behaviors.immutable[AckProto] { + val autoPilot = Behaviors.receive[AckProto] { (ctx, msg) ⇒ msg match { case m @ Init(sender) ⇒ diff --git a/akka-stream-typed/src/test/scala/akka/stream/typed/scaladsl/CustomGuardianAndMaterializerSpec.scala b/akka-stream-typed/src/test/scala/akka/stream/typed/scaladsl/CustomGuardianAndMaterializerSpec.scala index 480d8315bc..a3a0dfd23a 100644 --- a/akka-stream-typed/src/test/scala/akka/stream/typed/scaladsl/CustomGuardianAndMaterializerSpec.scala +++ b/akka-stream-typed/src/test/scala/akka/stream/typed/scaladsl/CustomGuardianAndMaterializerSpec.scala @@ -25,7 +25,7 @@ object CustomGuardianAndMaterializerSpec { class CustomGuardianAndMaterializerSpec extends ActorTestKit with TypedAkkaSpecWithShutdown { import CustomGuardianAndMaterializerSpec._ - val guardian = Behaviors.immutable[GuardianProtocol] { + val guardian = Behaviors.receive[GuardianProtocol] { (_, msg) ⇒ Behaviors.same } diff --git a/akka-testkit-typed/src/main/scala/akka/testkit/typed/internal/TestKitUtils.scala b/akka-testkit-typed/src/main/scala/akka/testkit/typed/internal/TestKitUtils.scala index 00b5a13231..5189d16e90 100644 --- a/akka-testkit-typed/src/main/scala/akka/testkit/typed/internal/TestKitUtils.scala +++ b/akka-testkit-typed/src/main/scala/akka/testkit/typed/internal/TestKitUtils.scala @@ -20,7 +20,7 @@ private[akka] object ActorTestKitGuardian { final case class SpawnActor[T](name: String, behavior: Behavior[T], replyTo: ActorRef[ActorRef[T]], props: Props) extends TestKitCommand final case class SpawnActorAnonymous[T](behavior: Behavior[T], replyTo: ActorRef[ActorRef[T]], props: Props) extends TestKitCommand - val testKitGuardian: Behavior[TestKitCommand] = Behaviors.immutable[TestKitCommand] { + val testKitGuardian: Behavior[TestKitCommand] = Behaviors.receive[TestKitCommand] { case (ctx, SpawnActor(name, behavior, reply, props)) ⇒ reply ! ctx.spawn(behavior, name, props) Behaviors.same diff --git a/akka-testkit-typed/src/main/scala/akka/testkit/typed/internal/TestProbeImpl.scala b/akka-testkit-typed/src/main/scala/akka/testkit/typed/internal/TestProbeImpl.scala index 178945406e..460a08d6cf 100644 --- a/akka-testkit-typed/src/main/scala/akka/testkit/typed/internal/TestProbeImpl.scala +++ b/akka-testkit-typed/src/main/scala/akka/testkit/typed/internal/TestProbeImpl.scala @@ -27,13 +27,13 @@ private[akka] object TestProbeImpl { private val testActorId = new AtomicInteger(0) private case class WatchActor[U](actor: ActorRef[U]) - private def testActor[M](queue: BlockingDeque[M], terminations: BlockingDeque[Terminated]): Behavior[M] = Behaviors.immutable[M] { (ctx, msg) ⇒ + private def testActor[M](queue: BlockingDeque[M], terminations: BlockingDeque[Terminated]): Behavior[M] = Behaviors.receive[M] { (ctx, msg) ⇒ msg match { case WatchActor(ref) ⇒ ctx.watch(ref) case other ⇒ queue.offerLast(other) } Behaviors.same - }.onSignal { + }.receiveSignal { case (_, t: Terminated) ⇒ terminations.offerLast(t) Behaviors.same diff --git a/akka-testkit-typed/src/test/java/akka/testkit/typed/javadsl/AsyncTestingExampleTest.java b/akka-testkit-typed/src/test/java/akka/testkit/typed/javadsl/AsyncTestingExampleTest.java index 710cb4d794..6a564cc36b 100644 --- a/akka-testkit-typed/src/test/java/akka/testkit/typed/javadsl/AsyncTestingExampleTest.java +++ b/akka-testkit-typed/src/test/java/akka/testkit/typed/javadsl/AsyncTestingExampleTest.java @@ -35,7 +35,7 @@ public class AsyncTestingExampleTest { } } - Behavior echoActor = Behaviors.immutable((ctx, ping) -> { + Behavior echoActor = Behaviors.receive((ctx, ping) -> { ping.replyTo.tell(new Pong(ping.msg)); return Behaviors.same(); }); diff --git a/akka-testkit-typed/src/test/java/akka/testkit/typed/javadsl/ManualTimerExampleTest.java b/akka-testkit-typed/src/test/java/akka/testkit/typed/javadsl/ManualTimerExampleTest.java index 0edbdaec5a..ca9f2c602b 100644 --- a/akka-testkit-typed/src/test/java/akka/testkit/typed/javadsl/ManualTimerExampleTest.java +++ b/akka-testkit-typed/src/test/java/akka/testkit/typed/javadsl/ManualTimerExampleTest.java @@ -35,7 +35,7 @@ public class ManualTimerExampleTest extends JUnitSuite { TestProbe probe = testKit.createTestProbe(); Behavior behavior = Behaviors.withTimers(timer -> { timer.startSingleTimer("T", new Tick(), Duration.create(10, TimeUnit.MILLISECONDS)); - return Behaviors.immutable( (ctx, tick) -> { + return Behaviors.receive( (ctx, tick) -> { probe.ref().tell(new Tock()); return Behaviors.same(); }); diff --git a/akka-testkit-typed/src/test/java/akka/testkit/typed/javadsl/SyncTestingExampleTest.java b/akka-testkit-typed/src/test/java/akka/testkit/typed/javadsl/SyncTestingExampleTest.java index 2315e2e373..ff5f2c41de 100644 --- a/akka-testkit-typed/src/test/java/akka/testkit/typed/javadsl/SyncTestingExampleTest.java +++ b/akka-testkit-typed/src/test/java/akka/testkit/typed/javadsl/SyncTestingExampleTest.java @@ -7,7 +7,6 @@ package akka.testkit.typed.javadsl; //#imports import akka.actor.typed.*; import akka.actor.typed.javadsl.*; -import akka.testkit.typed.javadsl.*; //#imports import org.junit.Test; import org.scalatest.junit.JUnitSuite; @@ -15,7 +14,7 @@ import org.scalatest.junit.JUnitSuite; public class SyncTestingExampleTest extends JUnitSuite { //#child - public static Behavior childActor = Behaviors.immutable((ctx, msg) -> Behaviors.same()); + public static Behavior childActor = Behaviors.receive((ctx, msg) -> Behaviors.same()); //#child //#under-test @@ -42,7 +41,7 @@ public class SyncTestingExampleTest extends JUnitSuite { } } - public static Behavior myBehavior = Behaviors.immutable(Command.class) + public static Behavior myBehavior = Behaviors.receive(Command.class) .onMessage(CreateAChild.class, (ctx, msg) -> { ctx.spawn(childActor, msg.childName); return Behaviors.same(); diff --git a/akka-testkit-typed/src/test/scala/akka/testkit/typed/scaladsl/AsyncTestingExampleSpec.scala b/akka-testkit-typed/src/test/scala/akka/testkit/typed/scaladsl/AsyncTestingExampleSpec.scala index 0c487fdefc..4ebf465268 100644 --- a/akka-testkit-typed/src/test/scala/akka/testkit/typed/scaladsl/AsyncTestingExampleSpec.scala +++ b/akka-testkit-typed/src/test/scala/akka/testkit/typed/scaladsl/AsyncTestingExampleSpec.scala @@ -13,7 +13,7 @@ object AsyncTestingExampleSpec { case class Ping(msg: String, response: ActorRef[Pong]) case class Pong(msg: String) - val echoActor = Behaviors.immutable[Ping] { (_, msg) ⇒ + val echoActor = Behaviors.receive[Ping] { (_, msg) ⇒ msg match { case Ping(m, replyTo) ⇒ replyTo ! Pong(m) diff --git a/akka-testkit-typed/src/test/scala/akka/testkit/typed/scaladsl/BehaviorTestKitSpec.scala b/akka-testkit-typed/src/test/scala/akka/testkit/typed/scaladsl/BehaviorTestKitSpec.scala index 933f30ec2a..e827d068fe 100644 --- a/akka-testkit-typed/src/test/scala/akka/testkit/typed/scaladsl/BehaviorTestKitSpec.scala +++ b/akka-testkit-typed/src/test/scala/akka/testkit/typed/scaladsl/BehaviorTestKitSpec.scala @@ -27,7 +27,7 @@ object BehaviorTestKitSpec { def behavior: Behavior[Command] = init() - def init(): Behavior[Command] = Behaviors.immutable[Command] { (ctx, msg) ⇒ + def init(): Behavior[Command] = Behaviors.receive[Command] { (ctx, msg) ⇒ msg match { case SpawnChildren(numberOfChildren) if numberOfChildren > 0 ⇒ 0.until(numberOfChildren).foreach { i ⇒ @@ -67,7 +67,7 @@ object BehaviorTestKitSpec { sealed trait Action - val initial: Behavior[Action] = Behaviors.immutable[Action] { (_, msg) ⇒ + val initial: Behavior[Action] = Behaviors.receive[Action] { (_, msg) ⇒ msg match { case _ ⇒ Behaviors.empty diff --git a/akka-testkit-typed/src/test/scala/akka/testkit/typed/scaladsl/ManualTimerExampleSpec.scala b/akka-testkit-typed/src/test/scala/akka/testkit/typed/scaladsl/ManualTimerExampleSpec.scala index 05bfe8f7f7..d6f42a6f59 100644 --- a/akka-testkit-typed/src/test/scala/akka/testkit/typed/scaladsl/ManualTimerExampleSpec.scala +++ b/akka-testkit-typed/src/test/scala/akka/testkit/typed/scaladsl/ManualTimerExampleSpec.scala @@ -24,7 +24,7 @@ class ManualTimerExampleSpec extends AbstractActorSpec { val probe = TestProbe[Tock.type]() val behavior = Behaviors.withTimers[Tick.type] { timer ⇒ timer.startSingleTimer("T", Tick, 10.millis) - Behaviors.immutable { (ctx, Tick) ⇒ + Behaviors.receive { (ctx, Tick) ⇒ probe.ref ! Tock Behaviors.same } @@ -48,7 +48,7 @@ class ManualTimerExampleSpec extends AbstractActorSpec { val probe = TestProbe[Tock.type]() val behavior = Behaviors.withTimers[Tick.type] { timer ⇒ timer.startPeriodicTimer("T", Tick, 10.millis) - Behaviors.immutable { (ctx, Tick) ⇒ + Behaviors.receive { (ctx, Tick) ⇒ probe.ref ! Tock Behaviors.same } @@ -76,7 +76,7 @@ class ManualTimerExampleSpec extends AbstractActorSpec { val behavior = Behaviors.withTimers[Command] { timer ⇒ timer.startPeriodicTimer("T", Tick(1), interval) - Behaviors.immutable { (ctx, cmd) ⇒ + Behaviors.receive { (ctx, cmd) ⇒ cmd match { case Tick(n) ⇒ probe.ref ! Tock(n) diff --git a/akka-testkit-typed/src/test/scala/akka/testkit/typed/scaladsl/SyncTestingExampleSpec.scala b/akka-testkit-typed/src/test/scala/akka/testkit/typed/scaladsl/SyncTestingExampleSpec.scala index 36b368a324..b2cc6ef9bb 100644 --- a/akka-testkit-typed/src/test/scala/akka/testkit/typed/scaladsl/SyncTestingExampleSpec.scala +++ b/akka-testkit-typed/src/test/scala/akka/testkit/typed/scaladsl/SyncTestingExampleSpec.scala @@ -13,7 +13,7 @@ import org.scalatest.{ Matchers, WordSpec } object SyncTestingExampleSpec { //#child - val childActor = Behaviors.immutable[String] { (_, _) ⇒ + val childActor = Behaviors.receiveMessage[String] { _ ⇒ Behaviors.same[String] } //#child @@ -26,7 +26,7 @@ object SyncTestingExampleSpec { case object SayHelloToAnonymousChild extends Cmd case class SayHello(who: ActorRef[String]) extends Cmd - val myBehavior = Behaviors.immutablePartial[Cmd] { + val myBehavior = Behaviors.receivePartial[Cmd] { case (ctx, CreateChild(name)) ⇒ ctx.spawn(childActor, name) Behaviors.same diff --git a/akka-testkit-typed/src/test/scala/akka/testkit/typed/scaladsl/TestProbeSpec.scala b/akka-testkit-typed/src/test/scala/akka/testkit/typed/scaladsl/TestProbeSpec.scala index de4f81400a..e4223db543 100644 --- a/akka-testkit-typed/src/test/scala/akka/testkit/typed/scaladsl/TestProbeSpec.scala +++ b/akka-testkit-typed/src/test/scala/akka/testkit/typed/scaladsl/TestProbeSpec.scala @@ -40,11 +40,11 @@ class TestProbeSpec extends AbstractActorSpec { "allow probing for actor stop when actor has not stopped yet" in { case object Stop val probe = TestProbe() - val ref = spawn(Behaviors.immutable[Stop.type]((ctx, message) ⇒ + val ref = spawn(Behaviors.receive[Stop.type]((ctx, message) ⇒ Behaviors.withTimers { (timer) ⇒ timer.startSingleTimer("key", Stop, 300.millis) - Behaviors.immutable((ctx, stop) ⇒ + Behaviors.receive((ctx, stop) ⇒ Behaviors.stopped ) }