Actors Initialization order.

30 views
Skip to first unread message

Hsnamed

unread,
Oct 19, 2017, 8:48:30 AM10/19/17
to Akka User List
Hello, i trying to build actor system as N-levels tree with mutable state/data . 


public class RootActor extends AbstractActor implements Loggable {
 
String name;
 
Root data;

 
public static Props props(String name, Root data, List<Node<?>> children) {
 
return Props.create(RootActor.class, () -> new RootActor(name, data, children));
 
}

 
public RootActor(String name, Root data, List<Node<?>> childs) {
 
this.name = name;
 
this.data = data;
 spreadChilds
(childs);
 
}

 
@Override
 
public Receive createReceive() {
           
return receiveBuilder().build();
 
}

 
private void spreadChilds(List<Node<?>> childs) {
 childs
.forEach( node -> {
         
Data obj = ((Node<Data>)node).getData();
         
List<Node<?>> grandchilds = node.getChildren();
         getContext
().actorOf(ChildActor.props(obj.getName(), obj, grandchilds), obj.getName());
 
});
 
}
}


1. What is the right way to initialize list of child actors and grandchildren , does the supervisor strategy helps me to know when actor tree initialization completed ?
2. Do i need to store child actors references in their parents ?
3. Are there right ways to initialize actors with mutable state/values ?

Thank you for advices.

Tal Pressman

unread,
Oct 19, 2017, 10:09:10 AM10/19/17
to Akka User List
Hi,

Can you clarify a bit what you're trying to do?
Are the children and grandchildren supposed to be Actors? What's a Node?

Generally speaking, if you want actor1 to be actor2's parent, then actor1 has to be the one creating actor2. Even then, all actor1 will have is an ActorRef, not a reference to the actual actor. This is an important distinction, because it means there is no way to call a "getChildren" method on a child actor to obtain the grandchildren. Instead, you would have to send a message to that child, and wait for it to respond.

To try and answer your specific questions:
1. There is no built-in way of telling when an actor is initialized (and, by extension, when a group of actors is initialized). In most cases, this shouldn't matter to you - once you call context.actorOf and obtain an ActorRef you can start using it (sending it messages) without having to wait. Once the actor is initialized, it'll start handling those messages.

2. You can either save them (in a Map, List, whatever) or use context.getChildren. Again, though, note that these are ActorRefs and not the actual actor objects.

3. This is easier - since no one has access to an actor's state, you can change it freely as long as you're in the actor's receive method (or it's somewhere in your call-stack). You have to be careful with callbacks, futures, etc. that run on different threads, though.

HTH,
Tal

Hsnamed

unread,
Oct 19, 2017, 12:13:04 PM10/19/17
to Akka User List
I have business objects tree which i receive from from datasource.
Each element of tree is node<T> where T is business object type, on the same level of tree T-types are same.

Then i traverse the tree for warm up/build actor system, pass to actor constructor business object T and list of children - List<Node<U>> for creating child actors.
Its like cascade initialization.

When the Actor received msg
1. it modify Actor business object state
2. depends on object state, Actor sends msg to CommanderActor wich call business command.
3. then Actor have to route msg to one concrete child actor.

4. Then the same steps from 1 to 3 are executed for child actor and so one.


 


четверг, 19 октября 2017 г., 15:48:30 UTC+3 пользователь Hsnamed написал:
Reply all
Reply to author
Forward
0 new messages