Compare commits

...

1 Commits

Author SHA1 Message Date
Christoph Stahl d1eecffcb4 Factorial 2023-05-10 15:56:27 +02:00
5 changed files with 116 additions and 37 deletions

View File

@ -24,10 +24,11 @@ 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<FakCont.Message> c = this.getContext().spawn(C.create(), "c");
ActorRef<Fak.Message> 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;
}
}

View File

@ -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<FakCont.Message> {
public static Behavior<FakCont.Message> create() {
return Behaviors.setup(C::new);
}
private C(ActorContext<FakCont.Message> context) {
super(context);
}
@Override
public Receive<FakCont.Message> createReceive() {
return newReceiveBuilder()
.onMessage(FakCont.FakContMessage.class, this::onFakContMessage)
.build();
}
private Behavior<FakCont.Message> onFakContMessage(FakCont.FakContMessage msg) {
getContext().getLog().info("{}", msg.arg());
return this;
}
}

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

@ -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<Fak.Message> {
public interface Message {};
public record FakMessage(int val, ActorRef<FakCont.Message> cust) implements Message { }
public static Behavior<Message> create() {
return Behaviors.setup(Fak::new);
}
private Fak(ActorContext<Message> context) {
super(context);
}
@Override
public Receive<Message> createReceive() {
return newReceiveBuilder()
.onMessage(FakMessage.class, this::onFakMessage)
.build();
}
private Behavior<Message> onFakMessage(FakMessage msg) {
if(msg.val == 0) {
msg.cust.tell(new FakCont.FakContMessage(1));
} else {
ActorRef<FakCont.Message> cont = this.getContext().spawnAnonymous(FakCont.create(msg.val, msg.cust));
this.getContext().getSelf().tell(new FakMessage(msg.val - 1, cont));
}
return this;
}
}

View File

@ -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<FakCont.Message> {
private final int val;
private final ActorRef<Message> cust;
public interface Message {};
public record FakContMessage(int arg) implements Message { }
public static Behavior<Message> create(int val, ActorRef<Message> cust) {
return Behaviors.setup(context -> new FakCont(context, val, cust));
}
private FakCont(ActorContext<Message> context, int val, ActorRef<Message> cust) {
super(context);
this.val = val;
this.cust = cust;
}
@Override
public Receive<Message> createReceive() {
return newReceiveBuilder()
.onMessage(FakContMessage.class, this::onFakContMessage)
.build();
}
private Behavior<Message> onFakContMessage(FakContMessage msg) {
this.cust.tell(new FakContMessage(this.val * msg.arg));
return this;
}
}