title: Akka Balancing Pool with variable-size Thread-Pool-Executor
body: <p>Here is my use case:</p>
  <p>I want to create a Balancing-Pool router of size x (x is the number of instances/routees that is determined at runtime) with each of the routees having its own dedicated thread.
  Each of the routees is expected to perform blocking operations.</p>
  <p>Akka documentation explains that you cannot change the dispatcher of a balancing pool. Which elimintes the option of using a Pinned Dispatcher (which would do exactly what I need, a threadpool of size 1 per routee).</p>
  <p>From <a href="http://doc.akka.io/docs/akka/current/scala/routing.html#BalancingPool" rel="nofollow noreferrer">Akka documentation</a>:</p>
  <blockquote>
  <p>The BalancingPool automatically uses a special BalancingDispatcher for its routees - disregarding any dispatcher that is set on the routee Props object. This is needed in order to implement the balancing semantics via sharing the same mailbox by all the routees.</p>
  <p>While it is not possible to change the dispatcher used by the routees, it is possible to fine tune the used executor. By default the <em><strong>fork-join-dispatcher</strong></em> is used and can be configured as explained in Dispatchers <em><strong>[AJ: I believe this is a typo and they mean fork-join-executor]</strong></em>. In situations where the routees are expected to perform blocking operations it may be useful to replace it with a thread-pool-executor hinting the number of allocated threads explicitly</p>
  </blockquote>
  <p>Config sample from Akka docs as well:</p>
  <pre><code>akka.actor.deployment {
    /parent/router10b {
      router = balancing-pool
      nr-of-instances = 5
      pool-dispatcher {
        executor = &quot;thread-pool-executor&quot;
  
        # allocate exactly 5 threads for this pool
        thread-pool-executor {
          core-pool-size-min = 5
          core-pool-size-max = 5
        }
      }
    }
  }
  </code></pre>
  <p>It seems like the threadpool executor with a pool size of matching the number of  routees would work, but here is the problem: How do I dynamically (at run-time) set the pool size of my threadpool executor to match the number of routees if the pool size has to be set explicitly in the configuration?</p>
  <p>Also, is it possible to create the Balancing-Pool (in code) while pulling the executor of its routees from config?
  i.e. I do not want to use the config to define my router, I want to use:</p>
  <pre><code>val myRouter = BalancingPool(x).props(Props[Worker])...)
  </code></pre>
  <p>but somehow specify which executor to use (from config), using the same style of withMailbox() or withDispatcher()</p>
  <p>Since the <a href="http://doc.akka.io/docs/akka/current/scala/dispatchers.html#Types_of_dispatchers" rel="nofollow noreferrer">Balancing Dispatcher is &quot;Driven by: java.util.concurrent.ExecutorService&quot;</a>, is it possible to create an ExecutorService and pass it to the balancing-pool router or its dispatcher?</p>