Blatt 03
This commit is contained in:
parent
3672e114ed
commit
b5517e4312
|
@ -23,12 +23,21 @@ 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");
|
||||
//#create-actors
|
||||
// Factorial
|
||||
var factorialReceiver = this.getContext().spawn(FactorialReceiver.create(), "FactorialReceiver");
|
||||
var factorial = this.getContext().spawn(Factorial.create(), "Factorial");
|
||||
factorial.tell(new Factorial.FactorialMsg(5, factorialReceiver));
|
||||
|
||||
// GCD
|
||||
var gcdReceiver = this.getContext().spawn(GCDReceiver.create(), "GCDReceiver");
|
||||
var gcd = this.getContext().spawn(GCD.create(), "GCD");
|
||||
gcd.tell(new GCD.GCDMessage(127, 74, gcdReceiver));
|
||||
|
||||
// Fibonacci
|
||||
var fibonacciReceiver = this.getContext().spawn(FibonacciReceiver.create(), "FibonacciReceiver");
|
||||
var fibonacci = this.getContext().spawn(Fibonacci.create(), "Fibonacci");
|
||||
fibonacci.tell(new Fibonacci.FibonacciMessage(7, fibonacciReceiver));
|
||||
|
||||
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,43 @@
|
|||
package com.example;
|
||||
|
||||
import akka.actor.typed.ActorRef;
|
||||
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.util.UUID;
|
||||
|
||||
public class Factorial extends AbstractBehavior<Factorial.Message> {
|
||||
|
||||
public interface Message {};
|
||||
|
||||
public record FactorialMsg(int val, ActorRef<FactorialCont.Message> cust) implements Message { }
|
||||
|
||||
public static Behavior<Message> create() {
|
||||
return Behaviors.setup(Factorial::new);
|
||||
}
|
||||
|
||||
private Factorial(ActorContext<Message> context) {
|
||||
super(context);
|
||||
}
|
||||
|
||||
@Override
|
||||
public Receive<Message> createReceive() {
|
||||
return newReceiveBuilder()
|
||||
.onMessage(FactorialMsg.class, this::onMsg)
|
||||
.build();
|
||||
}
|
||||
|
||||
private Behavior<Message> onMsg(FactorialMsg msg) {
|
||||
if (msg.val == 0){
|
||||
msg.cust.tell(new FactorialCont.FactorialContMsg(1));
|
||||
} else {
|
||||
var cont = this.getContext().spawn(FactorialCont.create(msg.val, msg.cust), UUID.randomUUID().toString());
|
||||
this.getContext().getSelf().tell(new FactorialMsg(msg.val - 1, cont));
|
||||
}
|
||||
|
||||
return this;
|
||||
}
|
||||
}
|
|
@ -0,0 +1,40 @@
|
|||
package com.example;
|
||||
|
||||
import akka.actor.typed.ActorRef;
|
||||
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;
|
||||
|
||||
public class FactorialCont extends AbstractBehavior<FactorialCont.Message> {
|
||||
|
||||
private final ActorRef<Message> cust;
|
||||
private final int val;
|
||||
|
||||
public interface Message {};
|
||||
|
||||
public record FactorialContMsg(int arg) implements Message { }
|
||||
|
||||
public static Behavior<Message> create(int val, ActorRef<FactorialCont.Message> cust) {
|
||||
return Behaviors.setup(context -> new FactorialCont(context, val, cust));
|
||||
}
|
||||
|
||||
private FactorialCont(ActorContext<Message> context, int val, ActorRef<Message> cust) {
|
||||
super(context);
|
||||
this.val = val;
|
||||
this.cust = cust;
|
||||
}
|
||||
|
||||
@Override
|
||||
public Receive<Message> createReceive() {
|
||||
return newReceiveBuilder()
|
||||
.onMessage(FactorialContMsg.class, this::onMsg)
|
||||
.build();
|
||||
}
|
||||
|
||||
private Behavior<Message> onMsg(FactorialContMsg msg) {
|
||||
this.cust.tell(new FactorialContMsg(this.val * msg.arg));
|
||||
return this;
|
||||
}
|
||||
}
|
|
@ -0,0 +1,31 @@
|
|||
package com.example;
|
||||
|
||||
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;
|
||||
|
||||
public class FactorialReceiver extends AbstractBehavior<FactorialCont.Message> {
|
||||
|
||||
public static Behavior<FactorialCont.Message> create() {
|
||||
return Behaviors.setup(FactorialReceiver::new);
|
||||
}
|
||||
|
||||
|
||||
private FactorialReceiver(ActorContext<FactorialCont.Message> context) {
|
||||
super(context);
|
||||
}
|
||||
|
||||
@Override
|
||||
public Receive<FactorialCont.Message> createReceive() {
|
||||
return newReceiveBuilder()
|
||||
.onMessage(FactorialCont.FactorialContMsg.class, this::onMsg)
|
||||
.build();
|
||||
}
|
||||
|
||||
private Behavior<FactorialCont.Message> onMsg(FactorialCont.FactorialContMsg msg) {
|
||||
this.getContext().getLog().info("Factorial: {}", msg.arg());
|
||||
return this;
|
||||
}
|
||||
}
|
|
@ -0,0 +1,44 @@
|
|||
package com.example;
|
||||
|
||||
import akka.actor.typed.ActorRef;
|
||||
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.util.UUID;
|
||||
|
||||
public class Fibonacci extends AbstractBehavior<Fibonacci.Message> {
|
||||
|
||||
public interface Message {};
|
||||
|
||||
public record FibonacciMessage(int n, ActorRef<FibonacciCont.Message> cust) implements Message { }
|
||||
|
||||
public static Behavior<Message> create() {
|
||||
return Behaviors.setup(Fibonacci::new);
|
||||
}
|
||||
|
||||
|
||||
private Fibonacci(ActorContext<Message> context) {
|
||||
super(context);
|
||||
}
|
||||
|
||||
@Override
|
||||
public Receive<Message> createReceive() {
|
||||
return newReceiveBuilder()
|
||||
.onMessage(FibonacciMessage.class, this::onExampleMessage)
|
||||
.build();
|
||||
}
|
||||
|
||||
private Behavior<Message> onExampleMessage(FibonacciMessage msg) {
|
||||
if (msg.n <= 1) {
|
||||
msg.cust.tell(new FibonacciCont.FibonacciContMessage(msg.n));
|
||||
} else {
|
||||
var cont = this.getContext().spawn(FibonacciCont.create(-1, msg.cust), UUID.randomUUID().toString());
|
||||
this.getContext().getSelf().tell(new FibonacciMessage(msg.n - 1, cont));
|
||||
this.getContext().getSelf().tell(new FibonacciMessage(msg.n - 2, cont));
|
||||
}
|
||||
return this;
|
||||
}
|
||||
}
|
|
@ -0,0 +1,45 @@
|
|||
package com.example;
|
||||
|
||||
import akka.actor.typed.ActorRef;
|
||||
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;
|
||||
|
||||
public class FibonacciCont extends AbstractBehavior<FibonacciCont.Message> {
|
||||
|
||||
private final ActorRef<Message> cust;
|
||||
|
||||
public interface Message {};
|
||||
|
||||
public record FibonacciContMessage(int result) implements Message { }
|
||||
|
||||
public static Behavior<Message> create(int first_value, ActorRef<Message> cust) {
|
||||
return Behaviors.setup(context -> new FibonacciCont(context, first_value, cust));
|
||||
}
|
||||
|
||||
private final int first_value;
|
||||
|
||||
private FibonacciCont(ActorContext<Message> context, int first_value, ActorRef<Message> cust) {
|
||||
super(context);
|
||||
this.first_value = first_value;
|
||||
this.cust = cust;
|
||||
}
|
||||
|
||||
@Override
|
||||
public Receive<Message> createReceive() {
|
||||
return newReceiveBuilder()
|
||||
.onMessage(FibonacciContMessage.class, this::onExampleMessage)
|
||||
.build();
|
||||
}
|
||||
|
||||
private Behavior<Message> onExampleMessage(FibonacciContMessage msg) {
|
||||
if (this.first_value == -1) {
|
||||
return FibonacciCont.create(msg.result, this.cust);
|
||||
} else {
|
||||
this.cust.tell(new FibonacciContMessage(msg.result + this.first_value));
|
||||
}
|
||||
return this;
|
||||
}
|
||||
}
|
|
@ -0,0 +1,32 @@
|
|||
package com.example;
|
||||
|
||||
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;
|
||||
|
||||
public class FibonacciReceiver extends AbstractBehavior<FibonacciCont.Message> {
|
||||
|
||||
|
||||
public static Behavior<FibonacciCont.Message> create() {
|
||||
return Behaviors.setup(FibonacciReceiver::new);
|
||||
}
|
||||
|
||||
|
||||
private FibonacciReceiver(ActorContext<FibonacciCont.Message> context) {
|
||||
super(context);
|
||||
}
|
||||
|
||||
@Override
|
||||
public Receive<FibonacciCont.Message> createReceive() {
|
||||
return newReceiveBuilder()
|
||||
.onMessage(FibonacciCont.FibonacciContMessage.class, this::onExampleMessage)
|
||||
.build();
|
||||
}
|
||||
|
||||
private Behavior<FibonacciCont.Message> onExampleMessage(FibonacciCont.FibonacciContMessage msg) {
|
||||
getContext().getLog().info("Fibonacci: {}",msg.result());
|
||||
return this;
|
||||
}
|
||||
}
|
|
@ -0,0 +1,40 @@
|
|||
package com.example;
|
||||
|
||||
import akka.actor.typed.ActorRef;
|
||||
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;
|
||||
|
||||
public class GCD extends AbstractBehavior<GCD.Message> {
|
||||
|
||||
public interface Message {};
|
||||
|
||||
public record GCDMessage(int a, int b, ActorRef<GCDReceiver.Message> cust) implements Message { }
|
||||
|
||||
public static Behavior<Message> create() {
|
||||
return Behaviors.setup(GCD::new);
|
||||
}
|
||||
|
||||
|
||||
private GCD(ActorContext<Message> context) {
|
||||
super(context);
|
||||
}
|
||||
|
||||
@Override
|
||||
public Receive<Message> createReceive() {
|
||||
return newReceiveBuilder()
|
||||
.onMessage(GCDMessage.class, this::onMsg)
|
||||
.build();
|
||||
}
|
||||
|
||||
private Behavior<Message> onMsg(GCDMessage msg) {
|
||||
if (msg.b == 0) {
|
||||
msg.cust.tell(new GCDReceiver.GCDResult(msg.a));
|
||||
} else {
|
||||
this.getContext().getSelf().tell(new GCDMessage(msg.b, msg.a % msg.b, msg.cust));
|
||||
}
|
||||
return this;
|
||||
}
|
||||
}
|
|
@ -0,0 +1,35 @@
|
|||
package com.example;
|
||||
|
||||
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;
|
||||
|
||||
public class GCDReceiver extends AbstractBehavior<GCDReceiver.Message> {
|
||||
|
||||
public interface Message {};
|
||||
|
||||
public record GCDResult(int result) implements Message { }
|
||||
|
||||
public static Behavior<Message> create() {
|
||||
return Behaviors.setup(GCDReceiver::new);
|
||||
}
|
||||
|
||||
|
||||
private GCDReceiver(ActorContext<Message> context) {
|
||||
super(context);
|
||||
}
|
||||
|
||||
@Override
|
||||
public Receive<Message> createReceive() {
|
||||
return newReceiveBuilder()
|
||||
.onMessage(GCDResult.class, this::onResult)
|
||||
.build();
|
||||
}
|
||||
|
||||
private Behavior<Message> onResult(GCDResult msg) {
|
||||
getContext().getLog().info("GCD: {}", msg.result);
|
||||
return this;
|
||||
}
|
||||
}
|
Loading…
Reference in New Issue