简体   繁体   中英

Wait for akka actor system to become available

I am using AKKA 2.1 with Scala 2.10.

I need multiple machines to start actor systems and instantiate some number of actors. After this, each system needs to access all other systems and collect references to their actors using "actorFor(...)".

However, I need a way for an actor system to wait on the other systems to boot before it connects, otherwise I get errors.

If actor system A connects to B while B is offline, my program fails. However, if A connects to B and obtains a remote actor reference before it exists on B, the program continues fine once B actually instantiates the actor.

In a nutshell, I need somehow to await the creation event at B before I try connecting to it. Is there a good way to do this in Scala+AKKA?

You might want to look into the cluster support for Akka. It allows you to listen to events when other machines join the cluster and become available for communication.

99% of the time solving the problems with people will let you solve it with actors. Imagine it being buildings (ActorSystems) and people (Actors).

You can create a "receptionist" actor that gets created on ActorSystem startup (Akka Extensions can be configured to be loaded when the ActorSystem starts) under a common path like "/user/receptionist" and then you tell the receptionist to hook you up with actor X/Y/Z when he/she comes online, and then have X/Y/Z register itself with the receptionist. So if X/Y/Z is not there when the external actor asks, it stores the request until some timeout or X/Y/Z "reports in".

First off, actorSelection is now the suggested mechanisms for obtaining references to actors, especially across the network. see: Akka Docs

This is a non-trivial problem in asynchronous and distributed systems. The akka cluster support uses all sorts of mechanisms adopted from their dynamo-like framework. If you imagine that each of your actors are people, and each actor system are people in the building, your task is akin to asking whether Bob has entered the building across the street. You can either:

  • Keep calling that building until Bob picks up. ie send a message attempting to get the actorRef until successful and deal with the errors
  • Make bob call you when he arrives. But this is really a catch 22, as Bob doesn't know you're at your building either.
  • You could introduce a 'seed' actor system to which all other actor systems are implicitly aware of at creation and each sends a message to that location once the actor is available. That would in effect create a centralized repository.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM