Blatt 02
This commit is contained in:
parent
3672e114ed
commit
a21a0549cd
|
@ -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<AkkaMainSystem.Create> {
|
||||
|
||||
public static class Create {
|
||||
|
@ -24,11 +27,15 @@ public class AkkaMainSystem extends AbstractBehavior<AkkaMainSystem.Create> {
|
|||
|
||||
private Behavior<Create> onCreate(Create command) {
|
||||
//#create-actors
|
||||
ActorRef<ExampleActor.Message> a = this.getContext().spawn(ExampleActor.create("Alice"), "alice");
|
||||
ActorRef<ExampleTimerActor.Message> 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;
|
||||
}
|
||||
}
|
||||
|
|
|
@ -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;
|
||||
}
|
||||
}
|
|
@ -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;
|
||||
}
|
||||
}
|
|
@ -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<RelayBot.Message> {
|
||||
|
||||
public interface Message {};
|
||||
|
||||
public record Hello(List<ActorRef<RelayBot.Message>> reihenfolge, int count) implements Message { }
|
||||
public record Start(List<ActorRef<Message>> reihenfolge) implements Message { }
|
||||
|
||||
public static Behavior<Message> create(String name) {
|
||||
return Behaviors.setup(context -> new RelayBot(context, name));
|
||||
}
|
||||
|
||||
private final String name;
|
||||
|
||||
private RelayBot(ActorContext<Message> context, String name) {
|
||||
super(context);
|
||||
this.name = name;
|
||||
}
|
||||
|
||||
@Override
|
||||
public Receive<Message> createReceive() {
|
||||
return newReceiveBuilder()
|
||||
.onMessage(Hello.class, this::onHello)
|
||||
.onMessage(Start.class, this::onStart)
|
||||
.build();
|
||||
}
|
||||
|
||||
private Behavior<Message> 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<Message> 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;
|
||||
}
|
||||
}
|
Loading…
Reference in New Issue