|
Akka/Scala example source code file (FactorialFrontend.scala)
The FactorialFrontend.scala Akka example source codepackage sample.cluster.factorial import scala.concurrent.duration._ import com.typesafe.config.ConfigFactory import akka.actor.Actor import akka.actor.ActorLogging import akka.actor.ActorSystem import akka.actor.Props import akka.cluster.Cluster import akka.routing.FromConfig import akka.actor.ReceiveTimeout //#frontend class FactorialFrontend(upToN: Int, repeat: Boolean) extends Actor with ActorLogging { val backend = context.actorOf(FromConfig.props(), name = "factorialBackendRouter") override def preStart(): Unit = { sendJobs() if (repeat) { context.setReceiveTimeout(10.seconds) } } def receive = { case (n: Int, factorial: BigInt) => if (n == upToN) { log.debug("{}! = {}", n, factorial) if (repeat) sendJobs() else context.stop(self) } case ReceiveTimeout => log.info("Timeout") sendJobs() } def sendJobs(): Unit = { log.info("Starting batch of factorials up to [{}]", upToN) 1 to upToN foreach { backend ! _ } } } //#frontend object FactorialFrontend { def main(args: Array[String]): Unit = { val upToN = 200 val config = ConfigFactory.parseString("akka.cluster.roles = [frontend]"). withFallback(ConfigFactory.load("factorial")) val system = ActorSystem("ClusterSystem", config) system.log.info("Factorials will start when 2 backend members in the cluster.") //#registerOnUp Cluster(system) registerOnMemberUp { system.actorOf(Props(classOf[FactorialFrontend], upToN, true), name = "factorialFrontend") } //#registerOnUp } } Other Akka source code examplesHere is a short list of links related to this Akka FactorialFrontend.scala source code file: |
... this post is sponsored by my books ... | |
#1 New Release! |
FP Best Seller |
Copyright 1998-2024 Alvin Alexander, alvinalexander.com
All Rights Reserved.
A percentage of advertising revenue from
pages under the /java/jwarehouse
URI on this website is
paid back to open source projects.