Skip to content

Instantly share code, notes, and snippets.

@langley
Last active May 7, 2017 23:53
Show Gist options
  • Save langley/8700113 to your computer and use it in GitHub Desktop.
Save langley/8700113 to your computer and use it in GitHub Desktop.
Determine whether an akka actor exists or not in the repl. Play 2.2.1 implies Akka 2.2.0 Using this trick means you don't need to maintain your own list of existing actors. However you'll need to actually use it from w/in an actor (notice that the the answer is a message)
import akka.actor._
// need an actor system, make it implicit to it's used by all our repl based actors
implicit val system = ActorSystem("replActorSystem")
// this gives us an easy way to define simple actors
import ActorDSL._
// make an actor that can be used to receive responses... just a good practice
implicit val sender = actor(new Act { become { case msg => println(s"Console sender recvd: $msg") } } )
// make a named actor we know is there
val findableActor = actor("findableActor")(new Act { become { case msg => sender ! s"finableActor heard you say: $msg" }})
val wildCardFindableActor = actor("findable_with_extra_for_wildcard_test")(new Act { become { case msg => sender ! s"findnable_with_extra_for_wildcard_test you say: $msg" }})
// see if it exists by sending it a message
val possiblyThere = system.actorSelection("akka://replActorSystem/user/findableActor")
possiblyThere ! new akka.actor.Identify()
// now compare that to one that doesn't exist
val probablyNotThere = system.actorSelection("akka://replActorSystem/user/noSuchActor")
probablyNotThere ! new akka.actor.Identify()
// Resolved actors respond with a Some(...) and unresolved ones respond with a None
// A simpler api to support this use case would be nice, perhaps that's what Akka 2.2.3 resolveOne is about
// Now look for an actor using a wildcard
val wildCardActorLookup = system.actorSelection("akka://replActorSystem/user/findable*")
wildCardActorLookup ! new akka.actor.Identify()
@langley
Copy link
Author

langley commented Feb 8, 2014

Updated gist to illustrate wild card searches...

Note: this was "built" and tested against play 2.2.1 which uses akka 2.2.0

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment