I want to create a GuardianActor with the capability of spawning actors. I’m confused how this actor should be created. Currently I’m trying to do something like this →
public class GuardianActor extends AbstractBehavior<SpawnProtocol.Command>{
private GuardianActor(ActorContext<SpawnProtocol.Command> context){
super(context);
}
public static Behavior<SpawnProtocol.Command> createRootBehavior() {
return Behaviors.setup(context -> {
// some initial tasks
return new GuardianActor(context);
});
@Override
public Receive<SpawnProtocol.Command> createReceive() {
return newReceiveBuilder().onMessage(SpawnProtocol.Command.class, this::onSpawnProtocolCommand).build();
}
private Behavior<SpawnProtocol.Command> onSpawnProtocolCommand(SpawnProtocol.Command msg) {
// what do i do here?
return this;
}
}
Here antyime a SpawnProtocol.Spawn<>
message is sent to the GuardianActor it is intercepted by the method specified in createReceive
. However if I simply return SpawnProtocol.create()
from the createRootBehavior
then the creation of the actor just takes place on its own. What is the correct pattern to follow ?