Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do I properly fire up a temporary actor in Akka?

Tags:

akka

I am building something similar (but not identical) to the ask pattern in Akka, where I need to instantiate a temporary actor that handled a single message and then kills itself. I've implemented the basic workflow using actorOf() once per request, but this doesn't feel quite right, as it will register a new actor at a new path each time.

What is the proper way of doing this?

like image 705
SoftMemes Avatar asked May 10 '12 09:05

SoftMemes


People also ask

How do actors work in Akka?

What is an Actor in Akka? An actor is essentially nothing more than an object that receives messages and takes actions to handle them. It is decoupled from the source of the message and its only responsibility is to properly recognize the type of message it has received and take action accordingly.

Who can be supervisor actor in Akka?

There are two type of supervision strategies that we follows to supervise any actor: One-For-One – Only the faulty child is affected when it fails. One-For-All – All children are affected when one child fails.


1 Answers

It sounds like you need to use a future. As far as I understand, Akka futures take care of actor creation & disposal for you; if you flatMap several futures together you'll notice that some execute within the same actor, while for others a new one is created. To shamelessly paraphrase the documentation:

import akka.japi.Function;
import java.util.concurrent.Callable;
import akka.dispatch.Futures;
import akka.dispatch.OnComplete;

Future<String> f = Futures.future(new Callable<String>() {
  public String call() {
    return "Hello" + "World";
  }
}, system.dispatcher()).andThen(new OnComplete<String>() {
    public void onComplete(Throwable err, String result) {
        // do something with the err and/or result
    }
   });
});

I guess something like the above might suffice? Check out the doc for more examples...

@viktor-clang might know otherwise, but I don't think you should be particularly worried about the number of actors spawned in the general case; they're much cheaper to create than OS threads.

like image 183
Chris Mowforth Avatar answered Dec 01 '22 17:12

Chris Mowforth