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 8 maj 2014 kl. 23:28 skrev Tony Bones <aabo...@gmail.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 > > > > > On Friday, April 11, 2014 1:30:12 AM UTC-7, Martynas Mickevičius wrote: > Try googling for some code snippets: http://lmgtfy.com/?q=akka+ActorIdentity > > Some results: > http://stackoverflow.com/a/18015626/77102 > http://letitcrash.com/post/55504766698/2-2-spotlight-actorselection-watch-and-identify > > > On Fri, Apr 11, 2014 at 6:52 AM, Atom Cong <hota...@gmail.com> wrote: > 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~! > > > > > On Thu, Apr 10, 2014 at 12:20 AM, Heiko Seeberger <heiko.s...@gmail.com> > wrote: > http://doc.akka.io/docs/akka/2.3.2/scala/actors.html#Identifying_Actors_via_Actor_Selection > > > On Thu, Apr 10, 2014 at 7:27 AM, Atom Cong <hota...@gmail.com> wrote: > 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. > > On Monday, February 17, 2014 2:27:12 AM UTC-8, Patrik Nordwall wrote: > 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 > > > On Sun, Feb 16, 2014 at 4:01 PM, Ömer Faruk Gül <omerg...@gmail.com> wrote: > 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. > > -- > >>>>>>>>>> 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 > > > > -- > >>>>>>>>>> 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 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 > Blog: blog.heikoseeberger.name > > -- > >>>>>>>>>> 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 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. > > 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. > > > -- > >>>>>>>>>> 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 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. > > > > -- > Martynas Mickevičius > Typesafe – Reactive Apps on the JVM > > -- > >>>>>>>>>> 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 akka-user+unsubscr...@googlegroups.com. > To post to this group, send email to akka-user@googlegroups.com. > Visit this group at http://groups.google.com/group/akka-user. > For more options, visit https://groups.google.com/d/optout. Dr. Roland Kuhn Akka Tech Lead Typesafe – Reactive apps on the JVM. twitter: @rolandkuhn -- >>>>>>>>>> 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 akka-user+unsubscr...@googlegroups.com. To post to this group, send email to akka-user@googlegroups.com. Visit this group at http://groups.google.com/group/akka-user. For more options, visit https://groups.google.com/d/optout.