title: How to load CDI event observers dynamically?
body: <p>I have an application running on JBoss / Wildfly. I use CDI event to communicate between modules. I would like to be able to use events as a way to plug in customisations, basically dynamically loading an additional observer that would also receive CDI Events.</p>
  <p>Here's a snippet I use to load classes dynamically from within an EJB:</p>
  <pre><code>File file = new File(&quot;D:\\workspace\\integrator\\target\\classes\\&quot;);
              
  URL url = file.toURI().toURL();
  URL[] urls = new URL[]{url};
  
  ClassLoader cl = new URLClassLoader(urls, Thread.currentThread().getContextClassLoader());
  
  Class cls = cl.loadClass(&quot;demo.DemoObserver&quot;);
  observerInstance=cls.newInstance();
  </code></pre>
  <p>The loaded class is very simple, just a normal CDI event observer that is annotated as @Dependent to be &quot;seen&quot; as a bean. My beans.xml is configured to automatically discover beans.</p>
  <pre><code>@Dependent
  public class DemoObserver {
      
      private boolean observed=false;
      
      public DemoObserver() {
          observed=false;
      }
      
      @PermitAll
      public void stateChanged(@Observes EquipmentE10StateChanged event) {
              observed=true;
              System.out.println(&quot;Demo observer received event E10 state changed: &quot; + event.toString());
      }
  
  }
  </code></pre>
  <p>The class is loaded and instantiated (I can see logs coming from its constructor), but it never receives events.</p>
  <p>Is there anything I can do to get this working?</p>