Dominik Gruber, @the_dom
Vienna Scala User Group – Feb. 20, 2014
Akka Concurrency
•

521 pages

•

Written by Derek Wyatt, Foreword by Roland Kuhn

•

Published in May 2013

•

Based on A...
Prerequisites
•

Basic Scala Knowledge

•

No Akka Knowledge

•

“Principles of Reactive Programming” Course: Half of
the ...
Pros
•

Big focus on testing

•

One big, concise example throughout the book

•

Exhaustive overview

Akka Concurrency

D...
Cons
•

Based on Akka 2.1
•

No Akka Cluster

•

No Akka Persistence

Akka Concurrency

Dominik Gruber • @the_dom
Table of Contents, I
•

Why Akka?, Concurrency and Parallelism, Actors,…

•

Akka Testing

•

Supervision and DeathWatch

...
Table of Contents, II
•

Futures, Networking, Remote Actors

•

Sharing Data with Agents

•

Granular Concurrency with Dat...
Some Examples

Akka Concurrency

Dominik Gruber • @the_dom
Being Stateful
def expectHello: Receive = {
case “Hello” =>
sender ! “Goodbye”
context.become(expectGoodbye)
}
def expectG...
FSM
sealed trait State
case object Idle extends State
case object Active extends State

!
sealed trait Data
case object Un...
class Buncher extends Actor with FSM[State, Data] {

startWith(Idle, Uninitialized)

when(Idle) {
case Event(SetTarget(ref...
class Buncher extends Actor with FSM[State, Data] {
//…
onTransition {
case Active -> Idle =>
stateData match {
case Todo(...
Routers
•

RoundRobinRouter

•

RandomRouter

•

BroadcastRouter

•

SmallestMailboxRouter

•

ScatterGatherFirstCompleted...
Dataflow Concurrency
def calculatePiTo(places: Int): Future[BigDecimal] = ???
def fibonaccis(n: Int): Future[Seq[BigDecima...
Akka Concurrency

Dominik Gruber • @the_dom
Future
val result = for {
i <- Future(5)
j <- Future(11)

Blocking!

} yield j

Akka Concurrency

Dominik Gruber • @the_do...
Future
Translates to:
val result = fiveFuture.flatMap { i =>
Future(11) map { j => j }
}

Akka Concurrency

Dominik Gruber...
Future
val fiveFuture = Future(5)
val elevenFuture = Future(11)
val result = for {
i <- fiveFuture

Non-Blocking

j <- ele...
Future
Translates to:
val result = fiveFuture.flatMap { i =>
elevenFuture map { j => j }
}

Akka Concurrency

Dominik Grub...
2014-02-20 | Akka Concurrency (Vienna Scala User Group)
2014-02-20 | Akka Concurrency (Vienna Scala User Group)
Upcoming SlideShare
Loading in …5
×

2014-02-20 | Akka Concurrency (Vienna Scala User Group)

1,226 views

Published on

Book review of "Akka Concurrency" by Derek Wyatt

Talk given at Vienna Scala User Group on Feb. 20, 2014

Published in: Technology
0 Comments
3 Likes
Statistics
Notes
  • Be the first to comment

No Downloads
Views
Total views
1,226
On SlideShare
0
From Embeds
0
Number of Embeds
6
Actions
Shares
0
Downloads
6
Comments
0
Likes
3
Embeds 0
No embeds

No notes for slide

2014-02-20 | Akka Concurrency (Vienna Scala User Group)

  1. 1. Dominik Gruber, @the_dom Vienna Scala User Group – Feb. 20, 2014
  2. 2. 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) Akka Concurrency Dominik Gruber • @the_dom
  3. 3. Prerequisites • Basic Scala Knowledge • No Akka Knowledge • “Principles of Reactive Programming” Course: Half of the content will be new Akka Concurrency Dominik Gruber • @the_dom
  4. 4. Pros • Big focus on testing • One big, concise example throughout the book • Exhaustive overview Akka Concurrency Dominik Gruber • @the_dom
  5. 5. Cons • Based on Akka 2.1 • No Akka Cluster • No Akka Persistence Akka Concurrency Dominik Gruber • @the_dom
  6. 6. Table of Contents, I • Why Akka?, Concurrency and Parallelism, Actors,… • Akka Testing • Supervision and DeathWatch • Being Stateful • Routing Messages • Dispatchers and Mailboxes Akka Concurrency Dominik Gruber • @the_dom
  7. 7. 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 Akka Concurrency Dominik Gruber • @the_dom
  8. 8. Some Examples Akka Concurrency Dominik Gruber • @the_dom
  9. 9. Being Stateful def expectHello: Receive = { case “Hello” => sender ! “Goodbye” context.become(expectGoodbye) } def expectGoodbye: Receive = { case “Goodbye” => sender ! “Hello” context.become(expectHello) } def receive = expectHello Akka Concurrency Dominik Gruber • @the_dom
  10. 10. FSM sealed 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 Akka Concurrency Dominik Gruber • @the_dom
  11. 11. 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) } // … } Akka Concurrency Dominik Gruber • @the_dom
  12. 12. 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 } } Akka Concurrency Dominik Gruber • @the_dom
  13. 13. Routers • RoundRobinRouter • RandomRouter • BroadcastRouter • SmallestMailboxRouter • ScatterGatherFirstCompletedRouter Akka Concurrency Dominik Gruber • @the_dom
  14. 14. Dataflow Concurrency def 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 } Akka Concurrency Dominik Gruber • @the_dom
  15. 15. Akka Concurrency Dominik Gruber • @the_dom
  16. 16. Future val result = for { i <- Future(5) j <- Future(11) Blocking! } yield j Akka Concurrency Dominik Gruber • @the_dom
  17. 17. Future Translates to: val result = fiveFuture.flatMap { i => Future(11) map { j => j } } Akka Concurrency Dominik Gruber • @the_dom
  18. 18. Future val fiveFuture = Future(5) val elevenFuture = Future(11) val result = for { i <- fiveFuture Non-Blocking j <- elevenFuture } yield j Akka Concurrency Dominik Gruber • @the_dom
  19. 19. Future Translates to: val result = fiveFuture.flatMap { i => elevenFuture map { j => j } } Akka Concurrency Dominik Gruber • @the_dom

×