Search code examples
javascalaakkatypesafe-config

Configure ActorSystem in another library


My application consumes a library which internally initialises an actor system like so

ActorSystem("MySys")

As a result, the MySys actor system uses the default dispatcher. I want to use a custom dispatcher for this specific actor system instead. How can I override the dispatcher used by this libraray's actor system in my application.conf?

I've already tried doing it like so

MySys {
  akka {
    actor {
      default-dispatcher = "my-dispatcher"
    }

    my-dispatcher {
      type = Dispatcher
      executor = "thread-pool-executor"
      thread-pool-executor {
        fixed-pool-size = 85
      }
    }

  }
}

However this does not work as a thread dump shows the following output

"MySys-akka.actor.default-dispatcher-6" #396 prio=5 os_prio=0 tid=0x00007f54501b6000 nid=0x1a6 waiting on condition [0x00007f55285f3000]
   java.lang.Thread.State: WAITING (parking)
        at sun.misc.Unsafe.park(Native Method)
        - parking to wait for  <0x00000000e4b5b1f0> (a akka.dispatch.ForkJoinExecutorConfigurator$AkkaForkJoinPool)
        at akka.dispatch.forkjoin.ForkJoinPool.scan(ForkJoinPool.java:2075)
        at akka.dispatch.forkjoin.ForkJoinPool.runWorker(ForkJoinPool.java:1979)
        at akka.dispatch.forkjoin.ForkJoinWorkerThread.run(ForkJoinWorkerThread.java:107)

"MySys-akka.actor.default-dispatcher-5" #363 prio=5 os_prio=0 tid=0x00007f545c006800 nid=0x185 waiting on condition [0x00007f5443acf000]
   java.lang.Thread.State: TIMED_WAITING (parking)
        at sun.misc.Unsafe.park(Native Method)
        - parking to wait for  <0x00000000e4b5b1f0> (a akka.dispatch.ForkJoinExecutorConfigurator$AkkaForkJoinPool)
        at akka.dispatch.forkjoin.ForkJoinPool.idleAwaitWork(ForkJoinPool.java:2135)
        at akka.dispatch.forkjoin.ForkJoinPool.scan(ForkJoinPool.java:2067)
        at akka.dispatch.forkjoin.ForkJoinPool.runWorker(ForkJoinPool.java:1979)
        at akka.dispatch.forkjoin.ForkJoinWorkerThread.run(ForkJoinWorkerThread.java:107)

"MySys-akka.actor.default-dispatcher-4" #362 prio=5 os_prio=0 tid=0x00007f55402e0000 nid=0x184 waiting on condition [0x00007f54445da000]
   java.lang.Thread.State: WAITING (parking)
        at sun.misc.Unsafe.park(Native Method)
        - parking to wait for  <0x00000000e4b5b1f0> (a akka.dispatch.ForkJoinExecutorConfigurator$AkkaForkJoinPool)
        at akka.dispatch.forkjoin.ForkJoinPool.scan(ForkJoinPool.java:2075)
        at akka.dispatch.forkjoin.ForkJoinPool.runWorker(ForkJoinPool.java:1979)
        at akka.dispatch.forkjoin.ForkJoinWorkerThread.run(ForkJoinWorkerThread.java:107)

"MySys-akka.actor.default-dispatcher-3" #361 prio=5 os_prio=0 tid=0x00007f55402df000 nid=0x183 waiting on condition [0x00007f5539774000]
   java.lang.Thread.State: WAITING (parking)
        at sun.misc.Unsafe.park(Native Method)
        - parking to wait for  <0x00000000e4b5b1f0> (a akka.dispatch.ForkJoinExecutorConfigurator$AkkaForkJoinPool)
        at akka.dispatch.forkjoin.ForkJoinPool.scan(ForkJoinPool.java:2075)
        at akka.dispatch.forkjoin.ForkJoinPool.runWorker(ForkJoinPool.java:1979)
        at akka.dispatch.forkjoin.ForkJoinWorkerThread.run(ForkJoinWorkerThread.java:107)

"MySys-akka.actor.default-dispatcher-2" #360 prio=5 os_prio=0 tid=0x00007f55402de800 nid=0x182 waiting on condition [0x00007f5440f31000]
   java.lang.Thread.State: WAITING (parking)
        at sun.misc.Unsafe.park(Native Method)
        - parking to wait for  <0x00000000e4b5b1f0> (a akka.dispatch.ForkJoinExecutorConfigurator$AkkaForkJoinPool)
        at akka.dispatch.forkjoin.ForkJoinPool.scan(ForkJoinPool.java:2075)
        at akka.dispatch.forkjoin.ForkJoinPool.runWorker(ForkJoinPool.java:1979)
        at akka.dispatch.forkjoin.ForkJoinWorkerThread.run(ForkJoinWorkerThread.java:107)

"MySys-scheduler-1" #359 prio=5 os_prio=0 tid=0x00007f55402dd800 nid=0x181 waiting on condition [0x00007f54440d5000]
   java.lang.Thread.State: TIMED_WAITING (sleeping)
        at java.lang.Thread.sleep(Native Method)
        at akka.actor.LightArrayRevolverScheduler.waitNanos(LightArrayRevolverScheduler.scala:85)
        at akka.actor.LightArrayRevolverScheduler$$anon$4.nextTick(LightArrayRevolverScheduler.scala:265)
        at akka.actor.LightArrayRevolverScheduler$$anon$4.run(LightArrayRevolverScheduler.scala:235)
        at java.lang.Thread.run(Thread.java:748)

Solution

  • If no configurations is passed when constructing a new ActorSystem then the root configuration will be used (i.e. everything inside the akka key)

    In the configuration section of the akka docs it is recommended that you initialize two different actor systems in the same application by specifically initializing them with separate configs.

    val config = ConfigFactory.load()
    val system1 = ActorSystem("MySys", config.getConfig("MySys"))
    val system2 = ActorSystem("my-app", config.getConfig("my-app"))
    

    If you can't influence how the first system is created I would suggest just use the default akka region in the configuration for MySys and then namespace the configuration you need for your application.