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 0af72b02cc..911266511b 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 @@ -116,7 +116,7 @@ public class OOIntroTest { ActorRef client = getSession.replyTo; ActorRef ses = context.spawn( - session(context.getSelf(), getSession.screenName, client), + new SessionBehavior(context.getSelf(), getSession.screenName, client), URLEncoder.encode(getSession.screenName, StandardCharsets.UTF_8.name())); // narrow to only expose PostMessage client.tell(new SessionGranted(ses.narrow())); @@ -137,56 +137,81 @@ public class OOIntroTest { } } - public static Behavior session( - ActorRef room, String screenName, ActorRef client) { - return Behaviors.receive(ChatRoom.SessionCommand.class) - .onMessage( - PostMessage.class, - (context, post) -> { - // from client, publish to others via the room - room.tell(new PublishSessionMessage(screenName, post.message)); - return Behaviors.same(); - }) - .onMessage( - NotifyClient.class, - (context, notification) -> { - // published from the room - client.tell(notification.message); - return Behaviors.same(); - }) - .build(); + public static class SessionBehavior extends AbstractBehavior { + + private final ActorRef room; + private final String screenName; + private final ActorRef client; + + public SessionBehavior( + ActorRef room, String screenName, ActorRef client) { + this.room = room; + this.screenName = screenName; + this.client = client; + } + + @Override + public Receive createReceive() { + return newReceiveBuilder() + .onMessage( + PostMessage.class, + post -> { + // from client, publish to others via the room + room.tell(new PublishSessionMessage(screenName, post.message)); + return Behaviors.same(); + }) + .onMessage( + NotifyClient.class, + notification -> { + // published from the room + client.tell(notification.message); + return Behaviors.same(); + }) + .build(); + } } // #chatroom-behavior } // #chatroom-actor // #chatroom-gabbler - public abstract static class Gabbler { - private Gabbler() {} + public static class Gabbler extends AbstractBehavior { - public static Behavior behavior() { - return Behaviors.receive(ChatRoom.SessionEvent.class) + private ActorContext context; + + public Gabbler(ActorContext context) { + this.context = context; + } + + @Override + public Receive createReceive() { + ReceiveBuilder builder = newReceiveBuilder(); + return builder .onMessage( ChatRoom.SessionDenied.class, - (context, message) -> { + message -> { System.out.println("cannot start chat room session: " + message.reason); return Behaviors.stopped(); }) .onMessage( ChatRoom.SessionGranted.class, - (context, message) -> { + message -> { message.handle.tell(new ChatRoom.PostMessage("Hello World!")); return Behaviors.same(); }) .onMessage( ChatRoom.MessagePosted.class, - (context, message) -> { + message -> { System.out.println( "message has been posted by '" + message.screenName + "': " + message.message); return Behaviors.stopped(); }) .build(); } + + public static Behavior behavior() { + return Behaviors.setup(Gabbler::new); + } } // #chatroom-gabbler @@ -213,4 +238,8 @@ public class OOIntroTest { final ActorSystem system = ActorSystem.create(main, "ChatRoomDemo"); // #chatroom-main } + + public static void main(String[] args) throws Exception { + runChatRoom(); + } } diff --git a/akka-docs/src/main/paradox/typed/actors.md b/akka-docs/src/main/paradox/typed/actors.md index 2881c3d4a1..0d29cb3161 100644 --- a/akka-docs/src/main/paradox/typed/actors.md +++ b/akka-docs/src/main/paradox/typed/actors.md @@ -402,9 +402,8 @@ problematic, so passing an @scala[`ActorRef[PublishSessionMessage]`]@java[`Actor #### Trying it out -In order to see this chat room in action we need to write a client Actor that can use it, for this -stateless actor it doesn't make much sense to use the `AbstractBehavior` so let's just reuse the -functional style gabbler from the sample above: +In order to see this chat room in action we need to write a client Actor that can use it +@scala[, for this stateless actor it doesn't make much sense to use the `AbstractBehavior` so let's just reuse the functional style gabbler from the sample above]: Scala : @@snip [OOIntroSpec.scala](/akka-actor-typed-tests/src/test/scala/docs/akka/typed/OOIntroSpec.scala) { #chatroom-gabbler }