Aufgabe 2.4

This commit is contained in:
Christoph Stahl 2023-04-17 10:27:08 +02:00
parent e748f4e870
commit 61fc8c548c
3 changed files with 23 additions and 23 deletions

View File

@ -53,12 +53,16 @@ public class Greeter extends AbstractBehavior<Greeter.Greet> {
// #greeter
}
public static Behavior<Greet> create() {
return Behaviors.setup(Greeter::new);
private final int max;
private int greetingCounter;
public static Behavior<Greet> create(int max) {
return Behaviors.setup(context -> new Greeter(context, max));
}
private Greeter(ActorContext<Greet> context) {
private Greeter(ActorContext<Greet> context, int max) {
super(context);
this.max = max;
}
@Override
@ -67,11 +71,16 @@ public class Greeter extends AbstractBehavior<Greeter.Greet> {
}
private Behavior<Greet> 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

View File

@ -5,16 +5,12 @@ import akka.actor.typed.javadsl.*;
public class GreeterBot extends AbstractBehavior<Greeter.Greeted> {
public static Behavior<Greeter.Greeted> create(int max) {
return Behaviors.setup(context -> new GreeterBot(context, max));
public static Behavior<Greeter.Greeted> create() {
return Behaviors.setup(GreeterBot::new);
}
private final int max;
private int greetingCounter;
private GreeterBot(ActorContext<Greeter.Greeted> context, int max) {
private GreeterBot(ActorContext<Greeter.Greeted> context) {
super(context);
this.max = max;
}
@Override
@ -23,13 +19,8 @@ public class GreeterBot extends AbstractBehavior<Greeter.Greeted> {
}
private Behavior<Greeter.Greeted> 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;
}
}
}

View File

@ -23,7 +23,7 @@ public class GreeterMain extends AbstractBehavior<GreeterMain.SayHello> {
private GreeterMain(ActorContext<SayHello> 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<GreeterMain.SayHello> {
private Behavior<SayHello> onSayHello(SayHello command) {
//#create-actors
ActorRef<Greeter.Greeted> 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;