Blatt 6
This commit is contained in:
parent
b0c7abc923
commit
bca86e4182
|
@ -4,31 +4,58 @@ import akka.actor.typed.ActorRef;
|
|||
import akka.actor.typed.Behavior;
|
||||
import akka.actor.typed.javadsl.*;
|
||||
|
||||
public class AkkaMainSystem extends AbstractBehavior<AkkaMainSystem.Create> {
|
||||
import java.time.Duration;
|
||||
|
||||
public static class Create {
|
||||
public class AkkaMainSystem extends AbstractBehavior<AkkaMainSystem.Message> {
|
||||
|
||||
private final TimerScheduler<Message> timers;
|
||||
private ActorRef<RelayActor.Message> alice;
|
||||
private ActorRef<RelayActor.Message> bob;
|
||||
private ActorRef<RelayActor.Message> charlie;
|
||||
|
||||
public interface Message { }
|
||||
public static class Create implements Message {}
|
||||
public enum sendTimeOut implements Message { INSTANCE }
|
||||
|
||||
public static Behavior<Message> create() {
|
||||
return Behaviors.setup(context -> Behaviors.withTimers(timers -> new AkkaMainSystem(context, timers)));
|
||||
}
|
||||
|
||||
public static Behavior<Create> create() {
|
||||
return Behaviors.setup(AkkaMainSystem::new);
|
||||
}
|
||||
|
||||
private AkkaMainSystem(ActorContext<Create> context) {
|
||||
private AkkaMainSystem(ActorContext<Message> context, TimerScheduler<Message> timers) {
|
||||
super(context);
|
||||
this.timers = timers;
|
||||
}
|
||||
|
||||
@Override
|
||||
public Receive<Create> createReceive() {
|
||||
return newReceiveBuilder().onMessage(Create.class, this::onCreate).build();
|
||||
public Receive<Message> createReceive() {
|
||||
return newReceiveBuilder()
|
||||
.onMessage(Create.class, this::onCreate)
|
||||
.onMessage(sendTimeOut.class, this::onSendTimeOut)
|
||||
.build();
|
||||
}
|
||||
|
||||
private Behavior<Create> onCreate(Create command) {
|
||||
private Behavior<Message> onSendTimeOut(sendTimeOut msg) {
|
||||
alice.tell(RelayActor.TimeOut.INSTANCE);
|
||||
bob.tell(RelayActor.TimeOut.INSTANCE);
|
||||
charlie.tell(RelayActor.TimeOut.INSTANCE);
|
||||
return this;
|
||||
}
|
||||
|
||||
private Behavior<Message> 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");
|
||||
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;
|
||||
}
|
||||
}
|
||||
|
|
|
@ -5,7 +5,7 @@ import akka.actor.typed.ActorSystem;
|
|||
import java.io.IOException;
|
||||
public class AkkaStart {
|
||||
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());
|
||||
|
||||
|
|
|
@ -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,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<RelayActor.Message> {
|
||||
|
||||
private ActorRef<Message> next;
|
||||
private ActorRef<Message> prev;
|
||||
|
||||
public interface Message {};
|
||||
|
||||
public record Init(ActorRef<RelayActor.Message> prev, ActorRef<RelayActor.Message> 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<Message> create() {
|
||||
return Behaviors.setup(context -> Behaviors.withTimers(timers -> new RelayActor(context, timers)));
|
||||
}
|
||||
|
||||
private final TimerScheduler<RelayActor.Message> timers;
|
||||
|
||||
private RelayActor(ActorContext<Message> context, TimerScheduler<RelayActor.Message> timers) {
|
||||
super(context);
|
||||
this.timers = timers;
|
||||
}
|
||||
|
||||
@Override
|
||||
public Receive<Message> 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<Message> onInit(Init msg) {
|
||||
this.next = msg.next;
|
||||
this.prev = msg.prev;
|
||||
return this;
|
||||
}
|
||||
private Behavior<Message> onClockWise(ClockWise msg) {
|
||||
this.getContext().getLog().info("Got Clockwise");
|
||||
this.timers.startSingleTimer(sendClockWise.INSTANCE, Duration.ofSeconds(3));
|
||||
return this;
|
||||
}
|
||||
|
||||
private Behavior<Message> onSendClockWise(sendClockWise msg) {
|
||||
this.next.tell(ClockWise.MSG);
|
||||
return this;
|
||||
}
|
||||
|
||||
private Behavior<Message> onCounterClockWise(CounterClockWise msg) {
|
||||
this.getContext().getLog().info("Got CounterClockwise");
|
||||
this.timers.startSingleTimer(sendCounterClockWise.INSTANCE, Duration.ofSeconds(5));
|
||||
return this;
|
||||
}
|
||||
|
||||
private Behavior<Message> onSendCounterClockWise(sendCounterClockWise msg) {
|
||||
this.prev.tell(CounterClockWise.MSG);
|
||||
return this;
|
||||
}
|
||||
|
||||
private Behavior<Message> onTimeOut(TimeOut msg) {
|
||||
this.getContext().getLog().info("TimeOut");
|
||||
return Behaviors.stopped();
|
||||
}
|
||||
}
|
Loading…
Reference in New Issue