Akka member(actor) lookup in cluster

2.1k views Asked by At

I have situation when my actor A1 can be started in any seed node in cluster. In some situation other actor A2 (possibly from another node) need to get ActorRef for A1. What is the best way to perform this?
1) I would like to avoid hardcode seed nodes network addresses.
2) I could run actor that subscribed for cluster events, but i don't know how to force him to see nodes that was in cluster before it started.
3) I could try find actor by looping ActorSelection, but i still cant get seed nodes list from my akka.Cluster. Closest way that i found is:
Cluster cluster = Cluster.get(context().system()); SortedSet<Member>members = cluster.state().members(); Traversable<Member> filteredmembers = members.filter(_.status == MemberStatus.up()); But this is Scala syntax and classes, i have exception:

'_' used as an identifier (use of '_' as an identifier might not be supported in releases after Java SE 8)

How to avoid this exception?

3

There are 3 answers

0
waterscar On BEST ANSWER

Check this out: http://doc.akka.io/docs/akka/2.4.0-RC2/java/distributed-pub-sub.html

With this, you can send messages to an actor within the cluster locally or remotely just by the actor's path. It even do a simple routing for you when you have multiple cluster node running the same actor.

0
Asprelis On

Well, my current implementation is:

import akka.actor.UntypedActor;
import akka.cluster.Cluster;
import akka.cluster.Member;
import scala.collection.Iterator;
import scala.collection.immutable.SortedSet;

Cluster cluster = Cluster.get(myUntypedActor.context().system());
SortedSet<Member> members = cluster.state().members();
final Iterator<Member> iter = members.iterator();
// ..searching in every member for needed actor using iterator 
0
Oleg Ushakov On

Try to replace a single underline to the double underline, this should work. That is, use __ instead of _

Cluster cluster = Cluster.get(context().system());
SortedSet<Member>members = cluster.state().members();
Traversable<Member> filteredmembers = members.filter(__.status == MemberStatus.up());