Last active
August 29, 2015 14:14
-
-
Save bhameyie/6bb9d49ef0acec14ed81 to your computer and use it in GitHub Desktop.
Vanilla Akka Cluster
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
clustering{ | |
host = "127.0.0.1" | |
host = ${?HOSTIP} | |
port = 9551 | |
port = ${?CLUSTER_PORT} | |
node-host = "127.0.0.1" | |
node-host = ${?NODE_HOST} | |
node-port = 9551 # different for each project | |
node-port = ${?NODE_PORT} | |
} | |
# Akka related configuration | |
akka { | |
debug { | |
receive = on | |
} | |
actor { | |
provider = "akka.cluster.ClusterActorRefProvider" | |
} | |
remote { | |
log-remote-lifecycle-events = on | |
enabled-transports = ["akka.remote.netty.tcp"] | |
netty.tcp { | |
hostname = ${clustering.node-host} | |
port = ${clustering.node-port} | |
} | |
} | |
cluster { | |
seed-nodes = ["akka.tcp://47Ronin@"${clustering.host}":"${clustering.port}] | |
auto-down-unreachable-after = 10s | |
} | |
} | |
akka.contrib.cluster.pub-sub.gossip-interval = 5s |
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
object Main { | |
def main(args: Array[String]): Unit = { | |
val systemName = "47Ronin" | |
val system = ActorSystem(systemName) | |
val mediator = DistributedPubSubExtension(system).mediator | |
mediator ! Publish(Channels.printingChannel, | |
PrintIt("print it I say")) // to send to all | |
mediator ! Send(Channels.printingChannel, | |
PrintIt("print it I say")) // to send to a single printer dude | |
} | |
} |
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
class MemberListener extends Actor with ActorLogging { | |
val cluster = Cluster(context.system) | |
override def preStart(): Unit = { | |
cluster.subscribe(self, initialStateMode = InitialStateAsEvents, | |
classOf[MemberEvent], classOf[UnreachableMember]) | |
} | |
override def postStop(): Unit = cluster.unsubscribe(self) | |
def receive = { | |
case MemberUp(member) => | |
log.info("Member is Up: {}", member.address) | |
case UnreachableMember(member) => | |
log.info("Member detected as unreachable: {}", member) | |
case MemberRemoved(member, previousStatus) => | |
log.info("Member is Removed: {} after {}", | |
member.address, previousStatus) | |
case _: MemberEvent => // ignore | |
} | |
} |
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
class PrinterDude extends Actor { | |
val mediator = DistributedPubSubExtension(context.system).mediator // Artifact from Akka Cluster found in akka contrib | |
mediator ! Subscribe(Channels.printingChannel, self) | |
def receive = { | |
case PrintIt(msg) => Console.println(msg) | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment