This doucment will briefly introduce some important parts of Akka specific for the implementation of this kata.
The official Akka documentation is really good (if we may say so ourselves): Akka Docs
Please note that this document describes parts of Akka very briefly and we refer to the original documentation for an in-depth description of Akka.
Below is a brief introduction of some concepts you will need for this kata with some pointers to where you can read more.
An actor system is, among other things, the context in which actors operate. You can have multiple actor systems within the same JVM.
See Actor Systems
Creating ActorSystems
val system = ActorSystem("MyActorSystem")
See Actors
Creating actors
In the system context, called top level actors (to be used sparsely). This creates an actor under "/user/myActorName".
val myActor = system.actorOf(Props[MyActor], "myActorName")
In the actor context, called children (i.e. when you're inside an actor)
val myActor = context.actorOf(Props[MyActor], "myActorName")
Looking up actors
Look up an actor by name (complete path)
val myActor = system.actorFor("/user/myActorName")
val remoteActor = system.actorFor("akka://OtherActorSystem@host:port/user/otherActorName")
Sending messages
Fire and forget
myActor ! "A message"
As futures (has performance implications)
import akka.pattern.ask
val myFuture = myActor ? "A message"
Passing along the original sender
myActor.forward("Another message")
Receiving messages
class MyActor extends Actor {
def receive = {
case "A message" => println(""Received the message")
case "Another message" => println("Received that other message")
}
}
Supervising actors
See Fault Tolerance
override val supervisorStrategy = OneForOneStrategy() {
case _ => Restart // Stop, ...
}
Scheduling messages
To schedule a message to be sent sometime in the future, once or repeatedly use the scheduler.
See Scheduler
system.scheduler.schedule(2 seconds, 2 seconds, actor, "every other second message")
Retrieving properties
As you can see in the code there are some properties predefined in the application.conf
file. To retrieve these properties, in the context of an actor, you can use the following:
context.system.settings.config.getString("...")