This commit is contained in:
Christoph Stahl 2024-05-09 18:24:41 +02:00
parent 3672e114ed
commit 6b021f335c
6 changed files with 174 additions and 95 deletions

41
README.md Normal file
View File

@ -0,0 +1,41 @@
# Blatt 04 - Timed Messages
In diesem Blatt ging es allen voran, um die möglichkeit Zeitgesteuert Nachrichten zu senden. Möglich macht es der
`TimerScheduler`.
Damit ein Aktor zugriff auf einen `TimerScheduler` hat, muss der Aktor damit erstellt werden. Anstelle dem _regulärem_
```java
public static Behavior<Message> create(... arguments ...) {
return Behaviors.setup(context -> new Actor(context, ... arguments ...));
}
```
muss ein Aktor nun mit `Timers` erstellt werden.
```java
public static Behavior<Message> create(... arguments ...) {
return Behaviors.setup(context -> Behaviors.withTimers(timers -> new Actor(context, timers, ... arguments ...)));
}
```
Entsprechend muss im Konstruktor dann auch an der entsprechenden Stelle ein `TimerScheduler` erwartet werden.
```java
private Actor(ActorContext<Message> context, TimerScheduler<Message> timers, ... arguments ...) {
super(context);
this.timers = timers; // wir speichern uns einfach den TimerScheduler
// restliche argumente setzen
}
```
Nun kann der `TimerScheduler` genutzt werden. Grundsätzlich wird mit dem `TimerScheduler` eine Nachricht nach einer
bestimmten Zeit **an sich selbst** gesendet. Diese muss (wie jede Nachricht) dann auch behandelt werden.
Der Code
```java
this.timers.startSingleTimer(new Message(), Duration.ofSeconds(10));
```
sendet eine Nachricht (`Message`) nach 10 Sekunden an sich selbst.
`TimerScheduler` können noch mehr, aber das ist erstmal alles, was zum Verständnis der Aufgabe gebraucht wird.

View File

@ -4,31 +4,62 @@ import akka.actor.typed.ActorRef;
import akka.actor.typed.Behavior; import akka.actor.typed.Behavior;
import akka.actor.typed.javadsl.*; import akka.actor.typed.javadsl.*;
public class AkkaMainSystem extends AbstractBehavior<AkkaMainSystem.Create> { import java.time.Duration;
import java.util.ArrayList;
public static class Create { public class AkkaMainSystem extends AbstractBehavior<AkkaMainSystem.Message> {
public interface Message { }
public static class Create implements Message { }
public static class Terminate implements Message { }
public static Behavior<Message> create() {
return Behaviors.setup(context -> Behaviors.withTimers(timers -> new AkkaMainSystem(context, timers)));
} }
public static Behavior<Create> create() { private final TimerScheduler<Message> timers;
return Behaviors.setup(AkkaMainSystem::new); private ArrayList<ActorRef<TimerActor.Message>> all_actors;
}
private AkkaMainSystem(ActorContext<Create> context) { private AkkaMainSystem(ActorContext<Message> context, TimerScheduler<Message> timers) {
super(context); super(context);
this.timers = timers;
} }
@Override @Override
public Receive<Create> createReceive() { public Receive<Message> createReceive() {
return newReceiveBuilder().onMessage(Create.class, this::onCreate).build(); return newReceiveBuilder().onMessage(Create.class, this::onCreate).onMessage(Terminate.class, this::onTerminate).build();
} }
private Behavior<Create> onCreate(Create command) { private Behavior<Message> onTerminate(Terminate msg) {
//#create-actors for (ActorRef<TimerActor.Message> actor : all_actors) {
ActorRef<ExampleActor.Message> a = this.getContext().spawn(ExampleActor.create("Alice"), "alice"); actor.tell(new TimerActor.Terminate());
ActorRef<ExampleTimerActor.Message> b = this.getContext().spawn(ExampleTimerActor.create(), "timeractor"); }
//#create-actors return this;
}
private Behavior<Message> onCreate(Create command) {
var alice = this.getContext().spawn(TimerActor.create("alice"), "alice");
var bob = this.getContext().spawn(TimerActor.create("bob"), "bob");
var charlie = this.getContext().spawn(TimerActor.create("charlie"), "charlie");
ArrayList<ActorRef<TimerActor.Message>> clock_reihenfolge = new ArrayList<>();
clock_reihenfolge.add(bob);
clock_reihenfolge.add(charlie);
clock_reihenfolge.add(alice);
ArrayList<ActorRef<TimerActor.Message>> counterclock_reihenfolge = new ArrayList<>();
counterclock_reihenfolge.add(bob);
counterclock_reihenfolge.add(charlie);
counterclock_reihenfolge.add(alice);
this.all_actors = new ArrayList<>(clock_reihenfolge);
alice.tell(new TimerActor.Clockwise(clock_reihenfolge));
alice.tell(new TimerActor.CounterClockwise(counterclock_reihenfolge));
timers.startSingleTimer(new Terminate(), Duration.ofSeconds(60));
a.tell(new ExampleActor.ExampleMessage(this.getContext().getSelf(),"Test123"));
return this; return this;
} }
} }

View File

