Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to get Akka actor by name as an ActorRef?

Tags:

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()) 
like image 572
Phil Avatar asked Sep 22 '14 04:09

Phil


People also ask

What is ActorRef in Akka?

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 a reference actor?

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.

What is an actor Scala?

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.

What is Akka cluster?

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.


1 Answers

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

like image 174
Arnaud Gourlay Avatar answered Sep 24 '22 08:09

Arnaud Gourlay