Aufgabe 2.4
This commit is contained in:
parent
e748f4e870
commit
61fc8c548c
|
@ -53,12 +53,16 @@ public class Greeter extends AbstractBehavior<Greeter.Greet> {
|
||||||
// #greeter
|
// #greeter
|
||||||
}
|
}
|
||||||
|
|
||||||
public static Behavior<Greet> create() {
|
private final int max;
|
||||||
return Behaviors.setup(Greeter::new);
|
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);
|
super(context);
|
||||||
|
this.max = max;
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
|
@ -67,11 +71,16 @@ public class Greeter extends AbstractBehavior<Greeter.Greet> {
|
||||||
}
|
}
|
||||||
|
|
||||||
private Behavior<Greet> onGreet(Greet command) {
|
private Behavior<Greet> onGreet(Greet command) {
|
||||||
getContext().getLog().info("Hello {}!", command.whom);
|
this.greetingCounter++;
|
||||||
//#greeter-send-message
|
getContext().getLog().info("Hello {}! ({})", command.whom, this.greetingCounter);
|
||||||
command.replyTo.tell(new Greeted(command.whom, getContext().getSelf()));
|
if (this.greetingCounter == max) {
|
||||||
//#greeter-send-message
|
return Behaviors.stopped();
|
||||||
return this;
|
} else {
|
||||||
|
//#greeter-send-message
|
||||||
|
command.replyTo.tell(new Greeted(command.whom, getContext().getSelf()));
|
||||||
|
//#greeter-send-message
|
||||||
|
return this;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
// #greeter
|
// #greeter
|
||||||
|
|
|
@ -5,16 +5,12 @@ import akka.actor.typed.javadsl.*;
|
||||||
|
|
||||||
public class GreeterBot extends AbstractBehavior<Greeter.Greeted> {
|
public class GreeterBot extends AbstractBehavior<Greeter.Greeted> {
|
||||||
|
|
||||||
public static Behavior<Greeter.Greeted> create(int max) {
|
public static Behavior<Greeter.Greeted> create() {
|
||||||
return Behaviors.setup(context -> new GreeterBot(context, max));
|
return Behaviors.setup(GreeterBot::new);
|
||||||
}
|
}
|
||||||
|
|
||||||
private final int max;
|
private GreeterBot(ActorContext<Greeter.Greeted> context) {
|
||||||
private int greetingCounter;
|
|
||||||
|
|
||||||
private GreeterBot(ActorContext<Greeter.Greeted> context, int max) {
|
|
||||||
super(context);
|
super(context);
|
||||||
this.max = max;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
|
@ -23,13 +19,8 @@ public class GreeterBot extends AbstractBehavior<Greeter.Greeted> {
|
||||||
}
|
}
|
||||||
|
|
||||||
private Behavior<Greeter.Greeted> onGreeted(Greeter.Greeted message) {
|
private Behavior<Greeter.Greeted> onGreeted(Greeter.Greeted message) {
|
||||||
greetingCounter++;
|
getContext().getLog().info("Greeting for {}", message.whom);
|
||||||
getContext().getLog().info("Greeting {} for {}", greetingCounter, message.whom);
|
|
||||||
if (greetingCounter == max) {
|
|
||||||
return Behaviors.stopped();
|
|
||||||
} else {
|
|
||||||
message.from.tell(new Greeter.Greet(message.whom, getContext().getSelf()));
|
message.from.tell(new Greeter.Greet(message.whom, getContext().getSelf()));
|
||||||
return this;
|
return this;
|
||||||
}
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -23,7 +23,7 @@ public class GreeterMain extends AbstractBehavior<GreeterMain.SayHello> {
|
||||||
private GreeterMain(ActorContext<SayHello> context) {
|
private GreeterMain(ActorContext<SayHello> context) {
|
||||||
super(context);
|
super(context);
|
||||||
//#create-actors
|
//#create-actors
|
||||||
greeter = context.spawn(Greeter.create(), "greeter");
|
greeter = context.spawn(Greeter.create(6), "greeter");
|
||||||
//#create-actors
|
//#create-actors
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -35,7 +35,7 @@ public class GreeterMain extends AbstractBehavior<GreeterMain.SayHello> {
|
||||||
private Behavior<SayHello> onSayHello(SayHello command) {
|
private Behavior<SayHello> onSayHello(SayHello command) {
|
||||||
//#create-actors
|
//#create-actors
|
||||||
ActorRef<Greeter.Greeted> replyTo =
|
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));
|
greeter.tell(new Greeter.Greet(command.name, replyTo));
|
||||||
//#create-actors
|
//#create-actors
|
||||||
return this;
|
return this;
|
||||||
|
|
Loading…
Reference in New Issue