0
votes

First I would like to say that I am very new to Akka and actors :)

I would like to create a distributed application. I will split the application in a web part(REST API) and a user management part. But what is the right way to access an actor from another part of the application?

I know that I can access an actor selection by providing its address (https://doc.akka.io/docs/akka/2.5/remoting.html#looking-up-remote-actors), but isn't there a way where I don't have to work with addresses?

I just want to create a system where it is very easy to reach a remote actor without using their addresses.

2
Address is a way to describe which actor you want to reach. So, what you are asking seems to be wether there is a way to reach an actor without specifying which one you want. I don't think so :)Dima
In order to access remote actor, you must know where is that actor is, and address is a way to to identify that. How do you expect to get reference of remote actor without out its address?Ra Ka
@RaKa I have some actor that will only have a single instance, so i though maybe I can reach them by their class.Jan Wytze

2 Answers

0
votes

Maybe you can have a look for Distributed Publish Subscribe in Cluster

But the limit is: your local actorsystem which hosts your local actor & the remote actorsystem which hosts your remote actor should be configured as an akka cluster.

If cluster could be your design, then you can do as follows:

Remote Part:

class Subscriber extends Actor with ActorLogging {
  import DistributedPubSubMediator.{ Subscribe, SubscribeAck }
  val mediator = DistributedPubSub(context.system).mediator
  // subscribe to the topic named "content"
  mediator ! Subscribe("content", self)

  def receive = {
    case s: String ⇒
      log.info("Got {}", s)
    case SubscribeAck(Subscribe("content", None, `self`)) ⇒
      log.info("subscribing")
  }
}

Local Part:

class Publisher extends Actor {
  import DistributedPubSubMediator.Publish
  // activate the extension
  val mediator = DistributedPubSub(context.system).mediator

  def receive = {
    case in: String ⇒
      val out = in.toUpperCase
      mediator ! Publish("content", out)
  }
}

The remote part actor subscribe content topic, and if local part want to talk with remote, it can just publish message to content topic.

Hope it gives you some thoughts.

0
votes

In Cluster Sharding the actor identity is persistanceId and must be part of message which you send to whole cluster. Thats why you need to define extractEntityId.

Read more in documentation: https://doc.akka.io/docs/akka/2.5/cluster-sharding.html