Domanda

I need to determine whether the sender of a message was one of the local or remote actors in the Akka cluster. At present the only way I've found to do this is

def isLocal(sndr: ActorRef) = sndr.path.address.toString == context.system.toString

What's a better way?

I'm using Akka 2.3

Update: To explain why I want to do this, in case there is a way to avoid the issue entirely.

On each node a router manages a number of actors to do work. The worker actors send results back to the local master actor and are assigned new jobs via the sender reference. Master actors also periodically send results to the master actors of other nodes, ensuring that data is randomly 'mixed' between masters on different nodes. In the case of data arriving from a remote actor, the receiving master should not attempt to allocate new work. It's essentially an implementation of an islands method with mixing.

È stato utile?

Soluzione

You can get the remoting address for your ActorSystem through an Extension (see Programmatically obtain ephemeral port with Akka). That way you could compare addresses instead of strings, a little less hacky.

Addressing Ryan's point, another approach would be to wrap the results message in another message. When you match on the wrapper you'll know it's not from a local worker and then you can unwrap it and do whatever you need to do with the results.

def receive = {
  case NonLocalResults(results) => // do something with non-local results
  case Results(...)             => // do something with local results
  ...
}

Altri suggerimenti

You can also check Sender's host and protocol. Local actors will have empty host and akka as a protocol, remote actors on the other hand will have valid hostname/ip and akka.tcp as a protocol

Autorizzato sotto: CC-BY-SA insieme a attribuzione
Non affiliato a StackOverflow
scroll top