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.
Here's a snippet I use to load classes dynamically from within an EJB:
File file = new File("D:\\workspace\\integrator\\target\\classes\\");
URL url = file.toURI().toURL();
URL[] urls = new URL[]{url};
ClassLoader cl = new URLClassLoader(urls, Thread.currentThread().getContextClassLoader());
Class cls = cl.loadClass("demo.DemoObserver");
observerInstance=cls.newInstance();
The loaded class is very simple, just a normal CDI event observer that is annotated as @Dependent to be "seen" as a bean. My beans.xml is configured to automatically discover beans.
@Dependent
public class DemoObserver {
private boolean observed=false;
public DemoObserver() {
observed=false;
}
@PermitAll
public void stateChanged(@Observes EquipmentE10StateChanged event) {
observed=true;
System.out.println("Demo observer received event E10 state changed: " + event.toString());
}
}
The class is loaded and instantiated (I can see logs coming from its constructor), but it never receives events.
Is there anything I can do to get this working?