reactive programming with akka - lessons learned
DESCRIPTION
This presentation was given by Daniel Deogun and Daniel Sawano at the 33rd Degree Conference, 2014. When mentioning Akka, most of us think of a framework allowing one to design high performant, scalable, and fault tolerant systems. But the question is, how can one utilize the power of reactive programming when surrounded by legacy? In this talk, we will share our insights and experiences from developing high performance systems with Akka. Despite that Akka APIs are more favorable in Scala, we have chosen and successfully used Akka’s Java APIs. A strategy that may have significant impact on the business and the success of a project. In addition, we will present how domain specific requirements influences your design options, the traps we have walked into, and how everyone may benefit from Akka regardless of green or brown field development.TRANSCRIPT
![Page 1: Reactive Programming With Akka - Lessons Learned](https://reader030.vdocuments.site/reader030/viewer/2022012910/53faacd28d7f72b82e8b50e1/html5/thumbnails/1.jpg)
Platinum Sponsor
REACTIVE PROGRAMMING WITH AKKA -‐ LESSONS LEARNED -‐
Daniel Deogun & Daniel Sawano
Twitter: @DanielDeogun, @DanielSawano
![Page 2: Reactive Programming With Akka - Lessons Learned](https://reader030.vdocuments.site/reader030/viewer/2022012910/53faacd28d7f72b82e8b50e1/html5/thumbnails/2.jpg)
Who We Are
Daniel����������� ������������������ Deogun Daniel����������� ������������������ Sawano
Omegapoint����������� ������������������ Stockholm����������� ������������������ -����������� ������������������ Gothenburg����������� ������������������ -����������� ������������������ Malmoe����������� ������������������ -����������� ������������������ Umea����������� ������������������ -����������� ������������������ New����������� ������������������ York
![Page 3: Reactive Programming With Akka - Lessons Learned](https://reader030.vdocuments.site/reader030/viewer/2022012910/53faacd28d7f72b82e8b50e1/html5/thumbnails/3.jpg)
Agenda
- Akka����������� ������������������ in����������� ������������������ a����������� ������������������ nutshell����������� ������������������
- Akka����������� ������������������ &����������� ������������������ Java����������� ������������������ ����������� ������������������
- Akka����������� ������������������ and����������� ������������������ Java����������� ������������������ 8����������� ������������������ Lambdas����������� ������������������
- Domain����������� ������������������ influences����������� ������������������
- Lessons����������� ������������������ learned����������� ������������������ from����������� ������������������ building����������� ������������������ real����������� ������������������ systems����������� ������������������ with����������� ������������������ Akka
![Page 5: Reactive Programming With Akka - Lessons Learned](https://reader030.vdocuments.site/reader030/viewer/2022012910/53faacd28d7f72b82e8b50e1/html5/thumbnails/5.jpg)
Akka in a Nutshell
Actors
[1] By Dickelbers (Own work) [CC-BY-SA-3.0 (http://creativecommons.org/licenses/by-sa/3.0)], via Wikimedia Commons !http://upload.wikimedia.org/wikipedia/commons/0/09/Sweden_postbox.JPG
Messages
Mailbox[1]
Actor����������� ������������������ System
Routers
![Page 6: Reactive Programming With Akka - Lessons Learned](https://reader030.vdocuments.site/reader030/viewer/2022012910/53faacd28d7f72b82e8b50e1/html5/thumbnails/6.jpg)
Akka in a Nutshell
Actors
[1] By Dickelbers (Own work) [CC-BY-SA-3.0 (http://creativecommons.org/licenses/by-sa/3.0)], via Wikimedia Commons !http://upload.wikimedia.org/wikipedia/commons/0/09/Sweden_postbox.JPG
Messages
Mailbox[1]
Actor����������� ������������������ System
Routers
![Page 7: Reactive Programming With Akka - Lessons Learned](https://reader030.vdocuments.site/reader030/viewer/2022012910/53faacd28d7f72b82e8b50e1/html5/thumbnails/7.jpg)
Akka in a Nutshell
Actors
[1] By Dickelbers (Own work) [CC-BY-SA-3.0 (http://creativecommons.org/licenses/by-sa/3.0)], via Wikimedia Commons !http://upload.wikimedia.org/wikipedia/commons/0/09/Sweden_postbox.JPG
Messages
Mailbox[1]
Actor����������� ������������������ System
Routers
![Page 8: Reactive Programming With Akka - Lessons Learned](https://reader030.vdocuments.site/reader030/viewer/2022012910/53faacd28d7f72b82e8b50e1/html5/thumbnails/8.jpg)
Akka in a Nutshell
Actors
[1] By Dickelbers (Own work) [CC-BY-SA-3.0 (http://creativecommons.org/licenses/by-sa/3.0)], via Wikimedia Commons !http://upload.wikimedia.org/wikipedia/commons/0/09/Sweden_postbox.JPG
Messages
Mailbox[1]
Actor����������� ������������������ System
Routers
![Page 9: Reactive Programming With Akka - Lessons Learned](https://reader030.vdocuments.site/reader030/viewer/2022012910/53faacd28d7f72b82e8b50e1/html5/thumbnails/9.jpg)
Akka in a Nutshell
Actors
[1] By Dickelbers (Own work) [CC-BY-SA-3.0 (http://creativecommons.org/licenses/by-sa/3.0)], via Wikimedia Commons !http://upload.wikimedia.org/wikipedia/commons/0/09/Sweden_postbox.JPG
Messages
Mailbox[1]
Actor����������� ������������������ System
Routers
![Page 10: Reactive Programming With Akka - Lessons Learned](https://reader030.vdocuments.site/reader030/viewer/2022012910/53faacd28d7f72b82e8b50e1/html5/thumbnails/10.jpg)
Akka in a Nutshell
Actors
[1] By Dickelbers (Own work) [CC-BY-SA-3.0 (http://creativecommons.org/licenses/by-sa/3.0)], via Wikimedia Commons !http://upload.wikimedia.org/wikipedia/commons/0/09/Sweden_postbox.JPG
Messages
Mailbox[1]
Actor����������� ������������������ System
Routers
![Page 11: Reactive Programming With Akka - Lessons Learned](https://reader030.vdocuments.site/reader030/viewer/2022012910/53faacd28d7f72b82e8b50e1/html5/thumbnails/11.jpg)
Our definition of Legacy Code
Legacy \ˈle-gə-sē\ “: something that happened in the past or that comes from someone in the past”
! - Merriam-Webster
![Page 12: Reactive Programming With Akka - Lessons Learned](https://reader030.vdocuments.site/reader030/viewer/2022012910/53faacd28d7f72b82e8b50e1/html5/thumbnails/12.jpg)
Our definition of Legacy Code
Legacy \ˈle-gə-sē\ “: something that happened in the past or that comes from someone in the past”
! - Merriam-Webster
- Deogun-Sawano
Legacy Code \ˈle-gə-sē\ \ˈkōd\ “: code that does not satisfy the characteristics of a reactive system”
![Page 13: Reactive Programming With Akka - Lessons Learned](https://reader030.vdocuments.site/reader030/viewer/2022012910/53faacd28d7f72b82e8b50e1/html5/thumbnails/13.jpg)
What is Legacy Code?
Characteristics����������� ������������������ of����������� ������������������ a����������� ������������������ reactive����������� ������������������ system,����������� ������������������ as����������� ������������������ defined����������� ������������������ by����������� ������������������ the����������� ������������������ reactive����������� ������������������ manifesto:����������� ������������������
-����������� ������������������ responsive����������� ������������������
-����������� ������������������ scalable����������� ������������������
-����������� ������������������ resilient����������� ������������������
-����������� ������������������ event-driven
reactivemanifesto.org
![Page 14: Reactive Programming With Akka - Lessons Learned](https://reader030.vdocuments.site/reader030/viewer/2022012910/53faacd28d7f72b82e8b50e1/html5/thumbnails/14.jpg)
Java or Scala
I����������� ������������������ want����������� ������������������ to����������� ������������������ build����������� ������������������ an����������� ������������������ application����������� ������������������ with����������� ������������������ Akka,����������� ������������������ should����������� ������������������ I����������� ������������������ use����������� ������������������
Java����������� ������������������ or����������� ������������������ Scala?����������� ������������������
Well,����������� ������������������ it����������� ������������������ depends...
[1] https://duke.kenai.com/wave/.Midsize/Wave.png.png![2] http://www.scala-lang.org/
[1] [2]
![Page 15: Reactive Programming With Akka - Lessons Learned](https://reader030.vdocuments.site/reader030/viewer/2022012910/53faacd28d7f72b82e8b50e1/html5/thumbnails/15.jpg)
Java or Scala
Assume����������� ������������������ we����������� ������������������ want����������� ������������������ to����������� ������������������ build����������� ������������������ a����������� ������������������ machine����������� ������������������ M����������� ������������������ to����������� ������������������ solve����������� ������������������ a����������� ������������������ problem����������� ������������������ P����������� ������������������ where,����������� ������������������
•Efficiency����������� ������������������ is����������� ������������������ imperative����������� ������������������ ����������� ������������������
•Sequential����������� ������������������ computations����������� ������������������ shall����������� ������������������ be����������� ������������������ independent����������� ������������������
• Implementation����������� ������������������ of����������� ������������������ M����������� ������������������ shall����������� ������������������ be����������� ������������������ platform����������� ������������������ independent����������� ������������������
• Complexity����������� ������������������ and����������� ������������������ boilerplate����������� ������������������ code����������� ������������������ shall����������� ������������������ be����������� ������������������ reduced����������� ������������������
•M’s����������� ������������������ behavior����������� ������������������ shall����������� ������������������ be����������� ������������������ verifiable����������� ������������������
•Time����������� ������������������ to����������� ������������������ Market����������� ������������������ is����������� ������������������ essential����������� ������������������ and����������� ������������������ risks����������� ������������������ minimized
![Page 16: Reactive Programming With Akka - Lessons Learned](https://reader030.vdocuments.site/reader030/viewer/2022012910/53faacd28d7f72b82e8b50e1/html5/thumbnails/16.jpg)
Java or Scala Pros & Cons
Assume����������� ������������������ we����������� ������������������ want����������� ������������������ to����������� ������������������ build����������� ������������������ a����������� ������������������ machine����������� ������������������ M����������� ������������������ to����������� ������������������ solve����������� ������������������ a����������� ������������������ ����������� ������������������ ����������� ������������������ ����������� ������������������ ����������� ������������������ ����������� ������������������ ����������� ������������������ ����������� ������������������ problem����������� ������������������ P����������� ������������������ where,����������� ������������������
����������� ������������������ Efficiency����������� ������������������ is����������� ������������������ imperative����������� ������������������
����������� ������������������ Sequential����������� ������������������ computations����������� ������������������ shall����������� ������������������ be����������� ������������������ independent����������� ������������������
����������� ������������������ Implementation����������� ������������������ of����������� ������������������ M����������� ������������������ shall����������� ������������������ be����������� ������������������ platform����������� ������������������ independent����������� ������������������
����������� ������������������ Complexity����������� ������������������ and����������� ������������������ boilerplate����������� ������������������ code����������� ������������������ shall����������� ������������������ be����������� ������������������ reduced����������� ������������������
����������� ������������������ M’s����������� ������������������ behavior����������� ������������������ shall����������� ������������������ be����������� ������������������ verifiable����������� ������������������
����������� ������������������ Time����������� ������������������ to����������� ������������������ Market����������� ������������������ is����������� ������������������ essential����������� ������������������ and����������� ������������������ risks����������� ������������������ shall����������� ������������������ be����������� ������������������ minimized
Scoreboard����������� ������������������
Java:����������� ������������������
Scala:����������� ������������������
![Page 17: Reactive Programming With Akka - Lessons Learned](https://reader030.vdocuments.site/reader030/viewer/2022012910/53faacd28d7f72b82e8b50e1/html5/thumbnails/17.jpg)
Java or Scala Pros & Cons
Assume����������� ������������������ we����������� ������������������ want����������� ������������������ to����������� ������������������ build����������� ������������������ a����������� ������������������ machine����������� ������������������ M����������� ������������������ to����������� ������������������ solve����������� ������������������ a����������� ������������������ ����������� ������������������ ����������� ������������������ ����������� ������������������ ����������� ������������������ ����������� ������������������ ����������� ������������������ ����������� ������������������ problem����������� ������������������ P����������� ������������������ where,����������� ������������������
����������� ������������������ Efficiency����������� ������������������ is����������� ������������������ imperative����������� ������������������
����������� ������������������ Sequential����������� ������������������ computations����������� ������������������ shall����������� ������������������ be����������� ������������������ independent����������� ������������������
����������� ������������������ Implementation����������� ������������������ of����������� ������������������ M����������� ������������������ shall����������� ������������������ be����������� ������������������ platform����������� ������������������ independent����������� ������������������
����������� ������������������ Complexity����������� ������������������ and����������� ������������������ boilerplate����������� ������������������ code����������� ������������������ shall����������� ������������������ be����������� ������������������ reduced����������� ������������������
����������� ������������������ M’s����������� ������������������ behavior����������� ������������������ shall����������� ������������������ be����������� ������������������ verifiable����������� ������������������
����������� ������������������ Time����������� ������������������ to����������� ������������������ Market����������� ������������������ is����������� ������������������ essential����������� ������������������ and����������� ������������������ risks����������� ������������������ shall����������� ������������������ be����������� ������������������ minimized
Scoreboard����������� ������������������
Java:����������� ������������������
Scala:����������� ������������������ XI
I
![Page 18: Reactive Programming With Akka - Lessons Learned](https://reader030.vdocuments.site/reader030/viewer/2022012910/53faacd28d7f72b82e8b50e1/html5/thumbnails/18.jpg)
Java or Scala Pros & Cons
Assume����������� ������������������ we����������� ������������������ want����������� ������������������ to����������� ������������������ build����������� ������������������ a����������� ������������������ machine����������� ������������������ M����������� ������������������ to����������� ������������������ solve����������� ������������������ a����������� ������������������ ����������� ������������������ ����������� ������������������ ����������� ������������������ ����������� ������������������ ����������� ������������������ ����������� ������������������ ����������� ������������������ problem����������� ������������������ P����������� ������������������ where,����������� ������������������
����������� ������������������ Efficiency����������� ������������������ is����������� ������������������ imperative����������� ������������������
����������� ������������������ Sequential����������� ������������������ computations����������� ������������������ shall����������� ������������������ be����������� ������������������ independent����������� ������������������
����������� ������������������ Implementation����������� ������������������ of����������� ������������������ M����������� ������������������ shall����������� ������������������ be����������� ������������������ platform����������� ������������������ independent����������� ������������������
����������� ������������������ Complexity����������� ������������������ and����������� ������������������ boilerplate����������� ������������������ code����������� ������������������ shall����������� ������������������ be����������� ������������������ reduced����������� ������������������
����������� ������������������ M’s����������� ������������������ behavior����������� ������������������ shall����������� ������������������ be����������� ������������������ verifiable����������� ������������������
����������� ������������������ Time����������� ������������������ to����������� ������������������ Market����������� ������������������ is����������� ������������������ essential����������� ������������������ and����������� ������������������ risks����������� ������������������ shall����������� ������������������ be����������� ������������������ minimized
Scoreboard����������� ������������������
Java:����������� ������������������
Scala:����������� ������������������ XI
IX
I
I
![Page 19: Reactive Programming With Akka - Lessons Learned](https://reader030.vdocuments.site/reader030/viewer/2022012910/53faacd28d7f72b82e8b50e1/html5/thumbnails/19.jpg)
Java or Scala Pros & Cons
Assume����������� ������������������ we����������� ������������������ want����������� ������������������ to����������� ������������������ build����������� ������������������ a����������� ������������������ machine����������� ������������������ M����������� ������������������ to����������� ������������������ solve����������� ������������������ a����������� ������������������ ����������� ������������������ ����������� ������������������ ����������� ������������������ ����������� ������������������ ����������� ������������������ ����������� ������������������ ����������� ������������������ problem����������� ������������������ P����������� ������������������ where,����������� ������������������
����������� ������������������ Efficiency����������� ������������������ is����������� ������������������ imperative����������� ������������������
����������� ������������������ Sequential����������� ������������������ computations����������� ������������������ shall����������� ������������������ be����������� ������������������ independent����������� ������������������
����������� ������������������ Implementation����������� ������������������ of����������� ������������������ M����������� ������������������ shall����������� ������������������ be����������� ������������������ platform����������� ������������������ independent����������� ������������������
����������� ������������������ Complexity����������� ������������������ and����������� ������������������ boilerplate����������� ������������������ code����������� ������������������ shall����������� ������������������ be����������� ������������������ reduced����������� ������������������
����������� ������������������ M’s����������� ������������������ behavior����������� ������������������ shall����������� ������������������ be����������� ������������������ verifiable����������� ������������������
����������� ������������������ Time����������� ������������������ to����������� ������������������ Market����������� ������������������ is����������� ������������������ essential����������� ������������������ and����������� ������������������ risks����������� ������������������ shall����������� ������������������ be����������� ������������������ minimized
Scoreboard����������� ������������������
Java:����������� ������������������
Scala:����������� ������������������ XI
IX
I
I
X
I
I
![Page 20: Reactive Programming With Akka - Lessons Learned](https://reader030.vdocuments.site/reader030/viewer/2022012910/53faacd28d7f72b82e8b50e1/html5/thumbnails/20.jpg)
Java or Scala Pros & Cons
Assume����������� ������������������ we����������� ������������������ want����������� ������������������ to����������� ������������������ build����������� ������������������ a����������� ������������������ machine����������� ������������������ M����������� ������������������ to����������� ������������������ solve����������� ������������������ a����������� ������������������ ����������� ������������������ ����������� ������������������ ����������� ������������������ ����������� ������������������ ����������� ������������������ ����������� ������������������ ����������� ������������������ problem����������� ������������������ P����������� ������������������ where,����������� ������������������
����������� ������������������ Efficiency����������� ������������������ is����������� ������������������ imperative����������� ������������������
����������� ������������������ Sequential����������� ������������������ computations����������� ������������������ shall����������� ������������������ be����������� ������������������ independent����������� ������������������
����������� ������������������ Implementation����������� ������������������ of����������� ������������������ M����������� ������������������ shall����������� ������������������ be����������� ������������������ platform����������� ������������������ independent����������� ������������������
����������� ������������������ Complexity����������� ������������������ and����������� ������������������ boilerplate����������� ������������������ code����������� ������������������ shall����������� ������������������ be����������� ������������������ reduced����������� ������������������
����������� ������������������ M’s����������� ������������������ behavior����������� ������������������ shall����������� ������������������ be����������� ������������������ verifiable����������� ������������������
����������� ������������������ Time����������� ������������������ to����������� ������������������ Market����������� ������������������ is����������� ������������������ essential����������� ������������������ and����������� ������������������ risks����������� ������������������ shall����������� ������������������ be����������� ������������������ minimized
Scoreboard����������� ������������������
Java:����������� ������������������
Scala:����������� ������������������ XI
IX
I
I
X
I
I
X
I
![Page 21: Reactive Programming With Akka - Lessons Learned](https://reader030.vdocuments.site/reader030/viewer/2022012910/53faacd28d7f72b82e8b50e1/html5/thumbnails/21.jpg)
Java or Scala Pros & Cons
Assume����������� ������������������ we����������� ������������������ want����������� ������������������ to����������� ������������������ build����������� ������������������ a����������� ������������������ machine����������� ������������������ M����������� ������������������ to����������� ������������������ solve����������� ������������������ a����������� ������������������ ����������� ������������������ ����������� ������������������ ����������� ������������������ ����������� ������������������ ����������� ������������������ ����������� ������������������ ����������� ������������������ problem����������� ������������������ P����������� ������������������ where,����������� ������������������
����������� ������������������ Efficiency����������� ������������������ is����������� ������������������ imperative����������� ������������������
����������� ������������������ Sequential����������� ������������������ computations����������� ������������������ shall����������� ������������������ be����������� ������������������ independent����������� ������������������
����������� ������������������ Implementation����������� ������������������ of����������� ������������������ M����������� ������������������ shall����������� ������������������ be����������� ������������������ platform����������� ������������������ independent����������� ������������������
����������� ������������������ Complexity����������� ������������������ and����������� ������������������ boilerplate����������� ������������������ code����������� ������������������ shall����������� ������������������ be����������� ������������������ reduced����������� ������������������
����������� ������������������ M’s����������� ������������������ behavior����������� ������������������ shall����������� ������������������ be����������� ������������������ verifiable����������� ������������������
����������� ������������������ Time����������� ������������������ to����������� ������������������ Market����������� ������������������ is����������� ������������������ essential����������� ������������������ and����������� ������������������ risks����������� ������������������ shall����������� ������������������ be����������� ������������������ minimized
Scoreboard����������� ������������������
Java:����������� ������������������
Scala:����������� ������������������ XI
IX
I
I
X
I
I
X
I
I
X
![Page 22: Reactive Programming With Akka - Lessons Learned](https://reader030.vdocuments.site/reader030/viewer/2022012910/53faacd28d7f72b82e8b50e1/html5/thumbnails/22.jpg)
Java or Scala Pros & Cons
Assume����������� ������������������ we����������� ������������������ want����������� ������������������ to����������� ������������������ build����������� ������������������ a����������� ������������������ machine����������� ������������������ M����������� ������������������ to����������� ������������������ solve����������� ������������������ a����������� ������������������ ����������� ������������������ ����������� ������������������ ����������� ������������������ ����������� ������������������ ����������� ������������������ ����������� ������������������ ����������� ������������������ problem����������� ������������������ P����������� ������������������ where,����������� ������������������
����������� ������������������ Efficiency����������� ������������������ is����������� ������������������ imperative����������� ������������������
����������� ������������������ Sequential����������� ������������������ computations����������� ������������������ shall����������� ������������������ be����������� ������������������ independent����������� ������������������
����������� ������������������ Implementation����������� ������������������ of����������� ������������������ M����������� ������������������ shall����������� ������������������ be����������� ������������������ platform����������� ������������������ independent����������� ������������������
����������� ������������������ Complexity����������� ������������������ and����������� ������������������ boilerplate����������� ������������������ code����������� ������������������ shall����������� ������������������ be����������� ������������������ reduced����������� ������������������
����������� ������������������ M’s����������� ������������������ behavior����������� ������������������ shall����������� ������������������ be����������� ������������������ verifiable����������� ������������������
����������� ������������������ Time����������� ������������������ to����������� ������������������ Market����������� ������������������ is����������� ������������������ essential����������� ������������������ and����������� ������������������ risks����������� ������������������ shall����������� ������������������ be����������� ������������������ minimized
Scoreboard����������� ������������������
Java:����������� ������������������
Scala:����������� ������������������ XI
IX
I
I
X
I
I
X
I
I
XX
![Page 23: Reactive Programming With Akka - Lessons Learned](https://reader030.vdocuments.site/reader030/viewer/2022012910/53faacd28d7f72b82e8b50e1/html5/thumbnails/23.jpg)
Java or Scala Conclusion
•Both����������� ������������������ Java����������� ������������������ and����������� ������������������ Scala����������� ������������������ works����������� ������������������ well����������� ������������������ with����������� ������������������ Akka����������� ������������������
!
•Choose����������� ������������������ the����������� ������������������ language����������� ������������������ that����������� ������������������ makes����������� ������������������ most����������� ������������������ sense����������� ������������������
!
•Don’t����������� ������������������ add����������� ������������������ unnecessary����������� ������������������ risk����������� ������������������
Scoreboard����������� ������������������
Java:����������� ������������������
Scala:����������� ������������������
I
I
I
I
I
I
I
I
![Page 24: Reactive Programming With Akka - Lessons Learned](https://reader030.vdocuments.site/reader030/viewer/2022012910/53faacd28d7f72b82e8b50e1/html5/thumbnails/24.jpg)
Akka All or nothing?
Elastic
Distributed
Decentralized
ResilientAkka
Supervision
Modularization
IntegrationParallelism
Abstraction����������� ������������������ level
![Page 25: Reactive Programming With Akka - Lessons Learned](https://reader030.vdocuments.site/reader030/viewer/2022012910/53faacd28d7f72b82e8b50e1/html5/thumbnails/25.jpg)
Akka All or nothing?
Elastic
Distributed
Decentralized
ResilientAkka
Supervision
Modularization
Integration
Parallelism
Abstraction����������� ������������������ level
![Page 26: Reactive Programming With Akka - Lessons Learned](https://reader030.vdocuments.site/reader030/viewer/2022012910/53faacd28d7f72b82e8b50e1/html5/thumbnails/26.jpg)
Akka All or nothing?
Elastic
Distributed
Decentralized
ResilientAkka
Supervision
Modularization
Integration
Parallelism
Abstraction����������� ������������������ level
![Page 27: Reactive Programming With Akka - Lessons Learned](https://reader030.vdocuments.site/reader030/viewer/2022012910/53faacd28d7f72b82e8b50e1/html5/thumbnails/27.jpg)
Akka All or nothing?
Elastic
Distributed
Decentralized
ResilientAkka
Supervision
Modularization Integration
Parallelism
Abstraction����������� ������������������ level
![Page 28: Reactive Programming With Akka - Lessons Learned](https://reader030.vdocuments.site/reader030/viewer/2022012910/53faacd28d7f72b82e8b50e1/html5/thumbnails/28.jpg)
Akka All or nothing?
Elastic
Distributed
Decentralized
ResilientAkka
Supervision
Modularization
Integration
Parallelism
Abstraction����������� ������������������ level
![Page 29: Reactive Programming With Akka - Lessons Learned](https://reader030.vdocuments.site/reader030/viewer/2022012910/53faacd28d7f72b82e8b50e1/html5/thumbnails/29.jpg)
Akka All or nothing?
Elastic
Distributed
Decentralized
ResilientAkka
Supervision
Modularization
Integration
Parallelism
Abstraction����������� ������������������ level
![Page 30: Reactive Programming With Akka - Lessons Learned](https://reader030.vdocuments.site/reader030/viewer/2022012910/53faacd28d7f72b82e8b50e1/html5/thumbnails/30.jpg)
Domain Specific Requirements
•Akka����������� ������������������ is����������� ������������������ more����������� ������������������ or����������� ������������������ less����������� ������������������ a����������� ������������������ perfect����������� ������������������ match����������� ������������������ for����������� ������������������ all����������� ������������������ parallelizable����������� ������������������ domains����������� ������������������ ����������� ������������������
!
•But����������� ������������������ what����������� ������������������ about����������� ������������������
• reactive����������� ������������������ domains����������� ������������������ with����������� ������������������ blocking����������� ������������������ parts?����������� ������������������ ����������� ������������������
• legacy����������� ������������������ domains����������� ������������������ with����������� ������������������ reactive����������� ������������������ parts?
[1] By Renesis (Own work) [CC-BY-SA-2.5 (http://creativecommons.org/licenses/by-sa/2.5)], via Wikimedia Commons. http://upload.wikimedia.org/wikipedia/commons/1/19/Stop2.png
[1]
![Page 31: Reactive Programming With Akka - Lessons Learned](https://reader030.vdocuments.site/reader030/viewer/2022012910/53faacd28d7f72b82e8b50e1/html5/thumbnails/31.jpg)
Blocking in a Reactive Environment
•If����������� ������������������ we����������� ������������������ choose����������� ������������������ Akka,����������� ������������������ we����������� ������������������ need����������� ������������������ to����������� ������������������ block����������� ������������������ actors,����������� ������������������ yuck!����������� ������������������ ����������� ������������������
!
!
!
•The����������� ������������������ main����������� ������������������ advantage����������� ������������������ is����������� ������������������ that����������� ������������������ we����������� ������������������ can����������� ������������������ reuse����������� ������������������ actors����������� ������������������ from����������� ������������������ parallelizable����������� ������������������ parts����������� ������������������ but����������� ������������������ are����������� ������������������ there����������� ������������������ any����������� ������������������ downsides?����������� ������������������
!
•The����������� ������������������ other����������� ������������������ option����������� ������������������ is����������� ������������������ to����������� ������������������ use����������� ������������������ legacy����������� ������������������ design
oh����������� ������������������ no����������� ������������������ ����������� ������������������
I’m����������� ������������������ blocked
but����������� ������������������ I’m����������� ������������������ still����������� ������������������ ����������� ������������������ pretty
![Page 32: Reactive Programming With Akka - Lessons Learned](https://reader030.vdocuments.site/reader030/viewer/2022012910/53faacd28d7f72b82e8b50e1/html5/thumbnails/32.jpg)
Reactive in a Legacy Environment?
Blocking
Blocking
Blocking
Blocking
AB
C
D
Is����������� ������������������ it����������� ������������������ possible����������� ������������������ to����������� ������������������ replace����������� ������������������ D����������� ������������������ by����������� ������������������ a����������� ������������������ component����������� ������������������ implemented����������� ������������������ with����������� ������������������ Akka?
![Page 33: Reactive Programming With Akka - Lessons Learned](https://reader030.vdocuments.site/reader030/viewer/2022012910/53faacd28d7f72b82e8b50e1/html5/thumbnails/33.jpg)
Debugger Friend or Foe?
We����������� ������������������ often����������� ������������������ get����������� ������������������ the����������� ������������������ question:����������� ������������������
!
����������� ������������������ “The����������� ������������������ asynchrony����������� ������������������ in����������� ������������������ Akka����������� ������������������ makes����������� ������������������ it����������� ������������������ very����������� ������������������ hard����������� ������������������ to����������� ������������������ ����������� ������������������ ����������� ������������������ ����������� ������������������ ����������� ������������������
����������� ������������������ ����������� ������������������ ����������� ������������������ ����������� ������������������ ����������� ������������������ use����������� ������������������ the����������� ������������������ debugger,����������� ������������������ Am����������� ������������������ I����������� ������������������ doing����������� ������������������ it����������� ������������������ wrong?”
![Page 34: Reactive Programming With Akka - Lessons Learned](https://reader030.vdocuments.site/reader030/viewer/2022012910/53faacd28d7f72b82e8b50e1/html5/thumbnails/34.jpg)
debugger in Legacy Design
Legacy����������� ������������������ Design
Object����������� ������������������ A Object����������� ������������������ B Object����������� ������������������ N
![Page 35: Reactive Programming With Akka - Lessons Learned](https://reader030.vdocuments.site/reader030/viewer/2022012910/53faacd28d7f72b82e8b50e1/html5/thumbnails/35.jpg)
debugger in Legacy Design
Legacy����������� ������������������ Design
Object����������� ������������������ A Object����������� ������������������ B
![Page 36: Reactive Programming With Akka - Lessons Learned](https://reader030.vdocuments.site/reader030/viewer/2022012910/53faacd28d7f72b82e8b50e1/html5/thumbnails/36.jpg)
debugger in Legacy Design
Legacy����������� ������������������ Design
Object����������� ������������������ A Object����������� ������������������ B Object����������� ������������������ N
Place����������� ������������������ a����������� ������������������ break����������� ������������������ point����������� ������������������ in����������� ������������������ N����������� ������������������ to����������� ������������������ find����������� ������������������ out����������� ������������������
����������� ������������������ -����������� ������������������ Which����������� ������������������ value����������� ������������������ caused����������� ������������������ the����������� ������������������ crash?����������� ������������������
����������� ������������������ -����������� ������������������ Who����������� ������������������ created����������� ������������������ it?
![Page 37: Reactive Programming With Akka - Lessons Learned](https://reader030.vdocuments.site/reader030/viewer/2022012910/53faacd28d7f72b82e8b50e1/html5/thumbnails/37.jpg)
debugger in Reactive Design
Reactive����������� ������������������ design
Break����������� ������������������ point����������� ������������������ in����������� ������������������ actor����������� ������������������ N
![Page 38: Reactive Programming With Akka - Lessons Learned](https://reader030.vdocuments.site/reader030/viewer/2022012910/53faacd28d7f72b82e8b50e1/html5/thumbnails/38.jpg)
Supervision
We’re����������� ������������������ reactive
![Page 39: Reactive Programming With Akka - Lessons Learned](https://reader030.vdocuments.site/reader030/viewer/2022012910/53faacd28d7f72b82e8b50e1/html5/thumbnails/39.jpg)
Supervision
oh����������� ������������������ no����������� ������������������ ����������� ������������������
I’m����������� ������������������ blocked
but����������� ������������������ I’m����������� ������������������ still����������� ������������������ ����������� ������������������ pretty
We’re����������� ������������������ reactive
![Page 40: Reactive Programming With Akka - Lessons Learned](https://reader030.vdocuments.site/reader030/viewer/2022012910/53faacd28d7f72b82e8b50e1/html5/thumbnails/40.jpg)
Supervision
Asynchronous
![Page 41: Reactive Programming With Akka - Lessons Learned](https://reader030.vdocuments.site/reader030/viewer/2022012910/53faacd28d7f72b82e8b50e1/html5/thumbnails/41.jpg)
Supervision
Asynchronous
Failure
![Page 42: Reactive Programming With Akka - Lessons Learned](https://reader030.vdocuments.site/reader030/viewer/2022012910/53faacd28d7f72b82e8b50e1/html5/thumbnails/42.jpg)
Supervision
SynchronousBlocking����������� ������������������ call
![Page 43: Reactive Programming With Akka - Lessons Learned](https://reader030.vdocuments.site/reader030/viewer/2022012910/53faacd28d7f72b82e8b50e1/html5/thumbnails/43.jpg)
Supervision
SynchronousBlocking����������� ������������������ call
![Page 44: Reactive Programming With Akka - Lessons Learned](https://reader030.vdocuments.site/reader030/viewer/2022012910/53faacd28d7f72b82e8b50e1/html5/thumbnails/44.jpg)
Supervision
SynchronousBlocking����������� ������������������ call
Send����������� ������������������ failure
![Page 45: Reactive Programming With Akka - Lessons Learned](https://reader030.vdocuments.site/reader030/viewer/2022012910/53faacd28d7f72b82e8b50e1/html5/thumbnails/45.jpg)
Supervision
SynchronousBlocking����������� ������������������ call
Send����������� ������������������ failure
![Page 46: Reactive Programming With Akka - Lessons Learned](https://reader030.vdocuments.site/reader030/viewer/2022012910/53faacd28d7f72b82e8b50e1/html5/thumbnails/46.jpg)
Supervision! private ActorRef targetActor;! private ActorRef caller;! private Timeout timeout;! private Cancellable timeoutMessage;!! @Override! public SupervisorStrategy supervisorStrategy() {! return new OneForOneStrategy(0, Duration.Zero(), new Function<Throwable, SupervisorStrategy.Directive>() {! public SupervisorStrategy.Directive apply(Throwable cause) {! caller.tell(new Failure(cause), self());! return SupervisorStrategy.stop();! }! });! }!! @Override! public void onReceive(final Object message) throws Exception {! if (message instanceof AskParam) {! AskParam askParam = (AskParam) message;! timeout = askParam.timeout;! caller = sender();! targetActor = context().actorOf(askParam.props);! context().watch(targetActor);! targetActor.forward(askParam.message, context());! final Scheduler scheduler = context().system().scheduler();! timeoutMessage = scheduler.scheduleOnce(askParam.timeout.duration(), self(), new AskTimeout(), context().dispatcher(), null);! }! else if (message instanceof Terminated) {! sendFailureToCaller(new ActorKilledException("Target actor terminated."));! timeoutMessage.cancel();! context().stop(self());! }! else if (message instanceof AskTimeout) {! sendFailureToCaller(new TimeoutException("Target actor timed out after " + timeout.toString()));! context().stop(self());! }! else {! unhandled(message);! }! }!! private void sendFailureToCaller(final Throwable t) {! caller.tell(new Failure(t), self());! }
![Page 47: Reactive Programming With Akka - Lessons Learned](https://reader030.vdocuments.site/reader030/viewer/2022012910/53faacd28d7f72b82e8b50e1/html5/thumbnails/47.jpg)
Supervision
-Write����������� ������������������ legacy����������� ������������������ code����������� ������������������ -Use����������� ������������������ Akka����������� ������������������ actors
![Page 48: Reactive Programming With Akka - Lessons Learned](https://reader030.vdocuments.site/reader030/viewer/2022012910/53faacd28d7f72b82e8b50e1/html5/thumbnails/48.jpg)
Supervision
-Write����������� ������������������ legacy����������� ������������������ code����������� ������������������ -Use����������� ������������������ Akka����������� ������������������ actors
Sequential����������� ������������������ does����������� ������������������ not����������� ������������������ imply����������� ������������������ synchronicity
![Page 49: Reactive Programming With Akka - Lessons Learned](https://reader030.vdocuments.site/reader030/viewer/2022012910/53faacd28d7f72b82e8b50e1/html5/thumbnails/49.jpg)
Changing mindset
Sequential����������� ������������������ does����������� ������������������ not����������� ������������������ imply����������� ������������������ ����������� ������������������
synchronicity
![Page 50: Reactive Programming With Akka - Lessons Learned](https://reader030.vdocuments.site/reader030/viewer/2022012910/53faacd28d7f72b82e8b50e1/html5/thumbnails/50.jpg)
Changing mindset
Sequential����������� ������������������ does����������� ������������������ not����������� ������������������ imply����������� ������������������ ����������� ������������������
synchronicity
![Page 51: Reactive Programming With Akka - Lessons Learned](https://reader030.vdocuments.site/reader030/viewer/2022012910/53faacd28d7f72b82e8b50e1/html5/thumbnails/51.jpg)
Changing mindset
Sequential����������� ������������������ does����������� ������������������ not����������� ������������������ imply����������� ������������������ ����������� ������������������
synchronicity
![Page 52: Reactive Programming With Akka - Lessons Learned](https://reader030.vdocuments.site/reader030/viewer/2022012910/53faacd28d7f72b82e8b50e1/html5/thumbnails/52.jpg)
Changing mindset
Sequential����������� ������������������ does����������� ������������������ not����������� ������������������ imply����������� ������������������ ����������� ������������������
synchronicity
![Page 53: Reactive Programming With Akka - Lessons Learned](https://reader030.vdocuments.site/reader030/viewer/2022012910/53faacd28d7f72b82e8b50e1/html5/thumbnails/53.jpg)
Loose Coupling?
Messages
![Page 54: Reactive Programming With Akka - Lessons Learned](https://reader030.vdocuments.site/reader030/viewer/2022012910/53faacd28d7f72b82e8b50e1/html5/thumbnails/54.jpg)
The Shared Mutable State Trap
Keeping����������� ������������������ state����������� ������������������ between����������� ������������������ messages����������� ������������������ in����������� ������������������ an����������� ������������������ actor����������� ������������������ is����������� ������������������ extremely����������� ������������������ dangerous����������� ������������������ because����������� ������������������ it����������� ������������������ may����������� ������������������ cause����������� ������������������ a����������� ������������������ shared����������� ������������������ mutable����������� ������������������ state����������� ������������������
!
!
!
![Page 55: Reactive Programming With Akka - Lessons Learned](https://reader030.vdocuments.site/reader030/viewer/2022012910/53faacd28d7f72b82e8b50e1/html5/thumbnails/55.jpg)
The Shared Mutable State Trap
Actor
Properties����������� ������������������
-����������� ������������������ One����������� ������������������ message����������� ������������������ queue����������� ������������������
-����������� ������������������ Only����������� ������������������ one����������� ������������������ message����������� ������������������ is����������� ������������������ processed����������� ������������������ at����������� ������������������ a����������� ������������������ time����������� ������������������
-����������� ������������������ Messages����������� ������������������ are����������� ������������������ processed����������� ������������������ in����������� ������������������ order����������� ������������������ received����������� ������������������
-����������� ������������������ An����������� ������������������ actor����������� ������������������ may����������� ������������������ choose����������� ������������������ to����������� ������������������ divide����������� ������������������ and����������� ������������������ conquer����������� ������������������ a����������� ������������������ task����������� ������������������ by����������� ������������������ calling����������� ������������������ other����������� ������������������ actors
![Page 56: Reactive Programming With Akka - Lessons Learned](https://reader030.vdocuments.site/reader030/viewer/2022012910/53faacd28d7f72b82e8b50e1/html5/thumbnails/56.jpg)
The Shared Mutable State Trap
Router
Actors����������� ������������������ may����������� ������������������ be����������� ������������������ grouped����������� ������������������ in����������� ������������������ a����������� ������������������ router
Properties����������� ������������������ -����������� ������������������ Messages����������� ������������������ are����������� ������������������ distributed����������� ������������������ among����������� ������������������ actors����������� ������������������ according����������� ������������������ to����������� ������������������ some����������� ������������������ message����������� ������������������ delivery����������� ������������������ scheme,����������� ������������������ eg����������� ������������������ Round����������� ������������������ Robin����������� ������������������ or����������� ������������������ Smallest����������� ������������������ Mailbox����������� ������������������ !-����������� ������������������ A����������� ������������������ router����������� ������������������ may����������� ������������������ receive����������� ������������������ a����������� ������������������ lot����������� ������������������ of����������� ������������������ messages
![Page 57: Reactive Programming With Akka - Lessons Learned](https://reader030.vdocuments.site/reader030/viewer/2022012910/53faacd28d7f72b82e8b50e1/html5/thumbnails/57.jpg)
The Shared Mutable State Trap
-����������� ������������������ A����������� ������������������ stateful����������� ������������������ actor����������� ������������������ is����������� ������������������ part����������� ������������������ of����������� ������������������ a����������� ������������������ router����������� ������������������ !-����������� ������������������ It����������� ������������������ uses����������� ������������������ divide����������� ������������������ and����������� ������������������ conquer����������� ������������������ to����������� ������������������ solve����������� ������������������ its����������� ������������������ task
Actor����������� ������������������ with����������� ������������������ state
Scenario
![Page 58: Reactive Programming With Akka - Lessons Learned](https://reader030.vdocuments.site/reader030/viewer/2022012910/53faacd28d7f72b82e8b50e1/html5/thumbnails/58.jpg)
The Shared Mutable State Trap
Actor����������� ������������������ with����������� ������������������ state -����������� ������������������ Each����������� ������������������ message����������� ������������������ recived����������� ������������������ from����������� ������������������ the����������� ������������������ ����������� ������������������
router����������� ������������������ resets����������� ������������������ the����������� ������������������ state����������� ������������������ !-����������� ������������������ New����������� ������������������ messages����������� ������������������ are����������� ������������������ inter����������� ������������������ mixed����������� ������������������ with����������� ������������������ child����������� ������������������ responses����������� ������������������ ����������� ������������������ !-����������� ������������������ Hence,����������� ������������������ we����������� ������������������ have����������� ������������������ a����������� ������������������ shared����������� ������������������ mutable����������� ������������������ state����������� ������������������
Scenario
![Page 59: Reactive Programming With Akka - Lessons Learned](https://reader030.vdocuments.site/reader030/viewer/2022012910/53faacd28d7f72b82e8b50e1/html5/thumbnails/59.jpg)
The Shared Mutable State Trap
Solution����������� ������������������ -����������� ������������������ Each����������� ������������������ request����������� ������������������ sent����������� ������������������ by����������� ������������������ the����������� ������������������ router����������� ������������������ results����������� ������������������ in����������� ������������������ a����������� ������������������ “Gather”����������� ������������������ actor����������� ������������������ !-����������� ������������������ The����������� ������������������ Gather����������� ������������������ actor����������� ������������������ is����������� ������������������ responsible����������� ������������������ for����������� ������������������ collecting����������� ������������������ the����������� ������������������ result����������� ������������������ ����������� ������������������ !-����������� ������������������ A����������� ������������������ Gather����������� ������������������ actor����������� ������������������ is����������� ������������������ NEVER����������� ������������������ reused
Gather����������� ������������������ actor
result
![Page 60: Reactive Programming With Akka - Lessons Learned](https://reader030.vdocuments.site/reader030/viewer/2022012910/53faacd28d7f72b82e8b50e1/html5/thumbnails/60.jpg)
Key Take-Aways
-����������� ������������������ It’s����������� ������������������ very����������� ������������������ easy����������� ������������������ to����������� ������������������ accidentally����������� ������������������ fall����������� ������������������ back����������� ������������������ to����������� ������������������ sequential����������� ������������������ “thinking”����������� ������������������ with����������� ������������������ state����������� ������������������ !-����������� ������������������ Often����������� ������������������ one����������� ������������������ does����������� ������������������ not����������� ������������������ realize����������� ������������������ this����������� ������������������ until����������� ������������������ the����������� ������������������ system����������� ������������������ is����������� ������������������ placed����������� ������������������ under����������� ������������������ heavy����������� ������������������ load����������� ������������������ !-����������� ������������������ Try����������� ������������������ to����������� ������������������ avoid����������� ������������������ state!
![Page 61: Reactive Programming With Akka - Lessons Learned](https://reader030.vdocuments.site/reader030/viewer/2022012910/53faacd28d7f72b82e8b50e1/html5/thumbnails/61.jpg)
Readability
By����������� ������������������ Various.����������� ������������������ Edited����������� ������������������ by:����������� ������������������ W����������� ������������������ H����������� ������������������ Maw����������� ������������������ and����������� ������������������ J����������� ������������������ Dredge����������� ������������������ (Abandoned����������� ������������������ library����������� ������������������ clearance.����������� ������������������ Self����������� ������������������ photographed.)����������� ������������������ [Public����������� ������������������ domain],����������� ������������������ via����������� ������������������ Wikimedia����������� ������������������ Commons
![Page 62: Reactive Programming With Akka - Lessons Learned](https://reader030.vdocuments.site/reader030/viewer/2022012910/53faacd28d7f72b82e8b50e1/html5/thumbnails/62.jpg)
Implications of untyped actors
!
Akka����������� ������������������ actors����������� ������������������ are����������� ������������������ untyped����������� ������������������ -����������� ������������������ by����������� ������������������ design
!! public void onReceive(Object message)
![Page 63: Reactive Programming With Akka - Lessons Learned](https://reader030.vdocuments.site/reader030/viewer/2022012910/53faacd28d7f72b82e8b50e1/html5/thumbnails/63.jpg)
Implications of untyped actors
!
Akka����������� ������������������ actors����������� ������������������ are����������� ������������������ untyped����������� ������������������ -����������� ������������������ by����������� ������������������ design
- Readability����������� ������������������
- No����������� ������������������ support����������� ������������������ from����������� ������������������ compiler/IDE
!! public void onReceive(Object message)
![Page 64: Reactive Programming With Akka - Lessons Learned](https://reader030.vdocuments.site/reader030/viewer/2022012910/53faacd28d7f72b82e8b50e1/html5/thumbnails/64.jpg)
Message Handling V 1.0
!!@Override!public void onReceive(Object message) {! if (message instanceof SomeMessage) {! doStuff();! }! else {! unhandled(message);! }!}!
![Page 65: Reactive Programming With Akka - Lessons Learned](https://reader030.vdocuments.site/reader030/viewer/2022012910/53faacd28d7f72b82e8b50e1/html5/thumbnails/65.jpg)
V 1.0 - If-else contd.
!!@Override!public void onReceive(Object message) {! if (message instanceof SomeMessage) {! doStuff();! }! else if (message instanceof SomeOtherMessage) {! doSomeOtherStuff();! }! else {! unhandled(message);! }!}!
![Page 66: Reactive Programming With Akka - Lessons Learned](https://reader030.vdocuments.site/reader030/viewer/2022012910/53faacd28d7f72b82e8b50e1/html5/thumbnails/66.jpg)
V 1.0 - If-else mess
!!@Override!public void onReceive(Object message) {! if (message instanceof SomeMessage) {! doStuff();! }! else if (message instanceof SomeOtherMessage) {! doSomeOtherStuff();! }! else if (message instanceof YetAnotherMessage) {! doEvenMoreStuff();! }! else {! unhandled(message);! }!}!
![Page 67: Reactive Programming With Akka - Lessons Learned](https://reader030.vdocuments.site/reader030/viewer/2022012910/53faacd28d7f72b82e8b50e1/html5/thumbnails/67.jpg)
Evolution of message handling
!!!!@Override!public void onReceive(Object message) {! ...!}!!
http://upload.wikimedia.org/wikipedia/commons/thumb/6/69/Human_evolution.svg/2000px-Human_evolution.svg.png
![Page 68: Reactive Programming With Akka - Lessons Learned](https://reader030.vdocuments.site/reader030/viewer/2022012910/53faacd28d7f72b82e8b50e1/html5/thumbnails/68.jpg)
V 2.0 - Overloading
!public void onMessage(SomeMessage message) {...}!!public void onMessage(SomeOtherMessage message) {...}
![Page 69: Reactive Programming With Akka - Lessons Learned](https://reader030.vdocuments.site/reader030/viewer/2022012910/53faacd28d7f72b82e8b50e1/html5/thumbnails/69.jpg)
V 2.0 - Overloading
!public void onMessage(SomeMessage message) {...}!!public void onMessage(SomeOtherMessage message) {...}
!@Override!public void onReceive(Object message) {! ...!! methods.get(message.getClass()).invoke(this, message);!! ...!}!
![Page 70: Reactive Programming With Akka - Lessons Learned](https://reader030.vdocuments.site/reader030/viewer/2022012910/53faacd28d7f72b82e8b50e1/html5/thumbnails/70.jpg)
V 2.1 - Annotations
!!class Worker extends BaseActor {! ! @Response! public void onMessage(SomeMessage message) {...}!!! public void onMessage(SomeOtherMessage message) {...}!}
![Page 71: Reactive Programming With Akka - Lessons Learned](https://reader030.vdocuments.site/reader030/viewer/2022012910/53faacd28d7f72b82e8b50e1/html5/thumbnails/71.jpg)
V 3.0 - Contract!!!interface Messages { !! void doSomething(SomeMessage message);!! void doSomethingElse(SomeOtherMessage message);!!}!!!!
![Page 72: Reactive Programming With Akka - Lessons Learned](https://reader030.vdocuments.site/reader030/viewer/2022012910/53faacd28d7f72b82e8b50e1/html5/thumbnails/72.jpg)
V 3.0 - Contract!!!interface Messages { !! void doSomething(SomeMessage message);!! void doSomethingElse(SomeOtherMessage message);!!}!!!!!SomeActor extends BaseActor implements Messages {...}!
![Page 73: Reactive Programming With Akka - Lessons Learned](https://reader030.vdocuments.site/reader030/viewer/2022012910/53faacd28d7f72b82e8b50e1/html5/thumbnails/73.jpg)
V 3.0 - Contract
!!class Worker extends BaseActor implements Messages {! !! public void handleResponse(SomeMessage message) {...}!!! public void handleRequest(SomeOtherMessage message) {...}!!}
![Page 74: Reactive Programming With Akka - Lessons Learned](https://reader030.vdocuments.site/reader030/viewer/2022012910/53faacd28d7f72b82e8b50e1/html5/thumbnails/74.jpg)
V 3.0 - Contract!!BaseActor extends UntypedActor {!! @Override!! public void preStart() {! methodDelegate = new MethodDelegate(this);! }!! @Override ! public void onReceive(Object message) {! if (methodDelegate.onReceive(message)) {! return;! }! unhandled(message);! }!}!
![Page 75: Reactive Programming With Akka - Lessons Learned](https://reader030.vdocuments.site/reader030/viewer/2022012910/53faacd28d7f72b82e8b50e1/html5/thumbnails/75.jpg)
Before
!class Worker extends BaseActor implements Messages {! @Override! public void onReceive(Object message) {! if (message instanceof SomeMessage) {! doStuff();! }! else if (message instanceof SomeOtherMessage) {! doSomeOtherStuff();! }! else if (message instanceof YetAnotherMessage) {! doEvenMoreStuff();! }! else {! unhandled(message);! }!
![Page 76: Reactive Programming With Akka - Lessons Learned](https://reader030.vdocuments.site/reader030/viewer/2022012910/53faacd28d7f72b82e8b50e1/html5/thumbnails/76.jpg)
After
!!class Worker extends BaseActor implements Messages { !! public void handleResponse(SomeMessage message) {! doStuff();! }!! public void handleRequest(SomeOtherMessage message) {! doSomeOtherStuff();! }!! public void process(YetAnotherMessage message) {! doEvenMoreStuff();! }!}!
![Page 77: Reactive Programming With Akka - Lessons Learned](https://reader030.vdocuments.site/reader030/viewer/2022012910/53faacd28d7f72b82e8b50e1/html5/thumbnails/77.jpg)
Akka & Java 8 Lambdas
- Duke: https://duke.kenai.com/thumbsup/DukeWithHelmet.png!- Lambda: http://en.wikipedia.org/wiki/Lambda_calculus
![Page 78: Reactive Programming With Akka - Lessons Learned](https://reader030.vdocuments.site/reader030/viewer/2022012910/53faacd28d7f72b82e8b50e1/html5/thumbnails/78.jpg)
Warning
http://doc.akka.io/docs/akka/2.3.3/java/lambda-actors.html
![Page 79: Reactive Programming With Akka - Lessons Learned](https://reader030.vdocuments.site/reader030/viewer/2022012910/53faacd28d7f72b82e8b50e1/html5/thumbnails/79.jpg)
The Receive Builder !!public class MyActor extends AbstractActor {! public MyActor() {! receive(ReceiveBuilder.! match(SomeMessage.class, m -> {! doStuff();! }).! match(SomeOtherMessage.class, m -> {! doSomeOtherStuff();! }).! match(YetAnotherMessage.class, m -> {! yetSomeMoreStuff();! }).! matchAny(this::unhandled).! build());! }!}!
![Page 80: Reactive Programming With Akka - Lessons Learned](https://reader030.vdocuments.site/reader030/viewer/2022012910/53faacd28d7f72b82e8b50e1/html5/thumbnails/80.jpg)
A Quick Note On Performance
- The����������� ������������������ partial����������� ������������������ functions����������� ������������������ created����������� ������������������ by����������� ������������������ the����������� ������������������ ReceiveBuilder����������� ������������������ consist����������� ������������������ of����������� ������������������ multiple����������� ������������������ lambda����������� ������������������ expressions����������� ������������������ for����������� ������������������ every����������� ������������������ match����������� ������������������ statement����������� ������������������ which����������� ������������������ may����������� ������������������ be����������� ������������������ hard����������� ������������������ for����������� ������������������ the����������� ������������������ JVM����������� ������������������ to����������� ������������������ optimize����������� ������������������
!
- The����������� ������������������ resulting����������� ������������������ code����������� ������������������ may����������� ������������������ not����������� ������������������ be����������� ������������������ as����������� ������������������ performant����������� ������������������ as����������� ������������������ the����������� ������������������ corresponding����������� ������������������ untyped����������� ������������������ actor����������� ������������������ version����������� ������������������
![Page 81: Reactive Programming With Akka - Lessons Learned](https://reader030.vdocuments.site/reader030/viewer/2022012910/53faacd28d7f72b82e8b50e1/html5/thumbnails/81.jpg)
Conclusions
- Contracts����������� ������������������ to����������� ������������������ explicitly����������� ������������������ define����������� ������������������ behavior����������� ������������������ works����������� ������������������ pretty����������� ������������������ well����������� ������������������
- Can����������� ������������������ be����������� ������������������ a����������� ������������������ useful����������� ������������������ tool����������� ������������������ in����������� ������������������ your����������� ������������������ toolbox����������� ������������������
- Java����������� ������������������ 8����������� ������������������ and����������� ������������������ lambdas����������� ������������������ provides����������� ������������������ additional����������� ������������������ approaches����������� ������������������
- Scala����������� ������������������ can����������� ������������������ give����������� ������������������ similar����������� ������������������ support����������� ������������������ via����������� ������������������ traits����������� ������������������ &����������� ������������������ match
![Page 82: Reactive Programming With Akka - Lessons Learned](https://reader030.vdocuments.site/reader030/viewer/2022012910/53faacd28d7f72b82e8b50e1/html5/thumbnails/82.jpg)
Testing Akka Code
Interact����������� ������������������ by����������� ������������������ sending����������� ������������������ messages BDD����������� ������������������
Scenarios
Integration����������� ������������������ tests
![Page 83: Reactive Programming With Akka - Lessons Learned](https://reader030.vdocuments.site/reader030/viewer/2022012910/53faacd28d7f72b82e8b50e1/html5/thumbnails/83.jpg)
Scenarios
Case����������� ������������������ A1:����������� ������������������ Sort����������� ������������������ any����������� ������������������ sequence����������� ������������������ of����������� ������������������ numbers����������� ������������������ in����������� ������������������ increasing����������� ������������������ order����������� ������������������ ����������� ������������������ !!Given����������� ������������������ a����������� ������������������ sequence����������� ������������������ of����������� ������������������ numbers����������� ������������������ in����������� ������������������ decreasing����������� ������������������ order����������� ������������������ !When����������� ������������������ applying����������� ������������������ the����������� ������������������ sort����������� ������������������ algorithm����������� ������������������ !Then����������� ������������������ the����������� ������������������ resulting����������� ������������������ sequence����������� ������������������ of����������� ������������������ numbers����������� ������������������ is����������� ������������������ in����������� ������������������ increasing����������� ������������������ order
![Page 84: Reactive Programming With Akka - Lessons Learned](https://reader030.vdocuments.site/reader030/viewer/2022012910/53faacd28d7f72b82e8b50e1/html5/thumbnails/84.jpg)
The Test
!!!!@Test!public void caseA1() {!! given(sequence(9,8,7,6,5,4,3,2,1,0));! ! whenSorting();! ! thenResultIs(sequence(0,1,2,3,4,5,6,7,8,9));!!}!
![Page 85: Reactive Programming With Akka - Lessons Learned](https://reader030.vdocuments.site/reader030/viewer/2022012910/53faacd28d7f72b82e8b50e1/html5/thumbnails/85.jpg)
Key Take-Aways
- It’s����������� ������������������ possible����������� ������������������ to����������� ������������������ use����������� ������������������ Akka����������� ������������������ in����������� ������������������ legacy����������� ������������������ code����������� ������������������
- Akka����������� ������������������ is����������� ������������������ Scala����������� ������������������ &����������� ������������������ Java����������� ������������������ compliant����������� ������������������
- Akka����������� ������������������ is����������� ������������������ a����������� ������������������ toolkit����������� ������������������ with����������� ������������������ a����������� ������������������ lot����������� ������������������ of����������� ������������������ goodies����������� ������������������
- Stop����������� ������������������ writing����������� ������������������ legacy����������� ������������������ code
![Page 86: Reactive Programming With Akka - Lessons Learned](https://reader030.vdocuments.site/reader030/viewer/2022012910/53faacd28d7f72b82e8b50e1/html5/thumbnails/86.jpg)
Thank����������� ������������������ you@DanielDeogun����������� ������������������ ����������� ������������������ @DanielSawano