-
-
Notifications
You must be signed in to change notification settings - Fork 2
/
ScalaAkkaActorsAggregate.scala
51 lines (40 loc) · 1.43 KB
/
ScalaAkkaActorsAggregate.scala
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
package actors
import scala.language.{ postfixOps }
import scala.concurrent.ExecutionContext.Implicits.global
import scala.concurrent.{ Await, Future }
import scala.concurrent.duration._
import services.scala.{ Item, ItemService }
import akka.actor._
import akka.pattern.ask
import akka.util.Timeout
import actors.ItemServiceAggregator._
object ScalaAkkaActorsAggregate extends App {
val system = ActorSystem()
val itemService = system.actorOf(Props[ItemServiceAggregator], "itemServiceAggregator")
// available clients
val clients = 1 until 10 toSeq
// how long until the ask times out
implicit val timeout = Timeout(10 seconds)
// start the futures
val statsFuture: Future[ItemStatistics] = (itemService ? GetItemStatistics(clients)).mapTo[ItemStatistics]
val itemsFuture = statsFuture map {
case ItemStatistics(results) => results.map(_._2).flatten
}
val items = Await.result(itemsFuture, 10 seconds)
// --------------------------- //
// Same code like ScalaFutures //
// --------------------------- //
val groupedByPrice = items groupBy (_.price)
// show results
groupedByPrice foreach {
case (price, items) =>
val itemsPerClient = items groupBy (_.client)
val output = s"""|==== PRICE $price =====
| Items : ${items.size}
| Clients : ${itemsPerClient.size}
""".stripMargin
println(output)
}
// Shutdown actorsystem and exit program
system.shutdown()
}