diff --git a/src/main/java/com/example/AkkaMainSystem.java b/src/main/java/com/example/AkkaMainSystem.java index 868351b..a77fcda 100644 --- a/src/main/java/com/example/AkkaMainSystem.java +++ b/src/main/java/com/example/AkkaMainSystem.java @@ -4,6 +4,9 @@ import akka.actor.typed.ActorRef; import akka.actor.typed.Behavior; import akka.actor.typed.javadsl.*; +import java.util.ArrayList; +import java.util.Arrays; + public class AkkaMainSystem extends AbstractBehavior { public static class Create { @@ -24,11 +27,15 @@ public class AkkaMainSystem extends AbstractBehavior { private Behavior onCreate(Create command) { //#create-actors - ActorRef a = this.getContext().spawn(ExampleActor.create("Alice"), "alice"); - ActorRef b = this.getContext().spawn(ExampleTimerActor.create(), "timeractor"); + var alice = this.getContext().spawn(RelayBot.create("alice"), "alice"); + var bob = this.getContext().spawn(RelayBot.create("bob"), "bob"); + var charlie = this.getContext().spawn(RelayBot.create("charlie"), "charlie"); + + var reihenfolge = new ArrayList<>(Arrays.asList(bob, charlie, alice)); + alice.tell(new RelayBot.Start(reihenfolge)); + //#create-actors - a.tell(new ExampleActor.ExampleMessage(this.getContext().getSelf(),"Test123")); return this; } } diff --git a/src/main/java/com/example/ExampleActor.java b/src/main/java/com/example/ExampleActor.java deleted file mode 100644 index cf56713..0000000 --- a/src/main/java/com/example/ExampleActor.java +++ /dev/null @@ -1,35 +0,0 @@ -package com.example; - -import akka.actor.typed.ActorRef; -import akka.actor.typed.Behavior; -import akka.actor.typed.javadsl.*; - -public class ExampleActor extends AbstractBehavior { - - public interface Message {}; - - public record ExampleMessage(ActorRef someReference, String someString) implements Message { } - - public static Behavior create(String name) { - return Behaviors.setup(context -> new ExampleActor(context, name)); - } - - private final String name; - - private ExampleActor(ActorContext context, String name) { - super(context); - this.name = name; - } - - @Override - public Receive createReceive() { - return newReceiveBuilder() - .onMessage(ExampleMessage.class, this::onExampleMessage) - .build(); - } - - private Behavior onExampleMessage(ExampleMessage msg) { - getContext().getLog().info("I ({}) got a message: ExampleMessage({},{})", this.name, msg.someReference, msg.someString); - return this; - } -} diff --git a/src/main/java/com/example/ExampleTimerActor.java b/src/main/java/com/example/ExampleTimerActor.java deleted file mode 100644 index 0bffbea..0000000 --- a/src/main/java/com/example/ExampleTimerActor.java +++ /dev/null @@ -1,45 +0,0 @@ -package com.example; - -import akka.actor.typed.javadsl.TimerScheduler; -import akka.actor.typed.Behavior; -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 java.time.Duration; - - -public class ExampleTimerActor extends AbstractBehavior { - - public interface Message {}; - - - public record ExampleMessage(String someString) implements Message { } - - public static Behavior create() { - return Behaviors.setup(context -> Behaviors.withTimers(timers -> new ExampleTimerActor(context, timers))); - } - - private final TimerScheduler timers; - - private ExampleTimerActor(ActorContext context, TimerScheduler timers) { - super(context); - this.timers = timers; - - Message msg = new ExampleMessage("test123"); - this.timers.startSingleTimer(msg, msg, Duration.ofSeconds(10)); - } - - @Override - public Receive createReceive() { - return newReceiveBuilder() - .onMessage(ExampleMessage.class, this::onExampleMessage) - .build(); - } - - private Behavior onExampleMessage(ExampleMessage msg) { - getContext().getLog().info("I have send myself this message after 10 Seconds: {}", msg.someString); - return this; - } -} diff --git a/src/main/java/com/example/RelayBot.java b/src/main/java/com/example/RelayBot.java new file mode 100644 index 0000000..afac932 --- /dev/null +++ b/src/main/java/com/example/RelayBot.java @@ -0,0 +1,55 @@ +package com.example; + +import akka.actor.typed.ActorRef; +import akka.actor.typed.Behavior; +import akka.actor.typed.javadsl.*; + +import java.util.ArrayList; +import java.util.List; + +public class RelayBot extends AbstractBehavior { + + public interface Message {}; + + public record Hello(List> reihenfolge, int count) implements Message { } + public record Start(List> reihenfolge) implements Message { } + + public static Behavior create(String name) { + return Behaviors.setup(context -> new RelayBot(context, name)); + } + + private final String name; + + private RelayBot(ActorContext context, String name) { + super(context); + this.name = name; + } + + @Override + public Receive createReceive() { + return newReceiveBuilder() + .onMessage(Hello.class, this::onHello) + .onMessage(Start.class, this::onStart) + .build(); + } + + private Behavior onStart(Start msg) { + var next_hop = msg.reihenfolge.remove(0); + var new_reihenfolge = new ArrayList<>(msg.reihenfolge); + new_reihenfolge.add(next_hop); + next_hop.tell(new Hello(new_reihenfolge, 1)); + return this; + } + + private Behavior onHello(Hello msg) { + if (msg.count > 15) { + return Behaviors.stopped(); + } + getContext().getLog().info("I, {}, got Hello ({})", this.name, msg.count); + var next_hop = msg.reihenfolge.remove(0); + var new_reihenfolge = new ArrayList<>(msg.reihenfolge); + new_reihenfolge.add(next_hop); + next_hop.tell(new Hello(new_reihenfolge, msg.count + 1)); + return this; + } +}