View
473
Download
4
Category
Preview:
DESCRIPTION
Book review of "Akka Concurrency" by Derek Wyatt Talk given at Vienna Scala User Group on Feb. 20, 2014
Citation preview
Dominik Gruber, @the_dom Vienna Scala User Group – Feb. 20, 2014
Dominik Gruber • @the_domAkka Concurrency
Akka Concurrency• 521 pages
• Written by Derek Wyatt, Foreword by Roland Kuhn
• Published in May 2013
• Based on Akka 2.1
• EUR 17,– (Kindle) / EUR 29,95 (Dead Tree)
Dominik Gruber • @the_domAkka Concurrency
Prerequisites• Basic Scala Knowledge
• No Akka Knowledge
• “Principles of Reactive Programming” Course: Half of the content will be new
Dominik Gruber • @the_domAkka Concurrency
Pros
• Big focus on testing
• One big, concise example throughout the book
• Exhaustive overview
Dominik Gruber • @the_domAkka Concurrency
Cons
• Based on Akka 2.1
• No Akka Cluster
• No Akka Persistence
Dominik Gruber • @the_domAkka Concurrency
Table of Contents, I• Why Akka?, Concurrency and Parallelism, Actors,…
• Akka Testing
• Supervision and DeathWatch
• Being Stateful
• Routing Messages
• Dispatchers and Mailboxes
Dominik Gruber • @the_domAkka Concurrency
Table of Contents, II• Futures, Networking, Remote Actors
• Sharing Data with Agents
• Granular Concurrency with Dataflow
• Patterns / Antipatterns
• Add-On Modules
• Using Akka from Java
Dominik Gruber • @the_domAkka Concurrency
Some Examples
Dominik Gruber • @the_domAkka Concurrency
Being Statefuldef expectHello: Receive = {
case “Hello” =>
sender ! “Goodbye”
context.become(expectGoodbye)
}
def expectGoodbye: Receive = {
case “Goodbye” =>
sender ! “Hello”
context.become(expectHello)
}
def receive = expectHello
Dominik Gruber • @the_domAkka Concurrency
FSMsealed trait State
case object Idle extends State
case object Active extends State
!
sealed trait Data
case object Uninitialized extends Data
case class Todo(target: ActorRef, queue: immutable.Seq[Any]) extends Data
Dominik Gruber • @the_domAkka Concurrency
class Buncher extends Actor with FSM[State, Data] {
startWith(Idle, Uninitialized)
when(Idle) {
case Event(SetTarget(ref), Uninitialized) =>
stay using Todo(ref, Vector.empty)
}
when(Active, stateTimeout = 1 second) {
case Event(Flush | StateTimeout, t: Todo) =>
goto(Idle) using t.copy(queue = Vector.empty)
}
// …
}
Dominik Gruber • @the_domAkka Concurrency
class Buncher extends Actor with FSM[State, Data] {
//…
onTransition {
case Active -> Idle =>
stateData match {
case Todo(ref, queue) => ref ! Batch(queue)
}
}
whenUnhandled {
// common code for both states
case Event(Queue(obj), t @ Todo(_, v)) =>
goto(Active) using t.copy(queue = v :+ obj)
case Event(e, s) =>
log.warning("received unhandled request {} in state {}/{}", e, stateName, s)
stay
}
}
Dominik Gruber • @the_domAkka Concurrency
Routers• RoundRobinRouter
• RandomRouter
• BroadcastRouter
• SmallestMailboxRouter
• ScatterGatherFirstCompletedRouter
Dominik Gruber • @the_domAkka Concurrency
Dataflow Concurrencydef calculatePiTo(places: Int): Future[BigDecimal] = ???
def fibonaccis(n: Int): Future[Seq[BigDecimal]] = ???
val perfect = flow {
val pie = calculatePiTo(3000000)
val fibs = fibonaccis(31402)
val lastFibs = fibs().last
pie() * lastFibs * lastFibs
}
Dominik Gruber • @the_domAkka Concurrency
Dominik Gruber • @the_domAkka Concurrency
Future
val result = for {
i <- Future(5)
j <- Future(11)
} yield j
Blocking!
Dominik Gruber • @the_domAkka Concurrency
Future
Translates to:
val result = fiveFuture.flatMap { i =>
Future(11) map { j => j }
}
Dominik Gruber • @the_domAkka Concurrency
Futureval fiveFuture = Future(5)
val elevenFuture = Future(11)
val result = for {
i <- fiveFuture
j <- elevenFuture
} yield j
Non-Blocking
Dominik Gruber • @the_domAkka Concurrency
Future
Translates to:
val result = fiveFuture.flatMap { i =>
elevenFuture map { j => j }
}
Recommended