Chris Kong
Chris Kong

Reputation: 399

Dynamically(programmatically) generate Actors in Akka

Actor system is stunning. I wonder if I can make it more flexible to use.

Saying I have a series of related node pairs (may in form of a file) like:

I want to generate a system containing 6 kinds of actors namely A~F programmatically when the server starts up. And they have supervisor-children relationship just like the pairs above tell.

Does this possible to Akka?

EDIT 2014-05-28

Sorry for the confuse.

I'm new to Akka, and some of the concept I learned so far might not be so accurate.

The real use case is that I want to generate an actor system using a config file which contains the relationship between actors. And each kind of actors have their strategy to deal receiving message. As @cmbaxter put here, given actor can only have one supervisor, so let's just simplify the problem, saying I have a file contain actor relations such as

Based on this, I want to generate a system programmatically as:

      A
     / \
    B   C
   /   / \
  D   E   F

And I want each of A~F to have routers so that I am able to not create actors after this system is built. I know router might create actors under the hood, but I surely don't want any actor is created by mistake.

Does this sound possible with Akka? Thanks in advance!

Upvotes: 2

Views: 2126

Answers (1)

Hierarchies like this are simply defined by creating actors from other Actors.

val a = system.actorOf(..., "a") ! PleaseCreate("b", "c")

// inside "a"
context.actorOf(..., "b") ! PleaseCreate("d")
context.actorOf(..., "c") ! PleaseCreate("e", "f")

// and so on

The receive method would simply:

 def receive = { case PleaseCreate(names) => names foreach { context.actorOf(..., _) }

Be sure to check out the official docs too: http://doc.akka.io/docs/akka/snapshot/scala/actors.html

I hope this helps :-)

Upvotes: 2

Related Questions