LIGHTNING TALKREPLACING THREADS WITH ACTORS FORCONCURRENT WEB APPLICATION SERVICECALLSMartin AndersonLJC Open Conference 2...
CONCURRENT SERVICE CALLS •  Most web pages need data from multiple    sources •  Scatter/Gather approach to perform all th...
CONCURRENCY MODEL                    dispatch service                          calls      rendering     controller’s job  ...
THREADS•  Well understood (if not always well   implemented!)•  java.util.concurrent library eases the pain•  Resource usa...
ACTORS•    Been around since 1973•    Erlang popularised usage•    Actor library for Java? Akka!•    Most strongly associa...
USING AN EXECUTOR SERVICE IN A SERVLET public void doGet(HttpServletRequest request, HttpServletResponse response) throws…...
USING AKKA ACTORS IN A SERVLET public void doGet(HttpServletRequest request, HttpServletResponse response) throws…{       ...
SO WHAT’S IN AN ACTOR 1? public class MasterActor extends UntypedActor {      private ActorRef router;      static class L...
SO WHAT’S IN AN ACTOR 2?  public MasterActor(final int numWorkers, final RemoteClient remoteClient) {         // create an...
SO WHAT’S IN AN ACTOR 3?      // message handler in MasterActor      public void onReceive(Object message) {          if (...
SO WHAT’S IN AN ACTOR 4? public class WorkerActor extends UntypedActor {      RemoteClient remoteClient;      public Worke...
SO WHAT’S IN OUR REMOTECLIENT?     public void execute(final Work work) throws IOException {       ContentExchange exchang...
SO WHY USE AKKA/ACTORS•     More resource efficient for certain jobs•     Easier for mere mortals to reason with•     Nest...
THANK YOU     https://github.com/bloodredsun/ConcurrentWebAppExample     Email: martin.anderson@betfair.com     Twitter: @...
Upcoming SlideShare
Loading in …5
×

Martin Anderson - threads v actors

2,076 views

Published on

Slides from my lightning presentation from the London Java Community Open Conference Nov 2011

Published in: Technology, Education
0 Comments
1 Like
Statistics
Notes
  • Be the first to comment

No Downloads
Views
Total views
2,076
On SlideShare
0
From Embeds
0
Number of Embeds
39
Actions
Shares
0
Downloads
24
Comments
0
Likes
1
Embeds 0
No embeds

No notes for slide

Martin Anderson - threads v actors

  1. 1. LIGHTNING TALKREPLACING THREADS WITH ACTORS FORCONCURRENT WEB APPLICATION SERVICECALLSMartin AndersonLJC Open Conference 2011
  2. 2. CONCURRENT SERVICE CALLS •  Most web pages need data from multiple sources •  Scatter/Gather approach to perform all the service calls in parallel then rendering the responses 2
  3. 3. CONCURRENCY MODEL dispatch service calls rendering controller’s job starts early done 3
  4. 4. THREADS•  Well understood (if not always well implemented!)•  java.util.concurrent library eases the pain•  Resource usage? 4
  5. 5. ACTORS•  Been around since 1973•  Erlang popularised usage•  Actor library for Java? Akka!•  Most strongly associated with Scala but has a Java API 5
  6. 6. USING AN EXECUTOR SERVICE IN A SERVLET public void doGet(HttpServletRequest request, HttpServletResponse response) throws…{ List<MyServiceCallable> myServiceCallables = new ArrayList<MyServiceCallable>(); for (int ii = 0; ii < 10; ii++) { myServiceCallables.add(new MyServiceCallablle(remoteClient)); } List<Future<String>> futures = executorService.invokeAll(myServiceCallables); … for (Future future : futures) { writer.write( future.get() ); } … } 6
  7. 7. USING AKKA ACTORS IN A SERVLET public void doGet(HttpServletRequest request, HttpServletResponse response) throws…{ Works works = new Works(); for (int ii = 0; ii < 10; ii++) { works.add(new Work())); } masterActor.tell(works); List<Future<String>> futures = works.getFutures(); … for (Future future : futures) { writer.write( future.get() ); } … } 7
  8. 8. SO WHAT’S IN AN ACTOR 1? public class MasterActor extends UntypedActor { private ActorRef router; static class LoadBalancer extends UntypedLoadBalancer { private final InfiniteIterator<ActorRef> workers; public LoadBalancer(ActorRef[] workers) { this.workers = new CyclicIterator<ActorRef>(asList(workers)); } public InfiniteIterator<ActorRef> seq() { return workers; } } 8
  9. 9. SO WHAT’S IN AN ACTOR 2? public MasterActor(final int numWorkers, final RemoteClient remoteClient) { // create an array of started workers final ActorRef[] workers = new ActorRef[numWorkers]; for (int i = 0; i < numWorkers; i++) { workers[i] = actorOf( new UntypedActorFactory() { public UntypedActor create() { return new WorkerActor(remoteClient); } }) .start(); } // wrap all the workers with a load-balancing router router = actorOf(new UntypedActorFactory() { public UntypedActor create() { return new LoadBalancer(workers); } }).start(); } 9
  10. 10. SO WHAT’S IN AN ACTOR 3? // message handler in MasterActor public void onReceive(Object message) { if (message instanceof Works) { for (Work work : ((Works) message).getAll()) { router.tell(work); } } } 10
  11. 11. SO WHAT’S IN AN ACTOR 4? public class WorkerActor extends UntypedActor { RemoteClient remoteClient; public WorkerActor(RemoteClient remoteClient) { this.remoteClient = remoteClient; } @Override public void onReceive(Object message) throws Exception { if (message instanceof Work) { Work work = (Work) message; remoteClient.execute(work); } } } 11
  12. 12. SO WHAT’S IN OUR REMOTECLIENT? public void execute(final Work work) throws IOException { ContentExchange exchange = new ContentExchange() { protected void onResponseComplete() throws IOException { super.onResponseComplete(); String responseContent = this.getResponseContent(); work.setFutureResponse(responseContent); } }; //set the method and the url exchange.setMethod("GET"); exchange.setURL(work.getUrl()); // start the exchange httpClient.send(exchange); } 12
  13. 13. SO WHY USE AKKA/ACTORS•  More resource efficient for certain jobs•  Easier for mere mortals to reason with•  Nested supervisors can restart failed actors•  Supports STM if required 13
  14. 14. THANK YOU https://github.com/bloodredsun/ConcurrentWebAppExample Email: martin.anderson@betfair.com Twitter: @mdjanderson Blog: http://bloodredsun.com/ Betfair Blog: http://views.betfair.com/14

×