Uses Spawn Protocol to create Akka's Actors from outside. It can be applied at any level of the actors' hierarchy.
I was inspired by Akka's spawn protocol, but I think that any actor could be a spawner if the actor implements a method to spawn the child actors that is why I decided to extend the initial idea.
dependencies {
implementation(
'me.dmmax.akka:akka-spawn-protocol:1.1'
)
// ... other dependencies
}
As an alternative approach you can use BOM to manage the spawn protocol versions in one place
dependencies {
implementation platform('me.dmmax.akka:akka-spawn-protocol-bom:1.1')
// Don't need to specify the version for root and extensions
implementation 'me.dmmax.akka:akka-spawn-protocol'
implementation 'me.dmmax.akka:akka-spawn-protocol-guice'
// ... other dependencies
}
BOM includes:
BOM | Artifact |
---|---|
Akka spawn protocol modules | me.dmmax.akka:akka-spawn-protocol-[module] |
Akka BOM | com.typesafe.akka:akka-bom_2.12 |
Create a spawn protocol
// Preconditions
akka.actor.ActorSystem classicActorSystem = akka.actor.ActorSystem.create();
ActorSystem<Void> actorSystem = Adapter.toTyped(classicActorSystem);
Scheduler scheduler = actorSystem.scheduler();
// Actor-specific configuration
Duration askTimeout = Duration.ofSeconds(1);
ActorRef<SpawnerActor.Command> spawnerActor = actorSystem.systemActorOf(SpawnerActor.create(), "rootActor", Props.empty());
// Create the spawn actor
SpawnProtocol<SpawnerActor.Command> spawnProtocol = new SpawnProtocol<>(scheduler, askTimeout, spawnerActor, SpawnActorCommandWrapper::new);
In addition, you need to handle the wrapper message inside the spawner actor
private <CHILD> Behavior<Command> onSpawnActor(SpawnActorCommandWrapper<CHILD> wrapper) {
SpawnProtocols.spawnChildActor(getContext(), wrapper.spawnActorCommand());
return this;
}
Now, it is pssible to create child actors using the SpawnProtocol
SpawnProtocol<SpawnerActor.Command> spawnProtocol = ...;
// Create a child actor
SpawnActorInfo<Ping> pingActorInfo = new SpawnActorInfo<>(PingActor.create(), ActorCreationStrategy.unique("pinger"));
ActorRef<Ping> pingActor = spawnPtocol.create(pingActorInfo);
I have a list of issues which I am planning to work in the nearest future. Feel free to add your own ideas there.