title: AKKA - Spring MVC and service - Timeout Exception
body: <p>I have a spring service in Spring MVC web app which calls an actor system to compute a value. When I fire multiple times on webapp the application launch an <code>TimeoutException</code>. Only the first computation is done.</p>
  
  <p>Could you give me some help ?</p>
  
  <p>Thanks</p>
  
  <pre><code>@Service
  public class Service {
  
      public static final int processors = Runtime.getRuntime().availableProcessors();
      @Value("${Iterations}")
      long numberOfIterations;
      @Value("${constante}")
      double constante;
  
      ActorSystem system;
      ActorRef master;
  
      public Serice() {
          // Create an Akka system
          system = ActorSystem.create("ComputationSystem");
  
          // create the master
          master = system.actorOf(new Props(new UntypedActorFactory() {
              public UntypedActor create() {
                  return new Master(constante);
              }
          }));
      }
  
      @PreDestroy
      public void cleanUp() throws Exception {
          system.shutdown();
      }
  
      @Override
      public double calculatePrice(double x, double y, double z,
              double ex) {
  
  
          // start the calculation
          Work work = new Work(numberOfIterations, x, y, z,
                  ex);
  
          Timeout timeout = new Timeout(Duration.create(60, "seconds"));
          Future&lt;Object&gt; future = ask(master, work, timeout);
  
          double total = 0;
          try {
              total = (Double) Await.result(future,
                      timeout.duration());
          } catch (Exception e) {
              throw new RuntimeException(e);
          } finally {
  
          }
  
          return total;
      }
  
  }
  
  
  public class Master extends UntypedActor {
      private final ActorRef workerRouter;
      private double total = 0;
      private int answerReceived = 0;
      private long nbPerThreads;
      private double x;
      private double constante;
      private ActorRef replayTo;
  
      public Master(final double constante) {
          workerRouter = this.getContext().actorOf(
                  new Props(new UntypedActorFactory() {
                      public UntypedActor create() {
                          return new Worker(constante);
                      }
                  }).withRouter(new RoundRobinRouter(Algo.processors)),
                  "workerRouter");
          this.constante = constante;
      }
  
      public void onReceive(Object message) {
          if (message instanceof Work) {
              Work work = (Work) message;
  
              replayTo = getSender();
              nbPerThreads = work.nbIterations / Algo.processors;
              x = work.x / 360.0;
  
              // Modify the message to give the right to the workers
              work.nbIterations = nbPerThreads;
              work.x = x;
  
              for (int i = 0; i &lt; Algo.processors; i++) {
  
                  workerRouter.tell(work, getSelf());
              }
              return;
          }
  
          if (message instanceof Double) {
  
  
              Double result = (Double) message;
              total += result;
              if (++answerReceived == Algo.processors) {
                  double meanOfPremiums = total / (nbPerThreads * Algo.processors); 
  
                  double result = Math.exp(-constante * x) * meanOfPremiums;
  
                  System.out.println("returning answer :" + message);
                  // Return the answer
                  replayTo.tell(result, getSelf());
              }
              return;
          }
          unhandled(message);
      }
  }
  </code></pre>