Hi Farid,

I think the solution that we prefer is to add a new initial state to your
actors, and when they are started:
 - send an Identify message using ActorSelection to the intended target to
resolve its ActorRef
 - wait for the reply (handling failure of course, maybe retrying for a
whiel), potentially buffering external requests until the other actor is
resolved
 - then save the ActorRef and use it directly. You can also watch this
Actor, and after receiving a Terminated message transition to the initial
state where you try to look up a new one.

This way you don't need to have a global lookup "registry" and you also use
the much faster (and safer) way of communicating via ActorRefs. The only
part where you need the ActorSelection is where you look-up the actor at
the first time (or the previous incarnation disappeared)

-Endre

On Tue, May 26, 2015 at 8:10 PM, Mahitab Farid <[email protected]> wrote:

> Hi,
>
> I have actor system, I initialize it at the application beginning.
> I need to call these actors from my controllers, so I pass the ActorRef to
> my controllers/services and keep them in local variable.
>
> upon some actions from user the actor system could be stopped, then
> started (not restart).
>
> keeping the references in the controller/services causing me errors after
> starting the ActorSystem as the controllers hold the old references
> (incarnations).
>
> So, how to keep the actor references?
>
> should I use ActorSelection with each request? would that affect the
> performance?
>
> public class MyController {
>
>   public static Promise<Result> myAction(String param) {
>
>     ActorSelection myActor = Akka.system().actorSelection(
>         "akka://application/user/myActor");
>
>     String myMessage = "hello";
>     Promise<JSONObject> resultJsonObjectPromise =
>       Promise.wrap(ask(myActor, myMessage, 1000000)).map(
>         new Function<Object, JSONObject>() {
>           public JSONObject apply(Object response) {
>             return ((myResponse) response).getJsonResult();
>           }
>         });
>
>     return resultJsonObjectPromise.map(new Function<JSONObject, Result>()
> {
>       public Result apply(JSONObject response) {
>           return ok(response.toString());
>       }
>     });
>   }
> }
>
>
>
> or shall I keep the actor reference in a main class and each time
> controller get the actor ref from this class?
>
>
> public class ActorBag {
>   public static ActorRef myActorRef;
> }
>
>
> @ application start:
>
> ActorBag.myActorRef = Akka.system().actorOf(
>         Props.create(MyActor.class, params), "MyActor");
>
>
>
> public class MyController {
>
>   public static Promise<Result> myAction(String param) {
>
>     ActorRef myActor = ActorBag.myActorRef;
>
>     String myMessage = "hello";
>     Promise<JSONObject> resultJsonObjectPromise =
>       Promise.wrap(ask(myActor, myMessage, 1000000)).map(
>         new Function<Object, JSONObject>() {
>           public JSONObject apply(Object response) {
>             return ((myResponse) response).getJsonResult();
>           }
>         });
>     //same as above
>   }
> }
>
>
> What is the best practice
> ActorRef myActor = ActorBag.myActorRef;
> or
> ActorSelection myActor =
> Akka.system().actorSelection("akka://application/user/myActor");
>
> or anything else
> ?
>
> Regards,
>    MFarid
>
> --
> >>>>>>>>>> Read the docs: http://akka.io/docs/
> >>>>>>>>>> Check the FAQ:
> http://doc.akka.io/docs/akka/current/additional/faq.html
> >>>>>>>>>> Search the archives: https://groups.google.com/group/akka-user
> ---
> You received this message because you are subscribed to the Google Groups
> "Akka User List" group.
> To unsubscribe from this group and stop receiving emails from it, send an
> email to [email protected].
> To post to this group, send email to [email protected].
> Visit this group at http://groups.google.com/group/akka-user.
> For more options, visit https://groups.google.com/d/optout.
>



-- 
Akka Team
Typesafe - Reactive apps on the JVM
Blog: letitcrash.com
Twitter: @akkateam

-- 
>>>>>>>>>>      Read the docs: http://akka.io/docs/
>>>>>>>>>>      Check the FAQ: 
>>>>>>>>>> http://doc.akka.io/docs/akka/current/additional/faq.html
>>>>>>>>>>      Search the archives: https://groups.google.com/group/akka-user
--- 
You received this message because you are subscribed to the Google Groups "Akka 
User List" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to [email protected].
To post to this group, send email to [email protected].
Visit this group at http://groups.google.com/group/akka-user.
For more options, visit https://groups.google.com/d/optout.

Reply via email to