How to run a temporary actor in Akka?

I create something similar (but not identical) to the request template in Akka, where I need to create an instance of a temporary actor that processes one message and then kills itself. I performed the main workflow using actorOf () once per request, but this is not entirely correct, since each time a new actor is registered in a new way.

What is the right way to do this?

+6
source share
1 answer

It looks like you need to use the future . As far as I understand, Akka futures care about creating and destroying actors for you; if you flatMap several futures together, you will notice that some are executed inside the same actor, and for others a new one is created. Shamelessly rephrase 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 think something like this might be enough? See the document for more examples ...

@viktor-clang may know differently about this, but I don’t think you are particularly worried about the number of actors generated in the general case; they are much cheaper to create than OS threads.

+5
source

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


All Articles