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 21f4321c4e..8a16d3edd7 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 @@ -390,26 +390,28 @@ public interface IntroTest { } // #chatroom-gabbler - public static void runChatRoom() throws Exception { + // #chatroom-main + public class Main { + public static Behavior create() { + return Behaviors.setup( + context -> { + ActorRef chatRoom = context.spawn(ChatRoom.create(), "chatRoom"); + ActorRef gabbler = context.spawn(Gabbler.create(), "gabbler"); + context.watch(gabbler); + chatRoom.tell(new ChatRoom.GetSession("ol’ Gabbler", gabbler)); - // #chatroom-main - Behavior main = - Behaviors.setup( - context -> { - ActorRef chatRoom = - context.spawn(ChatRoom.create(), "chatRoom"); - ActorRef gabbler = context.spawn(Gabbler.create(), "gabbler"); - context.watch(gabbler); - chatRoom.tell(new ChatRoom.GetSession("ol’ Gabbler", gabbler)); + return Behaviors.receiveSignal( + (c, sig) -> { + if (sig instanceof Terminated) return Behaviors.stopped(); + else return Behaviors.unhandled(); + }); + }); + } - return Behaviors.receiveSignal( - (c, sig) -> { - if (sig instanceof Terminated) return Behaviors.stopped(); - else return Behaviors.unhandled(); - }); - }); - - final ActorSystem system = ActorSystem.create(main, "ChatRoomDemo"); - // #chatroom-main + public static void main(String[] args) { + ActorSystem.create(Main.create(), "ChatRoomDemo"); + } } + // #chatroom-main + } diff --git a/akka-actor-typed-tests/src/test/java/jdocs/akka/typed/OOIntroTest.java b/akka-actor-typed-tests/src/test/java/jdocs/akka/typed/OOIntroTest.java index 12f1cd2aef..02bf93fef9 100644 --- a/akka-actor-typed-tests/src/test/java/jdocs/akka/typed/OOIntroTest.java +++ b/akka-actor-typed-tests/src/test/java/jdocs/akka/typed/OOIntroTest.java @@ -8,19 +8,26 @@ package jdocs.akka.typed; import akka.actor.typed.ActorRef; import akka.actor.typed.ActorSystem; import akka.actor.typed.Behavior; -import akka.actor.typed.Terminated; -import akka.actor.typed.javadsl.*; +import akka.actor.typed.javadsl.AbstractBehavior; +import akka.actor.typed.javadsl.ActorContext; +import akka.actor.typed.javadsl.Behaviors; +import akka.actor.typed.javadsl.Receive; +import akka.actor.typed.javadsl.ReceiveBuilder; + // #imports + +import akka.actor.typed.Terminated; + import java.io.UnsupportedEncodingException; import java.net.URLEncoder; import java.nio.charset.StandardCharsets; import java.util.ArrayList; import java.util.List; -public class OOIntroTest { +public interface OOIntroTest { // #chatroom-behavior - public static class ChatRoom { + public class ChatRoom { // #chatroom-behavior // #chatroom-protocol static interface RoomCommand {} @@ -175,7 +182,7 @@ public class OOIntroTest { // #chatroom-behavior // #chatroom-gabbler - public static class Gabbler extends AbstractBehavior { + public class Gabbler extends AbstractBehavior { public static Behavior create() { return Behaviors.setup(Gabbler::new); } @@ -215,30 +222,28 @@ public class OOIntroTest { } // #chatroom-gabbler - public static void runChatRoom() { + // #chatroom-main + public class Main { + public static Behavior create() { + return Behaviors.setup( + context -> { + ActorRef chatRoom = context.spawn(ChatRoom.create(), "chatRoom"); + ActorRef gabbler = context.spawn(Gabbler.create(), "gabbler"); + context.watch(gabbler); + chatRoom.tell(new ChatRoom.GetSession("ol’ Gabbler", gabbler)); - // #chatroom-main - Behavior main = - Behaviors.setup( - context -> { - ActorRef chatRoom = - context.spawn(ChatRoom.create(), "chatRoom"); - ActorRef gabbler = context.spawn(Gabbler.create(), "gabbler"); - context.watch(gabbler); - chatRoom.tell(new ChatRoom.GetSession("ol’ Gabbler", gabbler)); + return Behaviors.receiveSignal( + (c, sig) -> { + if (sig instanceof Terminated) return Behaviors.stopped(); + else return Behaviors.unhandled(); + }); + }); + } - return Behaviors.receiveSignal( - (c, sig) -> { - if (sig instanceof Terminated) return Behaviors.stopped(); - else return Behaviors.unhandled(); - }); - }); - - final ActorSystem system = ActorSystem.create(main, "ChatRoomDemo"); - // #chatroom-main + public static void main(String[] args) { + ActorSystem.create(Main.create(), "ChatRoomDemo"); + } } + // #chatroom-main - public static void main(String[] args) throws Exception { - runChatRoom(); - } } 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 e866710046..44c4d9a751 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 @@ -12,6 +12,7 @@ import akka.actor.typed.{ ActorRef, ActorSystem, Behavior } //#fiddle_code import akka.NotUsed +import akka.Done import akka.actor.typed.{ DispatcherSelector, Terminated } import org.scalatest.WordSpecLike @@ -193,13 +194,35 @@ object IntroSpec { } //#chatroom-gabbler + //#chatroom-main + object Main { + def apply(): Behavior[NotUsed] = + Behaviors.setup { context => + val chatRoom = context.spawn(ChatRoom(), "chatroom") + val gabblerRef = context.spawn(Gabbler(), "gabbler") + context.watch(gabblerRef) + chatRoom ! ChatRoom.GetSession("ol’ Gabbler", gabblerRef) + + Behaviors.receiveSignal { + case (_, Terminated(_)) => + Behaviors.stopped + } + } + + def main(args: Array[String]): Unit = { + ActorSystem(Main(), "ChatRoomDemo") + } + + } + //#chatroom-main + } class IntroSpec extends ScalaTestWithActorTestKit with WordSpecLike { import IntroSpec._ - "Hello world" must { + "Intro sample" must { "say hello" in { //#fiddle_code //#hello-world @@ -218,23 +241,8 @@ class IntroSpec extends ScalaTestWithActorTestKit with WordSpecLike { } "chat" in { - //#chatroom-main - val main: Behavior[NotUsed] = - Behaviors.setup { context => - val chatRoom = context.spawn(ChatRoom(), "chatroom") - val gabblerRef = context.spawn(Gabbler(), "gabbler") - context.watch(gabblerRef) - chatRoom ! ChatRoom.GetSession("ol’ Gabbler", gabblerRef) - - Behaviors.receiveSignal { - case (_, Terminated(_)) => - Behaviors.stopped - } - } - - val system = ActorSystem(main, "ChatRoomDemo") - //#chatroom-main - system.whenTerminated // remove compiler warnings + val system = ActorSystem(Main(), "ChatRoomDemo") + system.whenTerminated.futureValue should ===(Done) } } diff --git a/akka-actor-typed-tests/src/test/scala/docs/akka/typed/OOIntroSpec.scala b/akka-actor-typed-tests/src/test/scala/docs/akka/typed/OOIntroSpec.scala index c9753d918e..f8cc733b07 100644 --- a/akka-actor-typed-tests/src/test/scala/docs/akka/typed/OOIntroSpec.scala +++ b/akka-actor-typed-tests/src/test/scala/docs/akka/typed/OOIntroSpec.scala @@ -5,15 +5,19 @@ package docs.akka.typed //#imports +import akka.Done +import akka.actor.typed.{ ActorRef, ActorSystem, Behavior } +import akka.actor.typed.scaladsl.{ AbstractBehavior, ActorContext, Behaviors } +//#imports + +import akka.NotUsed +import akka.actor.typed.Terminated + import java.net.URLEncoder import java.nio.charset.StandardCharsets -import akka.actor.typed._ -import akka.actor.typed.scaladsl.{ AbstractBehavior, ActorContext, Behaviors } - import akka.actor.testkit.typed.scaladsl.ScalaTestWithActorTestKit import org.scalatest.WordSpecLike -//#imports object OOIntroSpec { @@ -107,37 +111,38 @@ object OOIntroSpec { //#chatroom-gabbler } + //#chatroom-main + object Main { + def apply(): Behavior[NotUsed] = + Behaviors.setup { context => + val chatRoom = context.spawn(ChatRoom(), "chatroom") + val gabblerRef = context.spawn(Gabbler(), "gabbler") + context.watch(gabblerRef) + chatRoom ! ChatRoom.GetSession("ol’ Gabbler", gabblerRef) + + Behaviors.receiveSignal { + case (_, Terminated(_)) => + Behaviors.stopped + } + } + + def main(args: Array[String]): Unit = { + ActorSystem(Main(), "ChatRoomDemo") + } + + } + //#chatroom-main + } class OOIntroSpec extends ScalaTestWithActorTestKit with WordSpecLike { import OOIntroSpec._ - "A chat room" must { + "Intro sample" must { "chat" in { - //#chatroom-main - val main: Behavior[String] = - Behaviors.setup { context => - val chatRoom = context.spawn(ChatRoom(), "chatroom") - val gabblerRef = context.spawn(Gabbler(), "gabbler") - context.watch(gabblerRef) - - Behaviors - .receiveMessagePartial[String] { - case "go" => - chatRoom ! ChatRoom.GetSession("ol’ Gabbler", gabblerRef) - Behaviors.same - } - .receiveSignal { - case (_, Terminated(_)) => - context.log.info("Stopping guardian") - Behaviors.stopped - } - } - - val system = ActorSystem(main, "ChatRoomDemo") - system ! "go" - //#chatroom-main + val system = ActorSystem(Main(), "ChatRoomDemo") + system.whenTerminated.futureValue should ===(Done) } } } diff --git a/akka-docs/src/main/paradox/typed/actors.md b/akka-docs/src/main/paradox/typed/actors.md index 51d031e1bb..b3f67e5b59 100644 --- a/akka-docs/src/main/paradox/typed/actors.md +++ b/akka-docs/src/main/paradox/typed/actors.md @@ -250,7 +250,7 @@ Scala Java : @@snip [IntroSpec.scala](/akka-actor-typed-tests/src/test/java/jdocs/akka/typed/IntroTest.java) { #chatroom-main } -In good tradition we call the `main` Actor what it is, it directly +In good tradition we call the `Main` Actor what it is, it directly corresponds to the `main` method in a traditional Java application. This 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 @@ -260,16 +260,16 @@ 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.setup`, which is like a factory for a behavior. +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 `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 +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 `context.watch` for it. This allows us to shut down the Actor system: when -the main Actor terminates there is nothing more to do. +the `Main` Actor terminates there is nothing more to do. -Therefore after creating the Actor system with the `main` Actor’s +Therefore after creating the Actor system with the `Main` Actor’s `Behavior` we can let the `main` method return, the `ActorSystem` will continue running and the JVM alive until the root actor stops. @@ -395,7 +395,7 @@ Scala Java : @@snip [OOIntroTest.java](/akka-actor-typed-tests/src/test/java/jdocs/akka/typed/OOIntroTest.java) { #chatroom-main } -In good tradition we call the `main` Actor what it is, it directly +In good tradition we call the `Main` Actor what it is, it directly corresponds to the `main` method in a traditional Java application. This 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 @@ -405,16 +405,16 @@ 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.setup`, which is like a factory for a behavior. +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 `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 +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 `context.watch` for it. This allows us to shut down the Actor system: when -the main Actor terminates there is nothing more to do. +the `Main` Actor terminates there is nothing more to do. -Therefore after creating the Actor system with the `main` Actor’s +Therefore after creating the Actor system with the `Main` Actor’s `Behavior` we can let the `main` method return, the `ActorSystem` will continue running and the JVM alive until the root actor stops.