Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<p>The really annoying thing about akka is that you need to update the thread local context classloader for the generic setting, and a lot of tools (like sbt) do not, because they do not know what thread you're running on or which classloader you will need.</p> <p>Here's <a href="https://github.com/typesafehub/activator/blob/master/project/LocalTemplateRepo.scala#L45-L50" rel="nofollow">an example from the activator</a> build:</p> <pre><code>val cl = ??? // Some mechanism of getting Akka's classpath with your classes too val old = Thread.currentThread.getContextClassLoader Thread.currentThread.setContextClassLoader(cl) try doSomethingWithAkka() finally Thread.currentThread.setContextClassLoader(old) </code></pre> <hr/> <p>Edit (by OP) so that this is more visible than in the comments:</p> <p>Akka (since 2.0) chooses the class loader it uses in ActorSystem (when it's not passed as an argument) by picking the first available (and non-null) among:</p> <ol> <li><code>Thread.currentThread.getContextClassLoader</code></li> <li><code>getClassLoader</code> on the class of a non-Akka stack frame up the call stack</li> <li><code>ActorSystem.getClass.getClassLoader</code></li> </ol> <p>So that's why the above solution of mutating the context class loader works. </p> <p>As to the class loader to use in practice, I got the expected behaviour with <code>val cl = getClass.getClassLoader</code> (from my build definition class) since that class loader contains all the build definition, plugins and dependencies. Also it effectively makes step 1 above behave like step 2.</p> <p>In the end though, I've settled with just creating the ActorSystem by calling <code>ActorSystem("someName", ConfigFactory.load(cl), cl)</code> and not touching the context class loader, which seems somewhat cleaner (and less frightening to someone like me who does not know what the context class loader is or does)</p> <p><strong>TL;DR</strong> Instead of</p> <pre><code>val system = ActorSystem() </code></pre> <p>write</p> <pre><code>val cl = getClass.getClassLoader val system = ActorSystem("mySystem", ConfigFactory.load(cl), cl) </code></pre>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload