Last active
November 26, 2017 15:03
-
-
Save guidomedina/fffeaa10d1017cc528a6817f2c4941be to your computer and use it in GitHub Desktop.
Akka cluster self leave example with both Artery and Netty
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
akka.actor { | |
provider = "akka.cluster.ClusterActorRefProvider" | |
} | |
akka.remote.artery { | |
enabled = on | |
canonical { | |
hostname = 127.0.0.1 | |
port = 0 | |
} | |
} | |
akka.cluster { | |
metrics.enabled = off | |
jmx.enabled = off | |
seed-nodes = [ | |
"akka://[email protected]:2551" | |
] | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
akka.actor { | |
provider = "akka.cluster.ClusterActorRefProvider" | |
} | |
akka.remote.netty.tcp { | |
hostname = 127.0.0.1 | |
port = 0 | |
} | |
akka.cluster { | |
metrics.enabled = off | |
jmx.enabled = off | |
seed-nodes = [ | |
"akka.tcp://[email protected]:2551" | |
] | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
akka.actor { | |
provider = "akka.cluster.ClusterActorRefProvider" | |
} | |
akka.remote.artery { | |
enabled = on | |
canonical { | |
hostname = 127.0.0.1 | |
port = 2551 | |
} | |
} | |
akka.cluster { | |
metrics.enabled = off | |
jmx.enabled = off | |
seed-nodes = [ | |
"akka://[email protected]:2551" | |
] | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
akka.actor { | |
provider = "akka.cluster.ClusterActorRefProvider" | |
} | |
akka.remote.netty.tcp { | |
hostname = 127.0.0.1 | |
port = 2551 | |
} | |
akka.cluster { | |
metrics.enabled = off | |
jmx.enabled = off | |
seed-nodes = [ | |
"akka.tcp://[email protected]:2551" | |
] | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import akka.actor.*; | |
import akka.cluster.Cluster; | |
import akka.event.*; | |
import akka.japi.Creator; | |
import com.typesafe.config.ConfigFactory; | |
import java.util.concurrent.CountDownLatch; | |
import static akka.cluster.ClusterEvent.*; | |
public class TestClusterSelfLeave extends UntypedActor { | |
public static class ActorCreator implements Creator<TestClusterSelfLeave> { | |
final boolean seed; | |
public ActorCreator(boolean seed) { | |
this.seed = seed; | |
} | |
@Override | |
public TestClusterSelfLeave create() throws Exception { | |
return new TestClusterSelfLeave(seed); | |
} | |
} | |
final LoggingAdapter log = Logging.getLogger(context().system(), this); | |
final Cluster cluster; | |
final boolean seed; | |
public TestClusterSelfLeave(boolean seed) { | |
this.seed = seed; | |
cluster = Cluster.get(context().system()); | |
cluster.subscribe(self(), initialStateAsEvents(), MemberEvent.class); | |
} | |
@Override | |
public void onReceive(Object message) throws Throwable { | |
log.info("Received message: {}", message); | |
if (message instanceof MemberUp) { | |
// For the purpose of this test for non-seed node as soon as the cluster is formed, | |
// which is when we get MemberUp messages. | |
if (!seed) { | |
cluster.leave(cluster.selfAddress()); | |
} | |
} else if (message instanceof MemberRemoved) { | |
if (cluster.selfAddress().equals(((MemberRemoved) message).member().address())) { | |
// If this node just left, then terminate the system. | |
context().system().terminate(); | |
} else { | |
// The non-seed node just left (or another node), for the purpose of this test, leave the cluster too. | |
cluster.leave(cluster.selfAddress()); | |
} | |
} | |
} | |
public static void main(String[] args) throws InterruptedException { | |
final ActorCreator seedCreator = new ActorCreator(true); | |
final ActorCreator looseCreator = new ActorCreator(false); | |
final CountDownLatch nettyLatch = new CountDownLatch(2); | |
// Seed Netty actor system with dummy actor. | |
final ActorSystem seedSystemNetty = ActorSystem.create("test1", ConfigFactory.parseResources("seed-node-netty.conf")); | |
seedSystemNetty.registerOnTermination(nettyLatch::countDown); | |
seedSystemNetty.actorOf(Props.create(seedCreator)); | |
// Loose Netty actor system with dummy actor. | |
final ActorSystem looseSystemNetty = ActorSystem.create("test1", ConfigFactory.parseResources("loose-node-netty.conf")); | |
looseSystemNetty.registerOnTermination(nettyLatch::countDown); | |
looseSystemNetty.actorOf(Props.create(looseCreator)); | |
nettyLatch.await(); | |
System.out.println("\n\n-------------Netty systems shutdown properly-------------\n\n"); | |
final CountDownLatch arteryLatch = new CountDownLatch(2); | |
// Seed Artery actor system with dummy actor. | |
final ActorSystem seedSystemArtery = ActorSystem.create("test2", ConfigFactory.parseResources("seed-node-artery.conf")); | |
seedSystemArtery.registerOnTermination(arteryLatch::countDown); | |
seedSystemArtery.actorOf(Props.create(seedCreator)); | |
// Loose Artery actor system with dummy actor. | |
final ActorSystem looseSystemArtery = ActorSystem.create("test2", ConfigFactory.parseResources("loose-node-artery.conf")); | |
looseSystemArtery.registerOnTermination(arteryLatch::countDown); | |
looseSystemArtery.actorOf(Props.create(looseCreator)); | |
arteryLatch.await(); | |
System.out.println("\n\n-------------Artery systems shutdown properly-------------"); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment