Using ActorSelection to check if actor exists

3,853 views
Skip to first unread message

Ömer Faruk Gül

unread,
Feb 16, 2014, 10:01:35 AM2/16/14
to akka...@googlegroups.com
Hi,

First of all I'm using akka 2.2 with play 2.2.1. So I don't have access to method resolveOn.

What's the proper way of checking if actor exists and create it if doesn't exists?

So I want to check if the following actorselection exists, what can I do?

Akka.system().actorSelection("room-"+room.id)

Thanks.

Patrik Nordwall

unread,
Feb 17, 2014, 5:27:12 AM2/17/14
to akka...@googlegroups.com
Hi,

You can send an akka.actor.Identify("hello") and the actor will reply with akka.actor.ActorIdentity("hello", Some(actorRef)) message, if it exists.
If it doesn't exist the reply will be akka.actor.ActorIdentity("hello", None), but this message (as all messages) can be lost and then you will not receive any reply, and then you can retry it.

You need an actor to handle the reply, which you might not have in your Play application. Then you can use ask instead, and act on the Future[ActorIdentity].

Cheers,
Patrik


--
>>>>>>>>>> Read the docs: http://akka.io/docs/
>>>>>>>>>> Check the FAQ: http://akka.io/faq/
>>>>>>>>>> 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 akka-user+...@googlegroups.com.
To post to this group, send email to akka...@googlegroups.com.
Visit this group at http://groups.google.com/group/akka-user.
For more options, visit https://groups.google.com/groups/opt_out.



--

Patrik Nordwall
Typesafe Reactive apps on the JVM
Twitter: @patriknw

Atom Cong

unread,
Apr 10, 2014, 1:27:22 AM4/10/14
to akka...@googlegroups.com
Hi, Akka experts,

     Patrik's solution seems make sense to me.  But I couldn't figure out how to code it up, and what exactly APIs I should use to do the series of operations.

     Can anyone point me to a code example if there is any?

Thank you very much.

Heiko Seeberger

unread,
Apr 10, 2014, 3:20:38 AM4/10/14
to Akka User List



>>>>>>>>>> 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 akka-user+...@googlegroups.com.
To post to this group, send email to akka...@googlegroups.com.
Visit this group at http://groups.google.com/group/akka-user.
For more options, visit https://groups.google.com/d/optout.



--

Heiko Seeberger
Twitter: @hseeberger

Atom Cong

unread,
Apr 11, 2014, 12:52:19 AM4/11/14
to akka...@googlegroups.com
Hi, Heiko,

     Thanks a lot for the response.    I saw that in the manual (java though, http://doc.akka.io/docs/akka/2.3.2/java/untyped-actors.html#actorselection-java), but couldn't understand what exactly is happening.  The sample code of class "Follower" does not have any comment, and does not seem match to the description above.

    For example, the text description says "use the getSender() reference blah blah", but I don't see getSender() being called in the class. Basically, I do see in this example that we are sending an Identity message to an ActorSelection, but I don't see any place obtaining the retrieved ActorRef and make use of it.

   It would be much easier to understand if this example was in a runnable example.  Do we have such an example in the tutorial example projects?


Thank you very much~!
   
   


You received this message because you are subscribed to a topic in the Google Groups "Akka User List" group.
To unsubscribe from this topic, visit https://groups.google.com/d/topic/akka-user/qqzKfE3_VSw/unsubscribe.
To unsubscribe from this group and all its topics, send an email to akka-user+...@googlegroups.com.

Martynas Mickevičius

unread,
Apr 11, 2014, 4:30:12 AM4/11/14
to akka...@googlegroups.com

Tony Bones

unread,
May 8, 2014, 5:28:45 PM5/8/14
to akka...@googlegroups.com
Ok, so I've been struggling with this same issue for a few days now.  I've used uni-directional Akka actors from Play 2.2.2 to start some background tasks that just blindly run.  This is the first time I had a need to do something more complex and while the docs are very abundant I couldn't make sense of a lot of it since I don't really know Scala too well.

I've done some searching around and it seems there are a few posts on this topic.  So here is my solution to the problem.  Please review and let me know if it looks good or if I'm doing something completely crazy wrong.  It's the result of reading 50 different forums, groups, mailing list, and issue tracker posts...and of course official documentation.

From Play 2.2.2 for Java Controller
public static Result invite( String id ) {

// akka actor
   ActorRef ref = null;
   
   // selection path
   String actorId = "InviteSupervisor-" + id;
String path = "/user/" + actorId;
ActorSelection sel = Akka.system().actorSelection(path);

// ask to identify
Timeout t = new Timeout(3000);
AskableActorSelection asker = new AskableActorSelection(sel);
        Future<Object> fut = asker.ask(new Identify(id), t);
        ActorIdentity ident;
try {
// wait results
ident = (ActorIdentity)Await.result(fut, t.duration());
        ref = ident.getRef();
} catch (Exception e) {
Logger.error(TAG + e.getMessage(), e);

// TODO handle timeout or other errors 
                        return badRequest("Selection Error");
}

// actor exist or not
if ( ref == null ) {
// start new actor

// kick off process queue
ref = Akka.system().actorOf(InviteSupervisor.props(id), actorId);

// start invite import process
ref.tell(new InviteSupervisor.ImportMessage(id), ActorRef.noSender());
return ok( "Actor not found :(  but one was created :) " + ref.path().toString() );
} else {
// already running, get progress

String status = "";

// get progress
Future<Object> futProg = asker.ask(new InviteSupervisor.ProgressMessage(null), t);
try {
// wait results
InviteSupervisor.ProgressMessage msg = (InviteSupervisor.ProgressMessage)Await.result(futProg, t.duration());
status = msg.getProgress().toString();

} catch (Exception e) {
Logger.error(TAG + e.getMessage(), e);

// TODO handle timeout or other errors
}

return ok( "Actor Found! " + ref.path().toString() + " [" + status + "]");
}

}


This looks for an actor instance by id on the selection path.  If not found, it creates the actor.  If found it ask the actor for its progress.  Hitting this same URL multiple times gives the results I was looking for.  

The Actor side doesn't do anything special since I'm putting the id in the path.  It never receives the Identify/ActorIndentity message, I guess its handled by the actor system or something internal?

It might be better to wrap some of this in a Play Promise and return that.  Get ride of the asker future timeout as well.  But I'm wondering if I'm interacting with Akka selection paths correctly here.  How's it look?

-Tony

Roland Kuhn

unread,
May 9, 2014, 1:36:26 AM5/9/14
to akka-user
Hi Tony,

that might work most of the time ;-) The problem is that the actor’s liveness might easily change between your Identify and the tell or actorOf call. A better approach is to create one supervisor for this type of job that always exists; keep the reference to that one in a Play Plugin  Then you just send off work to that one, which will process them one by one, enabling it to consistently check its list of child actors (getContext().getChild(name)) and create missing ones if needed (getContext().actorOf()).

Regards,

Roland


Dr. Roland Kuhn
Akka Tech Lead
Typesafe – Reactive apps on the JVM.
twitter: @rolandkuhn


Tony Bones

unread,
May 23, 2014, 5:53:15 PM5/23/14
to akka...@googlegroups.com
Ok, thank you.  This was the same feedback I got from my post on the Play group too.  I'll give it a try.
Reply all
Reply to author
Forward
0 new messages