akka { actor { provider = "cluster" } }
object EventListner { trait Messages {} case object Leave extends Messages case object Down extends Messages def props = Props(new EventListener) def create(host: String = "localhost", port: Int = 0, seednode: String = "") = { var config = ConfigFactory.parseString(s"akka.remote.netty.tcp.hostname=${host}") .withFallback(ConfigFactory.parseString(s"akka.remote.netty.tcp.port=${port}")) if (seednode.length > 0) { val strConfig = "akka.cluster.seed-nodes=[\"" + seednode + "\"]" val configSeed = ConfigFactory.parseString(strConfig) config = config.withFallback(configSeed) } config = config.withFallback(ConfigFactory.load("akka-cluster-config")) val clusterSystem = ActorSystem(name="ClusterSystem",config=config) clusterSystem.actorOf(Props[EventListener]) } }
import EventListner._ object EventDemo extends App { val listner1 = EventListner.create(port = 2551) //seed node
scala.io.StdIn.readLine() val listner2 = EventListner.create() //port=0 random port
scala.io.StdIn.readLine() val listner3 = EventListner.create() //port=0 random port
scala.io.StdIn.readLine() listner3 ! Leave scala.io.StdIn.readLine() listner2 ! Down scala.io.StdIn.readLine() listner1 ! Leave scala.io.StdIn.readLine() }
[INFO] [10/22/2018 18:50:40.888] [main] [akka.cluster.Cluster(akka://ClusterSystem)] Cluster Node [akka.tcp://ClusterSystem@localhost:2551] - Started up successfully
[INFO] [10/22/2018 18:50:40.931] [ClusterSystem-akka.actor.default-dispatcher-3] [akka.cluster.Cluster(akka://ClusterSystem)] Cluster Node [akka.tcp://ClusterSystem@localhost:2551] - Node [akka.tcp://ClusterSystem@localhost:2551] is JOINING itself (with roles [dc-default]) and forming new cluster
[INFO] [10/22/2018 18:50:40.933] [ClusterSystem-akka.actor.default-dispatcher-3] [akka.cluster.Cluster(akka://ClusterSystem)] Cluster Node [akka.tcp://ClusterSystem@localhost:2551] - Cluster Node [akka.tcp://ClusterSystem@localhost:2551] dc [default] is the new leader
[INFO] [10/22/2018 18:50:40.943] [ClusterSystem-akka.actor.default-dispatcher-3] [akka.cluster.Cluster(akka://ClusterSystem)] Cluster Node [akka.tcp://ClusterSystem@localhost:2551] - Leader is moving node [akka.tcp://ClusterSystem@localhost:2551] to [Up]
[INFO] [10/22/2018 18:50:41.037] [ClusterSystem-akka.actor.default-dispatcher-4] [akka.tcp://ClusterSystem@localhost:2551/user/$a] akka.tcp://ClusterSystem@localhost:2551 is UP!
[INFO] [10/22/2018 18:50:47.363] [ClusterSystem-akka.actor.default-dispatcher-23] [akka.tcp://ClusterSystem@localhost:51679/user/$a] akka.tcp://ClusterSystem@localhost:51679 is JOINING...
[INFO] [10/22/2018 18:50:47.930] [ClusterSystem-akka.actor.default-dispatcher-4] [akka.cluster.Cluster(akka://ClusterSystem)] Cluster Node [akka.tcp://ClusterSystem@localhost:2551] - Leader is moving node [akka.tcp://ClusterSystem@localh