title: Creating Spring-backed actors as routees
body: <p>Problem: I would like to have a thread pool allocated to a number of actors making some blocking calls (eg. to a web service, database, etc).</p>
  
  <p>I have the following configuration:</p>
  
  <pre><code>threadpool-dispatcher {
    type = Dispatcher
    executor = "thread-pool-executor"
    thread-pool-executor {
      core-pool-size-min = 4
      core-pool-size-max = 8
    }
  }
  </code></pre>
  
  <p>and the following setup method for my <strong>Router</strong> actor:</p>
  
  <pre><code>private final int numberOfChildren = 4;
  
  @Override
  public void preStart() throws Exception {
      log.info("Starting up.. creating {} children", numberOfChildren);
      List&lt;Routee&gt; routees = new ArrayList&lt;&gt;();
      for (int i = 0; i &lt; numberOfChildren; i++) {
          ActorRef actor = getContext().actorOf(springActorExtension.props(routeeClass)
                                  .withDispatcher(THREADPOOL_DISPATCHER));
          getContext().watch(actor);
          routees.add(new ActorRefRoutee(actor));
      }
      router = new Router(new SmallestMailboxRoutingLogic(), routees);
      super.preStart();
  }
  </code></pre>
  
  <p>SpringActorExtension is similar to the code presented <a href="http://kimrudolph.de/blog/spring-boot-meets-akka/" rel="nofollow">here</a>, it just uses Class objects instead of names.</p>
  
  <p>The routees are created by Spring in the end, for example:</p>
  
  <pre><code>@Bean
  @Scope(value = "prototype")
  public HttpClientActor httpClient() {
      return new HttpClientActor();
  }
  </code></pre>
  
  <p>All this works fine, but my problem is that the number of threads that the thread pool executor is configured to use must be declared at two places:</p>
  
  <ol>
  <li>in the threadpool-dispatcher configuration</li>
  <li>in the code of the router (<code>numberOfChildren</code>)</li>
  </ol>
  
  <p>Reading through the <a href="http://doc.akka.io/docs/akka/snapshot/scala/routing.html#Pool" rel="nofollow">Akka docs</a> there seems to be a way to create routees automatically based on the configuration alone, eg. </p>
  
  <pre><code>akka.actor.deployment {
    /parent/router1 {
      router = round-robin-pool
      nr-of-instances = 5
    }
  }
  </code></pre>
  
  <p>However, that does not say how (by whom) should the routee actors be created.</p>
  
  <p>Is there a way to either</p>
  
  <ol>
  <li>explain Akka to have the routees created by Spring</li>
  <li>or access the <code>thread-pool-executor</code> configuration (eg. the number of threads) in the code above and remove the need for specifying the number of threads two times.</li>
  </ol>