This commit is contained in:
Christoph Stahl 2023-05-10 16:21:50 +02:00
parent 4d21a44a7e
commit 6613fc461c
4 changed files with 76 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<C.Message> c = this.getContext().spawn(C.create(), "c");
ActorRef<GCD.Message> gcd = this.getContext().spawn(GCD.create(), "gcd");
//#create-actors
a.tell(new ExampleActor.ExampleMessage(this.getContext().getSelf(),"Test123"));
gcd.tell(new GCD.GCDMessage(127, 74, c));
return this;
}
}

View File

@ -0,0 +1,34 @@
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<C.Message> {
public interface Message {};
public record CMessage(int i) implements Message { }
public static Behavior<Message> create() {
return Behaviors.setup(C::new);
}
private C(ActorContext<Message> context) {
super(context);
}
@Override
public Receive<Message> createReceive() {
return newReceiveBuilder()
.onMessage(CMessage.class, this::onCMessage)
.build();
}
private Behavior<Message> onCMessage(CMessage msg) {
getContext().getLog().info("{}", msg.i);
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,39 @@
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<C.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::onGCDMessage)
.build();
}
private Behavior<Message> onGCDMessage(GCDMessage msg) {
if(msg.b == 0) {
msg.cust.tell(new C.CMessage(msg.a));
} else {
this.getContext().getSelf().tell(new GCDMessage(msg.b, msg.a % msg.b, msg.cust));
}
return this;
}
}