diff --git a/src/main/java/com/example/Greeter.java b/src/main/java/com/example/Greeter.java index ecc1296..63fdc7c 100644 --- a/src/main/java/com/example/Greeter.java +++ b/src/main/java/com/example/Greeter.java @@ -53,12 +53,16 @@ public class Greeter extends AbstractBehavior { // #greeter } - public static Behavior create() { - return Behaviors.setup(Greeter::new); + private final int max; + private int greetingCounter; + + public static Behavior create(int max) { + return Behaviors.setup(context -> new Greeter(context, max)); } - private Greeter(ActorContext context) { + private Greeter(ActorContext context, int max) { super(context); + this.max = max; } @Override @@ -67,11 +71,16 @@ public class Greeter extends AbstractBehavior { } private Behavior onGreet(Greet command) { - getContext().getLog().info("Hello {}!", command.whom); - //#greeter-send-message - command.replyTo.tell(new Greeted(command.whom, getContext().getSelf())); - //#greeter-send-message - return this; + this.greetingCounter++; + getContext().getLog().info("Hello {}! ({})", command.whom, this.greetingCounter); + if (this.greetingCounter == max) { + return Behaviors.stopped(); + } else { + //#greeter-send-message + command.replyTo.tell(new Greeted(command.whom, getContext().getSelf())); + //#greeter-send-message + return this; + } } } // #greeter diff --git a/src/main/java/com/example/GreeterBot.java b/src/main/java/com/example/GreeterBot.java index 4222a93..57f2ec6 100644 --- a/src/main/java/com/example/GreeterBot.java +++ b/src/main/java/com/example/GreeterBot.java @@ -5,16 +5,12 @@ import akka.actor.typed.javadsl.*; public class GreeterBot extends AbstractBehavior { - public static Behavior create(int max) { - return Behaviors.setup(context -> new GreeterBot(context, max)); + public static Behavior create() { + return Behaviors.setup(GreeterBot::new); } - private final int max; - private int greetingCounter; - - private GreeterBot(ActorContext context, int max) { + private GreeterBot(ActorContext context) { super(context); - this.max = max; } @Override @@ -23,13 +19,8 @@ public class GreeterBot extends AbstractBehavior { } private Behavior onGreeted(Greeter.Greeted message) { - greetingCounter++; - getContext().getLog().info("Greeting {} for {}", greetingCounter, message.whom); - if (greetingCounter == max) { - return Behaviors.stopped(); - } else { + getContext().getLog().info("Greeting for {}", message.whom); message.from.tell(new Greeter.Greet(message.whom, getContext().getSelf())); return this; - } } } diff --git a/src/main/java/com/example/GreeterMain.java b/src/main/java/com/example/GreeterMain.java index ca1bf5f..fa05bc3 100644 --- a/src/main/java/com/example/GreeterMain.java +++ b/src/main/java/com/example/GreeterMain.java @@ -23,7 +23,7 @@ public class GreeterMain extends AbstractBehavior { private GreeterMain(ActorContext context) { super(context); //#create-actors - greeter = context.spawn(Greeter.create(), "greeter"); + greeter = context.spawn(Greeter.create(6), "greeter"); //#create-actors } @@ -35,7 +35,7 @@ public class GreeterMain extends AbstractBehavior { private Behavior onSayHello(SayHello command) { //#create-actors ActorRef replyTo = - getContext().spawn(GreeterBot.create(6), command.name); + getContext().spawn(GreeterBot.create(), command.name); greeter.tell(new Greeter.Greet(command.name, replyTo)); //#create-actors return this;