In Akka I can create an actor as follows.
Akka.system(app).actorOf(Props(classOf[UnzipActor]), name="somename")
Then I am in a different class, how can I get this actor?
I can get an ActorSelection
lazy val unzip: ActorSelection = Akka.system.actorSelection("user/" + "somename")
However, a ActorSelection
is not what I want; I want an ActorRef
. How can I get a ActorRef
?
I want to have an ActorRef
since I wish to schedule a call to an ActorRef
using the scheduler.
Akka.system(app).scheduler.schedule( 5 seconds, 60 seconds, mustBeActorRef, MessageCaseClass())
ActorRefFactory, an interface which is implemented by ActorSystem and akka. actor. ActorContext. This means actors can be created top-level in the ActorSystem or as children of an existing actor, but only from within that actor. ActorRefs can be freely shared among actors by message passing.
What is an Actor Reference. An actor reference is a subtype of ActorRef , whose foremost purpose is to support sending messages to the actor it represents. Each actor has access to its canonical (local) reference through the. ActorContext.
The Actor Model provides a higher level of abstraction for writing concurrent and distributed systems. It alleviates the developer from having to deal with explicit locking and thread management, making it easier to write correct concurrent and parallel systems.
Akka Cluster provides a fault-tolerant decentralized peer-to-peer based Cluster Membership Service with no single point of failure or single point of bottleneck. It does this using gossip protocols and an automatic failure detector.
You can use the method resolveOne
on an ActorSelection to get an ActorRef asynchronously.
implicit val timeout = Timeout(FiniteDuration(1, TimeUnit.SECONDS)) Akka.system.actorSelection("user/" + "somename").resolveOne().onComplete { case Success(actorRef) => // logic with the actorRef case Failure(ex) => Logger.warn("user/" + "somename" + " does not exist") }
ref : http://doc.akka.io/api/akka/2.3.6/index.html#akka.actor.ActorSelection
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With