@ -5,7 +5,7 @@ import akka.actor.typed.ActorSystem;
import java.io.IOException; import java.io.IOException;
public class AkkaStart { public class AkkaStart {
public static void main(String[] args) { public static void main(String[] args) {
final ActorSystem<AkkaMainSystem.Create> messageMain = ActorSystem.create(AkkaMainSystem.create(), "akkaMainSystem"); final ActorSystem<AkkaMainSystem.Message> messageMain = ActorSystem.create(AkkaMainSystem.create(), "akkaMainSystem");
messageMain.tell(new AkkaMainSystem.Create()); messageMain.tell(new AkkaMainSystem.Create());

View File

@ -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<ExampleActor.Message> {
public interface Message {};
public record ExampleMessage(ActorRef<AkkaMainSystem.Create> someReference, String someString) implements Message { }
public static Behavior<Message> create(String name) {
return Behaviors.setup(context -> new ExampleActor(context, name));
}
private final String name;
private ExampleActor(ActorContext<Message> context, String name) {
super(context);
this.name = name;
}
@Override
public Receive<Message> createReceive() {
return newReceiveBuilder()
.onMessage(ExampleMessage.class, this::onExampleMessage)
.build();
}
private Behavior<Message> onExampleMessage(ExampleMessage msg) {
getContext().getLog().info("I ({}) got a message: ExampleMessage({},{})", this.name, msg.someReference, msg.someString);
return this;
}
}

View File

@ -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<ExampleTimerActor.Message> {
public interface Message {};
public record ExampleMessage(String someString) implements Message { }
public static Behavior<Message> create() {
return Behaviors.setup(context -> Behaviors.withTimers(timers -> new ExampleTimerActor(context, timers)));
}
private final TimerScheduler<ExampleTimerActor.Message> timers;
private ExampleTimerActor(ActorContext<Message> context, TimerScheduler<ExampleTimerActor.Message> timers) {
super(context);
this.timers = timers;
Message msg = new ExampleMessage("test123");
this.timers.startSingleTimer(msg, msg, Duration.ofSeconds(10));
}
@Override
public Receive<Message> createReceive() {
return newReceiveBuilder()
.onMessage(ExampleMessage.class, this::onExampleMessage)
.build();
}
private Behavior<Message> onExampleMessage(ExampleMessage msg) {
getContext().getLog().info("I have send myself this message after 10 Seconds: {}", msg.someString);
return this;
}
}

View File

@ -0,0 +1,87 @@
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;
import java.util.ArrayList;
public class TimerActor extends AbstractBehavior<TimerActor.Message> {
public interface Message {};
public record Clockwise(ArrayList<ActorRef<Message>> reihenfolge) implements Message {}
public record CounterClockwise(ArrayList<ActorRef<Message>> reihenfolge) implements Message {}
public record Terminate() implements Message {}
public record Clockwise_wait(ArrayList<ActorRef<Message>> reihenfolge) implements Message {}
public record CounterClockwise_wait(ArrayList<ActorRef<Message>> reihenfolge) implements Message {}
public static Behavior<Message> create(String name) {
return Behaviors.setup(context -> Behaviors.withTimers(timers -> new TimerActor(context, timers, name)));
}
private final TimerScheduler<TimerActor.Message> timers;
private final String name;
private TimerActor(ActorContext<Message> context, TimerScheduler<Message> timers, String name) {
super(context);
this.timers = timers;
this.name = name;
}
@Override
public Receive<Message> createReceive() {
return newReceiveBuilder()
.onMessage(Clockwise.class, this::onClockwise)
.onMessage(CounterClockwise.class, this::onCounterClockwise)
.onMessage(Clockwise_wait.class, this::onClockwise_wait)
.onMessage(CounterClockwise_wait.class, this::onCounterClockwise_wait)
.onMessage(Terminate.class, this::onTerminate)
.build();
}
private Behavior<Message> onClockwise(Clockwise msg) {
getContext().getLog().info("Clockwise erhalten");
this.timers.startSingleTimer(new Clockwise_wait(msg.reihenfolge), Duration.ofSeconds(3));
return this;
}
private Behavior<Message> onClockwise_wait(Clockwise_wait msg) {
getContext().getLog().info("Clockwise senden");
var new_reihenfolge = msg.reihenfolge;
var next = new_reihenfolge.remove(0);
new_reihenfolge.add(next);
next.tell(new Clockwise(new_reihenfolge));
return this;
}
private Behavior<Message> onCounterClockwise(CounterClockwise msg) {
getContext().getLog().info("CounterClockwise erhalten");
this.timers.startSingleTimer(new CounterClockwise_wait(msg.reihenfolge), Duration.ofSeconds(5));
return this;
}
private Behavior<Message> onCounterClockwise_wait(CounterClockwise_wait msg) {
getContext().getLog().info("CounterClockwise senden");
var new_reihenfolge = msg.reihenfolge;
var next = new_reihenfolge.remove(0);
new_reihenfolge.add(next);
next.tell(new CounterClockwise(new_reihenfolge));
return this;
}
private Behavior<Message> onTerminate(Terminate msg) {
getContext().getLog().info("Terminate");
return Behaviors.stopped();
}
}