This commit is contained in:
Egor Rutkowski 2024-07-01 15:02:13 +02:00
parent 0ee16028d7
commit cfcebd59c9
3 changed files with 49 additions and 4 deletions

View File

@ -0,0 +1,39 @@
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 Actor extends AbstractBehavior<Actor.Message> {
String name;
Expression expression;
public interface Message{}
public record StartMessage(Expression expression) implements Message{}
private Actor(ActorContext<Message> context, String name, Expression expression){
super(context);
this.name = name;
this.expression = expression;
}
public static Behavior<Actor.Message> create(String name,Expression expression){
return Behaviors.setup(context -> new Actor(context, name, expression));
}
@Override
public Receive<Actor.Message> createReceive(){
return newReceiveBuilder()
//.onMessage(StartMessage.class, this::onStartMessage)
.build();
}
private Behavior<Message> onStartMessage(Expression expression){
this.expression = expression;
return this;
}
}

View File

@ -2,7 +2,10 @@ package com.example;
import akka.actor.typed.ActorRef; import akka.actor.typed.ActorRef;
import akka.actor.typed.Behavior; import akka.actor.typed.Behavior;
import akka.actor.typed.javadsl.*; 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 AkkaMainSystem extends AbstractBehavior<AkkaMainSystem.Create> { public class AkkaMainSystem extends AbstractBehavior<AkkaMainSystem.Create> {
@ -26,9 +29,12 @@ public class AkkaMainSystem extends AbstractBehavior<AkkaMainSystem.Create> {
//#create-actors //#create-actors
ActorRef<ExampleActor.Message> a = this.getContext().spawn(ExampleActor.create("Alice"), "alice"); ActorRef<ExampleActor.Message> a = this.getContext().spawn(ExampleActor.create("Alice"), "alice");
ActorRef<ExampleTimerActor.Message> b = this.getContext().spawn(ExampleTimerActor.create(), "timeractor"); ActorRef<ExampleTimerActor.Message> b = this.getContext().spawn(ExampleTimerActor.create(), "timeractor");
//#create-actors
a.tell(new ExampleActor.ExampleMessage(this.getContext().getSelf(),"Test123")); //#create-actors
Expression expression = Expression.generateExpression(6, 9);
ActorRef<Actor.Message> test = this.getContext().spawn(Actor.create("Seng", expression), "Sengmann");
System.out.println(expression.toString()+ " Wert:" +expression.eval());
return this; return this;
} }
} }