As a Scala developer you will probably want to tell us that there is no main(Array[String]) method anywhere in these classes, so how do we run this program? The answer is that the appropriate main method is implemented in the generic launcher class akka.Main which expects only one command line argument: the class name of the application’s main actor. This main method will then create the infrastructure needed for running the actors, start the given main actor and arrange for the whole application to shut down once the main actor terminates. Thus you will be able to run the above code with a command similar to the following:
> java -classpath <all those JARs> akka.Main com.example.HelloWorl
import akka.actor.Actor
import akka.actor.Props
class HelloWorld extends Actor {
override def preStart(): Unit = {
// create the greeter actor
val greeter = context.actorOf(Props[Greeter], "greeter")
// tell it to perform the greeting
greeter ! Greeter.Greet
}
def receive = {
// when the greeter is done, stop this actor and with it the application
case Greeter.Done ⇒ context.stop(self)
}
}
object Greeter {
case object Greet
case object Done
}
class Greeter extends Actor {
def receive = {
case Greeter.Greet ⇒
println("Hello World!")
sender ! Greeter.Done
}
}
No comments:
Post a Comment