From within an Akka actor, how can you find out the nodes of the cluster? That is, the nodes which the local node considers to be currently accessible.
Thanks, - Daniel
You actually do not need to subscribe to ClusterDomainEvent
or MemberEvent
. You can just access the state
member of the Cluster extension, e.g.,
val cluster = akka.cluster.Cluster(context.system)
val members = cluster.state.members.filter(_.status == MemberStatus.Up)
Tweaked sample from Typesafe Activator tutorial:
case object GetNodes
class MemberListener extends Actor {
val cluster = Cluster(context.system)
override def preStart(): Unit =
cluster.subscribe(self, classOf[MemberEvent])
override def postStop(): Unit =
cluster unsubscribe self
var nodes = Set.empty[Address]
def receive = {
case state: CurrentClusterState =>
nodes = state.members.collect {
case m if m.status == MemberStatus.Up => m.address
}
case MemberUp(member) =>
nodes += member.address
case MemberRemoved(member, _) =>
nodes -= member.address
case _: MemberEvent ⇒ // ignore
case GetNodes =>
sender ! nodes
}
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With