Observe the cluster client and its receptionist

Allows the cluster client and its receptionist to be observable in terms of contact points becoming available and client heartbeats. Furthermore a query API for requesting the current state has been provided.
This commit is contained in:
Christopher Hunt 2016-05-04 04:50:16 -07:00
parent 313606eb1c
commit ceb0678de2
7 changed files with 610 additions and 34 deletions

View file

@ -4,6 +4,7 @@
package akka.cluster.client;
import akka.actor.*;
import com.typesafe.config.ConfigFactory;
import java.util.Arrays;
import java.util.HashSet;
@ -11,12 +12,6 @@ import java.util.Set;
import org.junit.ClassRule;
import org.junit.Test;
import akka.actor.ActorPath;
import akka.actor.ActorPaths;
import akka.actor.ActorRef;
import akka.actor.ActorSystem;
import akka.actor.Props;
import akka.actor.UntypedActor;
import akka.testkit.AkkaJUnitActorSystemResource;
import org.scalatest.junit.JUnitSuite;
@ -57,10 +52,80 @@ public class ClusterClientTest extends JUnitSuite {
c.tell(new ClusterClient.Send("/user/serviceA", "hello", true), ActorRef.noSender());
c.tell(new ClusterClient.SendToAll("/user/serviceB", "hi"), ActorRef.noSender());
//#client
system.actorOf(Props.create(ClientListener.class, c));
system.actorOf(Props.create(ReceptionistListener.class, ClusterClientReceptionist.get(system).underlying()));
}
static public class Service extends UntypedActor {
public void onReceive(Object msg) {
}
}
//#clientEventsListener
static public class ClientListener extends UntypedActor {
private final ActorRef targetClient;
private final Set<ActorPath> contactPoints = new HashSet<>();
public ClientListener(ActorRef targetClient) {
this.targetClient = targetClient;
}
@Override
public void preStart() {
targetClient.tell(SubscribeContactPoints.getInstance(), sender());
}
@Override
public void onReceive(Object message) {
if (message instanceof ContactPoints) {
ContactPoints msg = (ContactPoints)message;
contactPoints.addAll(msg.getContactPoints());
// Now do something with an up-to-date "contactPoints"
} else if (message instanceof ContactPointAdded) {
ContactPointAdded msg = (ContactPointAdded) message;
contactPoints.add(msg.contactPoint());
// Now do something with an up-to-date "contactPoints"
} else if (message instanceof ContactPointRemoved) {
ContactPointRemoved msg = (ContactPointRemoved)message;
contactPoints.remove(msg.contactPoint());
// Now do something with an up-to-date "contactPoints"
}
}
}
//#clientEventsListener
//#receptionistEventsListener
static public class ReceptionistListener extends UntypedActor {
private final ActorRef targetReceptionist;
private final Set<ActorRef> clusterClients = new HashSet<>();
public ReceptionistListener(ActorRef targetReceptionist) {
this.targetReceptionist = targetReceptionist;
}
@Override
public void preStart() {
targetReceptionist.tell(SubscribeClusterClients.getInstance(), sender());
}
@Override
public void onReceive(Object message) {
if (message instanceof ClusterClients) {
ClusterClients msg = (ClusterClients) message;
clusterClients.addAll(msg.getClusterClients());
// Now do something with an up-to-date "clusterClients"
} else if (message instanceof ClusterClientUp) {
ClusterClientUp msg = (ClusterClientUp) message;
clusterClients.add(msg.clusterClient());
// Now do something with an up-to-date "clusterClients"
} else if (message instanceof ClusterClientUnreachable) {
ClusterClientUnreachable msg = (ClusterClientUnreachable) message;
clusterClients.remove(msg.clusterClient());
// Now do something with an up-to-date "clusterClients"
}
}
}
//#receptionistEventsListener
}