Simple concurrency with an Akka Hello World sample
akka, scala
Solution
For your use case you'll probably want to use Routers.
For example:
val hello = system.actorOf(Props[HelloActor].withRouter(
RoundRobinRouter(nrOfInstances = 10)))
hello ! HelloActor.SayHello("Hello!") // Sends to one of the 10
As a side note, you should avoid blocking (ie. `Thread.sleep`) in your actor's `receive` method.
Problem
I'm evaluating Akka for a distributed service layer, the following example prints Hello {n} 10 times, but does it one after the other. As I understand it this is intentional for an Akka actor, so where do I go from here to make it concurrent? ``` import akka.actor._ object HelloActor { case class SayHello(message: String) } class HelloActor extends Actor { def receive = { case HelloActor.SayHello(message) => Thread.sleep(1000) println(message) } } object Main extends App { val system = ActorSystem("ActorSystem") val hello = system.actorOf(Props[HelloActor]) for (i <- 1 to 10) { hello ! HelloActor.SayHello(s"Hello $i") } } ``` I've experimented with creating multiple actors from the Main class but that feels wrong somehow, shouldn't I just call the actor then it handles concurrency / spawning more actors on its own? Could anyone provide an example of this (preferably modifying the above code). I've been reading and reading but it feels like a lot to take in immediately and I feel I'm just missing a key concept here somewhere.