Skip to content

Instantly share code, notes, and snippets.

@bhameyie
Last active August 29, 2015 14:14
Show Gist options
  • Save bhameyie/6bb9d49ef0acec14ed81 to your computer and use it in GitHub Desktop.
Save bhameyie/6bb9d49ef0acec14ed81 to your computer and use it in GitHub Desktop.
Vanilla Akka Cluster
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
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
}
}
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
}
}
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)
}
}
case class PrintIt(msg:String)
object Channels {
val printingChannel="printingEvents"
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment