Akka Java - Creating recursive child actors on the fly?

I understand that onReceive can only be performed by thread one at any given time.

Say I have an untyped actor like this:

import akka.actor.ActorRef;
import akka.actor.Props;
import akka.actor.UntypedActor;


public class ExampleActor extends UntypedActor {

     private ActorRef databaseActor;


    @Override
    public void preStart() {
       ActorRef databaseActor = getContext().system().actorOf(Props.create(DatabaseActor.class));
    }


    @Override
    public void onReceive(Object message) throws Exception {

        if (message.equals("start")) {
            // spawn a child actor of myself!
            ActorRef child = getContext().actorOf(Props.create(ExampleActor.class));
            databaseActor.tell("fetch", child);
        }

        if (message.equals("dbresponse")) {
           // just log the repsonse here!
        }

        if (message.equals("something else")) {
           // possibly mutate state
        }


   }
}

I essentially want to use Akka without using futures. At the same time, I want my actors to NOT block as much as possible. Is it possible to create recursive child actors in my onReceive, soley to handle specific messages from other participants?

Essentially in my "if (message.equals (" dbresponse "))", I want to just register the DB response and not mutate the state in my example.

Will this approach work? What are the implications of creating actors on the fly like this?

+4
1

, Actor . ask , ( ), , Futures, .

+4

Source: https://habr.com/ru/post/1539710/


All Articles