title: Best practices for dealing with exceptions in Akka actors
body: <p>I have the following task, for which I have Java/Executors solution working well but I'd like to implement same functionality in Akka and looking for best practices suggestions.</p>
  
  <p><strong>Problem:</strong> </p>
  
  <p>Fetch/parse data from multiple URLs in parallel, block till all data to be fetched and return aggregated result. Should retry on errors (IOException etc) up to certain number of times.</p>
  
  <p>My implementation so far is pretty straightforward - create Fetcher actor which knows what URLs should be fetched, it creates bunch of Worker actors and send them URLs, one per message. Once done with particular URL Worker send message back to Fetcher with a result. Fetcher keeps state of results, Workers stateless. Simplified code below.</p>
  
  <p><strong>Fetcher:</strong></p>
  
  <pre><code>class Fetcher extends UntypedActor {
    private ActorRef worker;
  
    public void onReceive(Object message) throws Exception {
      if (message instanceof FetchMessage) {
        this.worker = context().actorOf(SpringExtension.SpringExtProvider.get(actorSystem).props("Worker")
                .withRouter(new RoundRobinPool(4)), "worker");
        for(URL u: urls) {
          this.worker.tell(new WorkUnit(u), getSelf());
        }
     }
     else if (message instanceof Result) {
       // accumulate results
     }
  }
  </code></pre>
  
  <p><strong>Worker:</strong></p>
  
  <pre><code>class Worker extends UntypedActor {
  
    public void onReceive(Object message) throws Exception {
      if (message instanceof WorkUnit) {
        // fetch URL, parse etc
        // send result back to sender
        getSender().tell(new Result(...), null);
      }
  }
  </code></pre>
  
  <p>So far so good and in absence of exceptions everything works as expected. </p>
  
  <p>But if there is say IOException in fetching URL in Worker then Akka would restart Worker actor but message that Worker was processing at the time is lost. Even if I use different SupervisorStrategy the result is the same - some of the messages are effectively 'lost'. Of course I could have wrapped code inside Worker.onReceive() with try/catch but I feel that this goes against Akka philosophy. I guess I could use persistent messaging but I don't think added complexity of message persistence is justified in this case. </p>
  
  <p>I need perhaps some way for a Fetcher to figure out that Worker failed on fetching some of the URLs and resend WorkUnit again or detect that some Results are not coming back for too long. What would be the best approach to handle this case?</p>
  
  <p>Thanks,</p>