2018-03-13 23:45:55 +09:00
|
|
|
/*
|
2019-01-02 18:55:26 +08:00
|
|
|
* Copyright (C) 2018-2019 Lightbend Inc. <https://www.lightbend.com>
|
2018-03-13 23:45:55 +09:00
|
|
|
*/
|
|
|
|
|
|
2017-02-14 13:10:23 +02:00
|
|
|
package scala.docs.cluster
|
2013-11-29 16:27:23 +01:00
|
|
|
|
|
|
|
|
import scala.annotation.tailrec
|
|
|
|
|
import scala.concurrent.Future
|
|
|
|
|
import com.typesafe.config.ConfigFactory
|
|
|
|
|
import akka.actor.Actor
|
|
|
|
|
import akka.actor.ActorLogging
|
|
|
|
|
import akka.actor.ActorSystem
|
|
|
|
|
import akka.actor.Props
|
|
|
|
|
import akka.pattern.pipe
|
|
|
|
|
|
|
|
|
|
//#backend
|
|
|
|
|
class FactorialBackend extends Actor with ActorLogging {
|
|
|
|
|
|
|
|
|
|
import context.dispatcher
|
|
|
|
|
|
|
|
|
|
def receive = {
|
2019-02-09 15:25:39 +01:00
|
|
|
case (n: Int) =>
|
|
|
|
|
Future(factorial(n)) map { result => (n, result) } pipeTo sender()
|
2013-11-29 16:27:23 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
def factorial(n: Int): BigInt = {
|
|
|
|
|
@tailrec def factorialAcc(acc: BigInt, n: Int): BigInt = {
|
|
|
|
|
if (n <= 1) acc
|
|
|
|
|
else factorialAcc(acc * n, n - 1)
|
|
|
|
|
}
|
|
|
|
|
factorialAcc(BigInt(1), n)
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
}
|
|
|
|
|
//#backend
|
|
|
|
|
|
|
|
|
|
object FactorialBackend {
|
|
|
|
|
def main(args: Array[String]): Unit = {
|
|
|
|
|
// Override the configuration of the port when specified as program argument
|
|
|
|
|
val port = if (args.isEmpty) "0" else args(0)
|
|
|
|
|
val config = ConfigFactory.parseString(s"akka.remote.netty.tcp.port=$port").
|
|
|
|
|
withFallback(ConfigFactory.parseString("akka.cluster.roles = [backend]")).
|
|
|
|
|
withFallback(ConfigFactory.load("factorial"))
|
|
|
|
|
|
|
|
|
|
val system = ActorSystem("ClusterSystem", config)
|
|
|
|
|
system.actorOf(Props[FactorialBackend], name = "factorialBackend")
|
|
|
|
|
|
|
|
|
|
system.actorOf(Props[MetricsListener], name = "metricsListener")
|
|
|
|
|
}
|
2017-10-06 10:30:28 +02:00
|
|
|
}
|