How to spawn typed actor

(Syed Ali) #1

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

(Johan Andrén) #2

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”.

(Mushtaq) #3

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

(Johan Andrén) #4

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.

(Mushtaq) #5

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.

(Johan Andrén) #6

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.

(Mushtaq) #7

Makes sense!