From bca86e4182f13ac2e71837de90b8e7ff4b3f9992 Mon Sep 17 00:00:00 2001 From: Christoph Stahl Date: Wed, 17 May 2023 14:46:29 +0200 Subject: [PATCH] Blatt 6 --- src/main/java/com/example/AkkaMainSystem.java | 53 +++++++++--- src/main/java/com/example/AkkaStart.java | 2 +- src/main/java/com/example/ExampleActor.java | 35 -------- .../java/com/example/ExampleTimerActor.java | 45 ----------- src/main/java/com/example/RelayActor.java | 81 +++++++++++++++++++ 5 files changed, 122 insertions(+), 94 deletions(-) delete mode 100644 src/main/java/com/example/ExampleActor.java delete mode 100644 src/main/java/com/example/ExampleTimerActor.java create mode 100644 src/main/java/com/example/RelayActor.java diff --git a/src/main/java/com/example/AkkaMainSystem.java b/src/main/java/com/example/AkkaMainSystem.java index 868351b..a80e07d 100644 --- a/src/main/java/com/example/AkkaMainSystem.java +++ b/src/main/java/com/example/AkkaMainSystem.java @@ -4,31 +4,58 @@ import akka.actor.typed.ActorRef; import akka.actor.typed.Behavior; import akka.actor.typed.javadsl.*; -public class AkkaMainSystem extends AbstractBehavior { +import java.time.Duration; - public static class Create { +public class AkkaMainSystem extends AbstractBehavior { + + private final TimerScheduler timers; + private ActorRef alice; + private ActorRef bob; + private ActorRef charlie; + + public interface Message { } + public static class Create implements Message {} + public enum sendTimeOut implements Message { INSTANCE } + + public static Behavior create() { + return Behaviors.setup(context -> Behaviors.withTimers(timers -> new AkkaMainSystem(context, timers))); } - public static Behavior create() { - return Behaviors.setup(AkkaMainSystem::new); - } - - private AkkaMainSystem(ActorContext context) { + private AkkaMainSystem(ActorContext context, TimerScheduler timers) { super(context); + this.timers = timers; } @Override - public Receive createReceive() { - return newReceiveBuilder().onMessage(Create.class, this::onCreate).build(); + public Receive createReceive() { + return newReceiveBuilder() + .onMessage(Create.class, this::onCreate) + .onMessage(sendTimeOut.class, this::onSendTimeOut) + .build(); } - private Behavior onCreate(Create command) { + private Behavior onSendTimeOut(sendTimeOut msg) { + alice.tell(RelayActor.TimeOut.INSTANCE); + bob.tell(RelayActor.TimeOut.INSTANCE); + charlie.tell(RelayActor.TimeOut.INSTANCE); + return this; + } + + private Behavior onCreate(Create command) { //#create-actors - ActorRef a = this.getContext().spawn(ExampleActor.create("Alice"), "alice"); - ActorRef b = this.getContext().spawn(ExampleTimerActor.create(), "timeractor"); + alice = this.getContext().spawn(RelayActor.create(), "alice"); + bob = this.getContext().spawn(RelayActor.create(), "bob"); + charlie = this.getContext().spawn(RelayActor.create(), "charlie"); //#create-actors - a.tell(new ExampleActor.ExampleMessage(this.getContext().getSelf(),"Test123")); + alice.tell(new RelayActor.Init(charlie, bob)); + bob.tell(new RelayActor.Init(alice, charlie)); + charlie.tell(new RelayActor.Init(bob, alice)); + + timers.startSingleTimer(sendTimeOut.INSTANCE, Duration.ofSeconds(60)); + + alice.tell(RelayActor.ClockWise.MSG); + alice.tell(RelayActor.CounterClockWise.MSG); return this; } } diff --git a/src/main/java/com/example/AkkaStart.java b/src/main/java/com/example/AkkaStart.java index 45339c2..19ed5ac 100644 --- a/src/main/java/com/example/AkkaStart.java +++ b/src/main/java/com/example/AkkaStart.java @@ -5,7 +5,7 @@ import akka.actor.typed.ActorSystem; import java.io.IOException; public class AkkaStart { public static void main(String[] args) { - final ActorSystem messageMain = ActorSystem.create(AkkaMainSystem.create(), "akkaMainSystem"); + final ActorSystem messageMain = ActorSystem.create(AkkaMainSystem.create(), "akkaMainSystem"); messageMain.tell(new AkkaMainSystem.Create()); 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/RelayActor.java b/src/main/java/com/example/RelayActor.java new file mode 100644 index 0000000..80c4c57 --- /dev/null +++ b/src/main/java/com/example/RelayActor.java @@ -0,0 +1,81 @@ +package com.example; + +import akka.actor.typed.ActorRef; +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 RelayActor extends AbstractBehavior { + + private ActorRef next; + private ActorRef prev; + + public interface Message {}; + + public record Init(ActorRef prev, ActorRef next) implements Message {} + public enum ClockWise implements Message { MSG } + public enum CounterClockWise implements Message { MSG } + public enum sendClockWise implements Message { INSTANCE } + public enum sendCounterClockWise implements Message { INSTANCE } + public enum TimeOut implements Message {INSTANCE} + public static Behavior create() { + return Behaviors.setup(context -> Behaviors.withTimers(timers -> new RelayActor(context, timers))); + } + + private final TimerScheduler timers; + + private RelayActor(ActorContext context, TimerScheduler timers) { + super(context); + this.timers = timers; + } + + @Override + public Receive createReceive() { + return newReceiveBuilder() + .onMessage(Init.class, this::onInit) + .onMessage(ClockWise.class, this::onClockWise) + .onMessage(CounterClockWise.class, this::onCounterClockWise) + .onMessage(sendClockWise.class, this::onSendClockWise) + .onMessage(sendCounterClockWise.class, this::onSendCounterClockWise) + .onMessage(TimeOut.class, this::onTimeOut) + .build(); + } + + private Behavior onInit(Init msg) { + this.next = msg.next; + this.prev = msg.prev; + return this; + } + private Behavior onClockWise(ClockWise msg) { + this.getContext().getLog().info("Got Clockwise"); + this.timers.startSingleTimer(sendClockWise.INSTANCE, Duration.ofSeconds(3)); + return this; + } + + private Behavior onSendClockWise(sendClockWise msg) { + this.next.tell(ClockWise.MSG); + return this; + } + + private Behavior onCounterClockWise(CounterClockWise msg) { + this.getContext().getLog().info("Got CounterClockwise"); + this.timers.startSingleTimer(sendCounterClockWise.INSTANCE, Duration.ofSeconds(5)); + return this; + } + + private Behavior onSendCounterClockWise(sendCounterClockWise msg) { + this.prev.tell(CounterClockWise.MSG); + return this; + } + + private Behavior onTimeOut(TimeOut msg) { + this.getContext().getLog().info("TimeOut"); + return Behaviors.stopped(); + } +}