diff --git a/src/main/java/com/example/AkkaMainSystem.java b/src/main/java/com/example/AkkaMainSystem.java index 7ddda70..966977a 100644 --- a/src/main/java/com/example/AkkaMainSystem.java +++ b/src/main/java/com/example/AkkaMainSystem.java @@ -24,10 +24,11 @@ public class AkkaMainSystem extends AbstractBehavior { private Behavior onCreate(Create command) { //#create-actors - ActorRef a = this.getContext().spawn(ExampleActor.create("Alice"), "alice"); + ActorRef c = this.getContext().spawn(C.create(), "c"); + ActorRef fak = this.getContext().spawn(Fak.create(), "fak"); //#create-actors - a.tell(new ExampleActor.ExampleMessage(this.getContext().getSelf(),"Test123")); + fak.tell(new Fak.FakMessage(5, c)); return this; } } diff --git a/src/main/java/com/example/C.java b/src/main/java/com/example/C.java new file mode 100644 index 0000000..5af5052 --- /dev/null +++ b/src/main/java/com/example/C.java @@ -0,0 +1,30 @@ +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 C extends AbstractBehavior { + + public static Behavior create() { + return Behaviors.setup(C::new); + } + + private C(ActorContext context) { + super(context); + } + + @Override + public Receive createReceive() { + return newReceiveBuilder() + .onMessage(FakCont.FakContMessage.class, this::onFakContMessage) + .build(); + } + + private Behavior onFakContMessage(FakCont.FakContMessage msg) { + getContext().getLog().info("{}", msg.arg()); + return this; + } +} 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/Fak.java b/src/main/java/com/example/Fak.java new file mode 100644 index 0000000..3914620 --- /dev/null +++ b/src/main/java/com/example/Fak.java @@ -0,0 +1,41 @@ +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 Fak extends AbstractBehavior { + + public interface Message {}; + + public record FakMessage(int val, ActorRef cust) implements Message { } + + public static Behavior create() { + return Behaviors.setup(Fak::new); + } + + + private Fak(ActorContext context) { + super(context); + } + + @Override + public Receive createReceive() { + return newReceiveBuilder() + .onMessage(FakMessage.class, this::onFakMessage) + .build(); + } + + private Behavior onFakMessage(FakMessage msg) { + if(msg.val == 0) { + msg.cust.tell(new FakCont.FakContMessage(1)); + } else { + ActorRef cont = this.getContext().spawnAnonymous(FakCont.create(msg.val, msg.cust)); + this.getContext().getSelf().tell(new FakMessage(msg.val - 1, cont)); + } + return this; + } +} diff --git a/src/main/java/com/example/FakCont.java b/src/main/java/com/example/FakCont.java new file mode 100644 index 0000000..5f8b8ab --- /dev/null +++ b/src/main/java/com/example/FakCont.java @@ -0,0 +1,42 @@ +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 FakCont extends AbstractBehavior { + + private final int val; + private final ActorRef cust; + + public interface Message {}; + + public record FakContMessage(int arg) implements Message { } + + public static Behavior create(int val, ActorRef cust) { + return Behaviors.setup(context -> new FakCont(context, val, cust)); + } + + + + private FakCont(ActorContext context, int val, ActorRef cust) { + super(context); + this.val = val; + this.cust = cust; + } + + @Override + public Receive createReceive() { + return newReceiveBuilder() + .onMessage(FakContMessage.class, this::onFakContMessage) + .build(); + } + + private Behavior onFakContMessage(FakContMessage msg) { + this.cust.tell(new FakContMessage(this.val * msg.arg)); + return this; + } +}