How to spawn typed actor

Is there a way to spawn a typed actor outside of actor? Untyped actor system had actorOf function, can’t find any way to spawn a typed actor.

Regards,

Syed

Did you read this chapter of the docs? https://doc.akka.io/docs/akka/current/typed/actor-lifecycle.html#creating-actors

Especially the SpawnProtocol section if you want to create actors from “the outside”.

Similarly, is there a way to stop the actors created from “the outside”?

No way to “kill” it, you will have to have a message in the actor protocol that tells it to stop (transition into Behavior.stopped).

Note that there is nothing stopping you from creating your own guardian (or actor you run as a child somewhere) that has different semantics, if you look at the akka.actor.typed.SpawnProtocol implementation it is quite a simple behavior.

Yes, that is what we did, we have our own guardian that spawns as well as stops.

Just felt that if there is a provision in akka to spawn user actors, there should also be a way to stop them from the outside.

In general it is better to have stopping as a part of the actor protocol to allow for graceful stop rather than dropping all inbox contents (as with context.stop from the parent) so we do not want to encourage that when it is so easy to do yourself if you have decided you really want it.

Makes sense!

Hello there, I faced the same request to start spawning actors from “outside” but still have access to the Actor Reference.

I prepared a small library that handles it: GitHub - dmmax/akka-spawn-protocol: Uses Spawn Protocol to create Akka Actors from outside. It can be applied at any level of actors' hierarchy, it also has Guice’s extension if you use it as a DI framework.

From what I could see your library works with the classic API and not the typed API the author was asking about. Is this the case?

If you mean the library that I shared - it works primarily with typed actors. Please check the examples in the readme.md