Created
July 27, 2013 07:26
-
-
Save patriknw/6094123 to your computer and use it in GitHub Desktop.
Minimized sample of cluster router
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
package sample.cluster.simple | |
import scala.concurrent.duration._ | |
import akka.actor._ | |
import akka.cluster.Cluster | |
import akka.cluster.ClusterEvent._ | |
import akka.routing.FromConfig | |
import com.typesafe.config.ConfigFactory | |
object SimpleRouterApp { | |
/** | |
* Start with: | |
* sbt run-main sample.cluster.simple.SimpleRouterApp 2551 | |
* sbt run-main sample.cluster.simple.SimpleRouterApp 2552 | |
* sbt run-main sample.cluster.simple.SimpleRouterApp 2553 | |
*/ | |
def main(args: Array[String]): Unit = { | |
// Override the configuration of the port | |
// when specified as program argument | |
if (args.nonEmpty) System.setProperty("akka.remote.netty.tcp.port", args(0)) | |
val conf = ConfigFactory.load(ConfigFactory.parseString(""" | |
akka.actor { | |
provider = "akka.cluster.ClusterActorRefProvider" | |
} | |
akka.remote { | |
log-remote-lifecycle-events = off | |
netty.tcp { | |
hostname = "127.0.0.1" | |
port = 0 | |
} | |
} | |
akka.cluster { | |
seed-nodes = [ | |
"akka.tcp://ClusterSystem@127.0.0.1:2551", | |
"akka.tcp://ClusterSystem@127.0.0.1:2552"] | |
auto-down = on | |
} | |
akka.log-dead-letters=1000 | |
akka.actor.deployment { | |
/producer/router { | |
router = round-robin | |
nr-of-instances = 100 | |
cluster { | |
enabled = on | |
routees-path = "/user/routee" | |
allow-local-routees = on | |
} | |
} | |
}""")) | |
val system = ActorSystem("ClusterSystem", conf) | |
Cluster(system) | |
system.actorOf(Props[Routee], name = "routee") | |
system.actorOf(Props[Producer], name = "producer") | |
} | |
} | |
class Routee extends Actor with ActorLogging { | |
def receive = { | |
case msg ⇒ | |
log.info("Routee received: {} from {}", msg, sender.path) | |
} | |
} | |
class Producer extends Actor with ActorLogging { | |
val router = context.actorOf(Props.empty.withRouter(FromConfig), | |
name = "router") | |
import context.dispatcher | |
val tickTask = context.system.scheduler.schedule(3.seconds, 3.seconds, self, "tick") | |
var count = 0 | |
override def postStop(): Unit = { | |
tickTask.cancel() | |
} | |
def receive = { | |
case "tick" ⇒ | |
count += 1 | |
router ! count | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment