Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to get a reference to an existing ActorSystem in Akka?

is it possible in Akka (scala) to get a reference to an existing ActorSystem?

I am working on a Spray application with another Actor for DB. I am also extending Directives to have an object per path. the directives are not actors by themselves, but they need to pass messages to the DBActor. here:

class HttpActor extends Actor with HttpService {

  val actorRefFactory = context

  def receive = runRoute(
    IndexService.route ~ 
    HostsService.route    
  )
}

object HostsService extends Directives{
  def route(implicit dm: DetachMagnet2) = {
    path("hosts") {
      get {  
        detach() {
          **dbActor ! CreateHost** 
          complete("get me hosts!")
        }
      } ~
      post {
        detach() {
          entity(as[String]) { payload =>
            complete(s"post hosts $payload")     
          }
        }
      }
    }
  }
}

is there a way for HostsService to discover the ActorSystem itself so he can find the DBActor, or must HttpActor pass it in? the latter seems less elegant, as it HostsService will need to become a class (not an object), so no longer a singleton.

like image 894
Ehud Kaldor Avatar asked Feb 08 '15 22:02

Ehud Kaldor


People also ask

How to get ActorRef of actor?

An ActorRef can be obtained from an ActorRefFactory, an interface which is implemented by ActorSystem and 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.

What is ActorRef in Akka?

java.io.Serializable. An ActorRef is the identity or address of an Actor instance. It is valid only during the Actor’s lifetime and allows messages to be sent to that Actor instance.

Does Akka use event loop?

Threading Akka Actors are similar to Verticles in Vert. x in the sense that both are the unit of execution. Similar to Actors, Verticles are running on the event loop by default, which is essentially a thread pool that shall not be blocked.

Can Akka be used with Java?

Akka is a toolkit and runtime for building highly concurrent, distributed, and fault-tolerant event-driven applications on the JVM. Akka can be used with both Java and Scala. This guide introduces Akka Actors by describing the Java version of the Hello World example.


1 Answers

From here:

there was a ticket for creating such a registry, but we were not satisfied with what we got when trying to specify the semantics in detail. One part is that we removed all global state so that different parts of an application can use Akka without having to worry about each other and a global feature would break this. Another is that it would encourage get-or-create usage—my pet peeve—which would make the semantics unclear: you give a name and a config, but if the name already exists you potentially get back a differently configured system (which is usually quite fatal).

There is nothing stopping you from putting a hashmap in some central place of your application, (pre-)populate that with the actor systems you need and be done, that's basically a one-liner (which is another reason for not including it in Akka, because instead of a simple solution to a very narrow problem we'd have to think of a solution to a much more generic problem)

In your case, it's better to pass your system implicitly to the route function:

class HttpActor extends Actor with HttpService {

  implicit val actorRefFactory = context

  def receive = runRoute(
    IndexService.route ~ 
    HostsService.route    
  )
}

object HostsService extends Directives {
  def route(implicit dm: DetachMagnet2, as: ActorContext) = {...}
}
like image 78
dk14 Avatar answered Oct 10 '22 22:10

dk14