Akka(3)Start with first Project - Second Part
Creating the result Listener
class Listener extends Actor { // Everything is Actor
def receive = {
case PiApproximation(pi, duration) =>
println("\n\tPi approximation: \t\t%s\n\tCalculation time: \t%s".format(pi, duration))
//print out the result
context.system.shutdown()
//shutdown the system
}
}
Bootstrap the calculation
We will extend the App trait in Scala, which means that we will be able to run this as an application directly from the command line.
object Pi extends App {
calculate(nrOfWorkers = 4, nrOfElements = 10000, nrOfMessages = 10000)
…snip...
def calculate(nrOfWorkers: Int, nrOfElements: Int, nrOfMessages: Int) {
// Create an Akka system
val system = ActorSystem("PiSystem")
// create the result listener, which will print the result and
// shutdown the system
val listener = system.actorOf(Props[Listener], name = "listener")
// create the master
val master = system.actorOf(Props(new Master(
nrOfWorkers, nrOfMessages, nrOfElements, listener)),
name = "master")
// start the calculation by send the Calculate message to master actor
master ! Calculate
}
}
We also have a test case there, I can run that via Eclipse JUnit and Scala Test.
No, I can not understand that. I will go on with the documents to learn more.
References:
http://doc.akka.io/docs/akka/2.1.0/
http://doc.akka.io/docs/akka/2.1.0/general/index.html
https://github.com/typesafehub
http://typesafe.com/resources/tutorials/getting-started-with-akka-scala.html#getting-started-with-akka-scala
http://alvinalexander.com/scala/java.lang.nosuchmethoderror-scala.predef.augmentstring-error
http://stackoverflow.com/questions/11203268/what-is-a-sealed-trait
Creating the result Listener
class Listener extends Actor { // Everything is Actor
def receive = {
case PiApproximation(pi, duration) =>
println("\n\tPi approximation: \t\t%s\n\tCalculation time: \t%s".format(pi, duration))
//print out the result
context.system.shutdown()
//shutdown the system
}
}
Bootstrap the calculation
We will extend the App trait in Scala, which means that we will be able to run this as an application directly from the command line.
object Pi extends App {
calculate(nrOfWorkers = 4, nrOfElements = 10000, nrOfMessages = 10000)
…snip...
def calculate(nrOfWorkers: Int, nrOfElements: Int, nrOfMessages: Int) {
// Create an Akka system
val system = ActorSystem("PiSystem")
// create the result listener, which will print the result and
// shutdown the system
val listener = system.actorOf(Props[Listener], name = "listener")
// create the master
val master = system.actorOf(Props(new Master(
nrOfWorkers, nrOfMessages, nrOfElements, listener)),
name = "master")
// start the calculation by send the Calculate message to master actor
master ! Calculate
}
}
We also have a test case there, I can run that via Eclipse JUnit and Scala Test.
No, I can not understand that. I will go on with the documents to learn more.
References:
http://doc.akka.io/docs/akka/2.1.0/
http://doc.akka.io/docs/akka/2.1.0/general/index.html
https://github.com/typesafehub
http://typesafe.com/resources/tutorials/getting-started-with-akka-scala.html#getting-started-with-akka-scala
http://alvinalexander.com/scala/java.lang.nosuchmethoderror-scala.predef.augmentstring-error
http://stackoverflow.com/questions/11203268/what-is-a-sealed-trait