2

I am migrating my project to Wildfly10 and using cdi-api 1.2.

I have been using this structure to get the beanManager and then lookup some resources (example EJBs).

import javax.enterprise.inject.spi.BeanManager;
import javax.enterprise.inject.spi.CDI;

public abstract class ServiceLocator {
    //....
    private BeanManager getBeanManager() throws NamingException {
        return CDI.current().getBeanManager();
    }
    public static <T> T lookup(Class<T> clazz) {
        BeanManager manager = this.getBeanManager(); 
    //...
}

This works fine using in a Servlet or a Filter.

However, when I use this same function in a MDB JMS like:

@MessageDriven(mappedName = MyQueueProcessor.QUEUE_NAME, 
//...
public class RecebimentoMensagemCAM0021MDB {
    @Override
    protected void process(Object obj) {
        MyServices service = ServiceLocator.lookup(MyServices.class);
   //...

Occur this exception:

java.lang.IllegalStateException: WFLYWELD0039: Singleton not set for 
ModuleClassLoader for Module "org.apache.activemq.artemis.ra:main" 
from local module loader @629f0666 (finder: local module finder 
@1bc6a36e (roots: /home/confidence/wildfly-
10.1.0.Final/modules,/home/confidence/wildfly-
10.1.0.Final/modules/system/layers/base)). This means that you are 
trying to access a weld deployment with a Thread Context ClassLoader 
that is not associated with the deployment.
    at org.jboss.as.weld.services.ModuleGroupSingletonProvider$TCCLSingleton.get(ModuleGroupSingletonProvider.java:77)
    at org.jboss.as.weld.services.ModuleGroupSingletonProvider$TCCLSingleton.get(ModuleGroupSingletonProvider.java:134)
    at org.jboss.weld.Container.instance(Container.java:55)
    at org.jboss.as.weld.WeldProvider.getCDI(WeldProvider.java:61)
    at javax.enterprise.inject.spi.CDI.current(CDI.java:60)
    at br.com.project.ServiceLocator.getBeanManager(ServiceLocator.java:xx)
    at br.com.project.ServiceLocator.lookup(ServiceLocator.java:xx)

Anyone could help how to make this lookup works in the JMS.

Thanks in advance.

  • This is just a blind shot, but still - can you try **NOT** to use `CDI.current()` from MDB? Instead, try simply `@Inject BeanManager bm` directly into MDB. MDB is (from CDI standpoint) so called `InjectionTarget` hence still eligible to be injected into. – Siliarus Sep 13 '17 at 06:28
  • I tried to use @Inject, but it returned an null object – Thiago Chagas - Batata Sep 13 '17 at 23:03

1 Answers1

0

You can get an instance of your services using just CDI, not is necessary your locator, eg:

MyServices myServices = CDI.select(MyServices.class).get()

http://docs.jboss.org/cdi/learn/userguide/CDI-user-guide.html#_obtaining_a_contextual_instance_by_programmatic_lookup

Dilnei Cunha
  • 159
  • 4
  • 10