Upgrade to Pro — share decks privately, control downloads, hide ads and more …

Akka (Actor) in Practice

Akka (Actor) in Practice

[Scala Night](https://festa.io/events/18) 에서 발표한 [Akka in Practice] 발표 자료입니다.

Sunghyun Hwang

April 14, 2018
Tweet

More Decks by Sunghyun Hwang

Other Decks in Programming

Transcript

  1. !3

  2. !4

  3. !8 class Pizza { private var slices = 8 def

    eat(nrOfSlices: Int = 1): Int = { if (slices >= nrOfSlices) { slices = slices - nrOfSlices return nrOfSlices } else { throw OutOfPizzaException } } }
  4. !9 class Pizza { private var slices = 8 def

    eat(nrOfSlices: Int = 1): Int = { if (slices >= nrOfSlices) { slices = slices - nrOfSlices return nrOfSlices } else { throw OutOfPizzaException } } }
  5. !19 .eat(6) .eat(3) Unfortunately, the encapsulation model of objects does

    not guarantee anything about what happens in that section.
  6. !20 Unfortunately, the encapsulation model of objects does not guarantee

    anything about what happens in that section. (…) Now, imagine this issue compounded by the existence of many threads. [2] .eat(6) .eat(3) .eat(2) .eat(8)
  7. An actor is a container for State, Behavior, a Mailbox,

    Child Actors and a Supervisor Strategy. All of this is encapsulated behind an Actor Reference. [3] !21
  8. OOP to me means only messaging, local retention and protection

    and hiding of state-process, and extreme late-binding of all things. — Alan Kay [4] !37
  9. The Actor model alone, while great for many use cases,

    do not solve all concurrency and scalability problems. !40
  10. The Actor model alone, while great for many use cases,

    do not solve all concurrency and scalability problems. (…) In addition to STM, Akka provides several other concurrency abstractions such as Agents and Dataflow Concurrency. [6] !41
  11. Akka is a toolkit for building highly concurrent, distributed, and

    resilient message-driven applications for Java and Scala [1] !42
  12. • Actor (Mostly) • I/O • Cluster • Cluster Tools

    • Persistence • Streams • Alpakka • HTTP • Typed !43 Akka
  13. !47 Recommending Actor Maximizing Actor Maximizing Actor Computing Actor Computing

    Actor Computing Actor Computing Actor Computing Actor Computing Actor
  14. !48 Recommending Actor Maximizing Actor Maximizing Actor Computing Actor Computing

    Actor Computing Actor Computing Actor Computing Actor Computing Actor
  15. !52 override def receive: Receive = { case WannaEatPizza(nr) ⇒

    eat(nr) case DoNotWannaEatPiazza ⇒ ??? }
  16. !53 override def receive: Receive = { case WannaEatPizza(nr) ⇒

    eat(nr) case DoNotWannaEatPiazza ⇒ ??? } type Receive = PartialFunction[Any, Unit]
  17. override def receive: Receive = { case WannaEatPizza(nr) ⇒ eat(nr)

    case OrderPizza(addr) ⇒ procOrder(addr) case CancelOrder(num) ⇒ cancel(num) case ShowPizzaMenu ⇒ sender ! pizzas case DoNotWannaEatPiazza ⇒ sender ! SoWhat }
  18. !57 class CounterActor extends Actor { var cnt: Int =

    0 override def receive = { case Tick ⇒ cnt = cnt + 1 sender ! cnt } }
  19. !58 def ticker(cnt: Int): Receive = { case Tick ⇒

    context become ticker(cnt + 1) }
  20. !62 class SomeActor extends Actor { override def receive =

    { case id: Int ⇒ val card = database.query(s"SELECT * FROM card WHERE id = $id") val cached = cache.get(card.slug) sender ! cached } }
  21. !63 Future { val card = database.query(s"SELECT * FROM card

    WHERE id = $id") cache.get(card.slug) }
  22. !64 val a1 = actor ? 1 val a2 =

    actor ? 2 for { r1 ← a1 r2 ← a2 } yield (r1, r2) val f1 = fut(1) val f2 = fut(2) for { r1 ← f1 r2 ← f2 } yield (r1, r2)
  23. class StreamActor extends Actor { override def receive = {

    case Data(x, y, z) ⇒ other ! convert(x, y, z) } } !66
  24. We are hiring! https://rainist.com/recruit/engineer • Back-end Engineer • Data Engineer

    • Data Scientist • Android Engineer • iOS Engineer • QA Engineer • Security Engineer • … X Engineer !73
  25. Materials !75 • [Akka Official Documents](https://akka.io) • [The Zen of

    Akka - by Konrad Malawski](https://www.youtube.com/ watch?v=vgFoKOxrTzg) • [The best is yet to come - State of Akka in 2017 by Konrad Malawski] (https://www.youtube.com/watch?v=CGyIPVGB2-4) • [Akka: Simpler Scalability, Fault-Tolerance, Concurrency & Remoting through Actors](https://www.slideshare.net/jboner/akka-simpler- scalability-faulttolerance-concurrency-remoting-through-actors/)
  26. !76 • [1] https://akka.io • [2] https://doc.akka.io/docs/akka/current/guide/actors-motivation.html#the- challenge-of-encapsulation • [3]

    https://doc.akka.io/docs/akka/2.5/general/actors.html#what-is-an-actor- • [4] http://www.purl.org/stefan_ram/pub/doc_kay_oop_en • [5] https://en.wikipedia.org/wiki/Actor_model#Fundamental_concepts • [6] https://www.lightbend.com/blog/why-akka • [7] <Agile Software Development, Principles, Patterns, and Practices> (2003, Martin, Robert C.) References