Actors in Java

308 views
242 views

Published on

My slides from JavaDay 2011 Kyiv

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

  • Be the first to like this

No Downloads
Views
Total views
308
On SlideShare
0
From Embeds
0
Number of Embeds
1
Actions
Shares
0
Downloads
0
Comments
0
Likes
0
Embeds 0
No embeds

No notes for slide

Actors in Java

  1. 1. АКТЁРЫ В JVMПростая архитектура дляконкурентных отказоустойчивыхмасштабируемых Java-приложений
  2. 2. Данные
  3. 3. Данные
  4. 4. Данные
  5. 5. Данные
  6. 6. Данные
  7. 7. Данные
  8. 8. Данные
  9. 9. Данные
  10. 10. Данные
  11. 11. Данные
  12. 12. Данные
  13. 13. Данные
  14. 14. CalculateGet Lock Modify ДанныеRelease Lock
  15. 15. Get Lock A Modify A IF (cond) Get Lock B A Modify B B Release Release Lock A Lock B
  16. 16. Get Lock A Modify AGet Lock A Get Lock B Modify A Modify B Release Lock A Release Lock B Release Lock A
  17. 17. 2 1 Get Lock BGet Lock A 2 1 Modify B Modify A 2 Get Lock A 1 1 IF (cond) Get Lock B 2 Modify B 1 Modify B 2 Release A 1 1Release A Release B 2 Release B
  18. 18. 1 1 2Get Lock A Get Lock A Get Lock B 1 1 2 Modify A Modify A Modify B 1 1 2Get Lock B Get Lock B Get Lock A 1 1 2 Modify B Modify B Modify B 2 1 1 Release ARelease B Release B 2 2 1 Release BGet Lock B Release A 1 Get Lock A 2 2 Modify B Get Lock B 1 Modify A 1 2Release A Modify B 1 Get Lock B 2 2Get Lock A Get Lock A 1 2 2 Modify B Modify B Modify B 1 2 2 Release BRelease A Release A 2 2 1Release B Release B Release A
  19. 19. 1 1 2 2Get Lock A Get Lock A Get Lock B Get Lock B 1 1 2 2 Modify A Modify A Modify B Modify B 1 1 2 1Get Lock B Get Lock B Get Lock A Get Lock A 1 1 2 1 Modify B Modify B Modify B Modify A 2 1 1 Release A 1Release B Release B Get Lock B 2 2 1 Release B 2Get Lock B Release A 1 Get Lock A Get Lock A 2 2 Modify B Get Lock B 1 Modify A 1 2Release A Modify B 1 Get Lock B 2 2Get Lock A Get Lock A 1 2 2 Modify B Modify B Modify B 1 2 2 Release BRelease A Release A 2 2 1Release B Release B Release A
  20. 20. От чего избавиться?Что улучшить?
  21. 21. Взаимные блокировкиПоследовательный доступ
  22. 22. Разделяемая памятьСинхронные вызовыОчередь событий
  23. 23. Актер =состояние + поведение + почтовый ящик
  24. 24. A
  25. 25. A AA A AA AAAA A A A A AA A A A A AA AA AA A AA AA A AA A AA AA AA A AA A
  26. 26. A
  27. 27. A Данные
  28. 28. A ДанныеA A
  29. 29. A ДанныеA AA
  30. 30. A A
  31. 31. A A
  32. 32. A A AAA A A
  33. 33. A A AAA A A AA A A
  34. 34. AA
  35. 35. AAction ActionActionAction
  36. 36. AAction F ActionGet! F ActionAction F
  37. 37. AAction F ActionAction ActionAction FGet! F
  38. 38. Данные
  39. 39. A4 A8
  40. 40. A4 A8
  41. 41. Call! Action ActionAction ReturnAction
  42. 42. Action ActionAction ActionReturn Return
  43. 43. AДанные Данные копия A Данные копия 2
  44. 44. AA A A A A →
  45. 45. ?
  46. 46. AStart
  47. 47. A Login A AStart Register A Email Dispatcher
  48. 48. ALogin
  49. 49. A Login A Login ALogin A Login A Login
  50. 50. A Sender A Sender A Email ADispatcher Sender A Sender
  51. 51. A A UserSession1 User1 ALogin
  52. 52. A A UserSession1 User1 A ALogin UserSession2 A User2
  53. 53. A A UserSession1 User1 A ALogin UserSession2 A A UserSession3 User2
  54. 54. A A AUserSession1 A
  55. 55. A AUserSession1 Document1
  56. 56. A AUserSession1 Document1 A Document7
  57. 57. A AUserSession3 Document4 A AUserSession1 Document1 A AUserSession5 Document7
  58. 58. A A A Login Session User A AStart Register A Document A Email Dispatcher
  59. 59. A ASession User A Document
  60. 60. ASession1
  61. 61. ASession1 ADocument1
  62. 62. A Session1 A Document1A A A AUser User User User
  63. 63. A Document1 A A A AUser1 User2 User3 User4
  64. 64. A Document1 A A A A User1 User2 User3 User4 A A A A A ASession1 Session2 Session3 Session4 Session5 Session6
  65. 65. A ДанныеDocument1
  66. 66. A ДанныеDocument1
  67. 67. A Данные
  68. 68. A ДанныеGET Данные Копия Данные Копия
  69. 69. A ДанныеUPDATE
  70. 70. Actor
  71. 71. A UserSession1 User1 A ALogin UserSession2 A A UserSession3 User2
  72. 72. To:Session1 A A Login UserSession2 A A UserSession3 User2
  73. 73. To:Session1 A A Login UserSession2 A A UserSession3 User2
  74. 74. To:Session1 A A UserSession1 User1 A A Login UserSession2 A A UserSession3 User2
  75. 75. GET A Данные Данные Копия Данные Копия
  76. 76. A A UserSession1 User1 A ALogin UserSession2 A UserSession3 User2
  77. 77. A A UserSession1 User1 ALogin UserSession2 UserSession3 User2
  78. 78. A A UserSession1 User1 ALogin
  79. 79. A
  80. 80. A
  81. 81. AAAA
  82. 82. F AF AF AF A
  83. 83. F A F AF F A F A
  84. 84. F A F AF F A F A
  85. 85. F A F AF F A F A
  86. 86. akka.io
  87. 87. import akka.actor.Actorimport akka.event.EventHandlerpublic class HelloActor extends Actor { def receive = { case "hello" => HelloService().hi case s: String => HelloService().sayHello(s) case _ => EventHandler.info(this, "unknown message") }}
  88. 88. //Startval actorRef = Actor.actorOf[MyActor]actorRef.start()//Send messageactorRef ! "hello"actorRef ! new Date()//Obtain actorsval actors = Actor.registry.actorsFor(classOf[HelloActor])
  89. 89. //Askval response = actorRef ? "hello"//Get resultval result = response.getActor.become( newRecieveFunction )
  90. 90. import akka.actor.UntypedActor;import akka.event.EventHandler;public class HelloActor extends UntypedActor { public void onReceive(Object message) { if (message instanceof String) { HelloService.getInstance().sayHello(message); } }}
  91. 91. //StartActorRef ref = Actors.actorOf(HelloActor.class);ref.start();//Send messageref.tell("hello");ref.tell(new Date());//Obtain actorsActorRef[] actors = Actors.registry().actorsFor(HelloActor.class);
  92. 92. //AskFuture response = actorRef.ask("hello");…//Get resultString result = response.get();//Hello, synchronous call! Long time no see.String syncResult = actorRef.ask("hello").get();
  93. 93. • Actors• Typed Actors• Supervisors• Routing• Futures• STM• FSM• TestKit
  94. 94. class SomeSpec extends … TestKit { "A Worker" must { "send timely replies" in { val worker = actorOf(...) within (500 millis) { worker ! "some work" } } }}
  95. 95. Виктор Тесленкоvictor@teslenko.com.uatwitter: @vir

×