0

My problem is similar to @InterceptorBinding / CDI/ EJB 3.2 - problems with injection

But my question is this: Why does resource / EJB injection work fine for an EJB interceptor but breaks when doing it the CDI way? Especially since EJB and CDI seem to work very much the same way in Java EE 7 in all other cases (@Inject + @EJB in POJOs...)

Is this difference documented somewhere or is it a bug?

Setup Glassfish 4.1 (EJB 3.2), Weld 2.2.2.Final (CDI 1.1) => Java EE 7

working resource injection (EJB)

public class ResourceInjectedInterceptor {

    @Resource(name = "java:comp/DefaultManagedExecutorService")
    private ManagedExecutorService managedExecutorService;

    @Resource
    private ManagedExecutorService managedExecutorService2;

    @EJB
    private SomeEjb someEjb;

    @AroundInvoke
    public Object intercept(InvocationContext ctx) throws Exception {
        return ctx.proceed();
    }
}

@Stateless
public class InterceptedBean {

    @Interceptors(AsyncTestInterceptor.class)
    public void beingIntercepted() {
        //...
    }
}

in the above version all three managedExecutorService, managedExecutorService2 and someEjb get injected, also the interceptor works as expected.

broken resource injection (CDI)

@Intercept
@Interceptor
public class ResourceInjectedInterceptor {

    @Resource(name = "java:comp/DefaultManagedExecutorService")
    private ManagedExecutorService managedExecutorService;

    @Resource
    private ManagedExecutorService managedExecutorService2;

    @EJB
    private SomeEjb someEjb;

    @AroundInvoke
    public Object intercept(InvocationContext ctx) throws Exception {
        return ctx.proceed();
    }
}

@Inherited
@InterceptorBinding
@Retention(RUNTIME)
@Target({METHOD, TYPE})
public @interface Intercept {
}

public class InterceptedBean {

    @Intercept
    public void beingIntercepted() {
        //...
    }
}

<beans bean-discovery-mode="all"
       xmlns="http://xmlns.jcp.org/xml/ns/javaee"
       xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
       xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/beans_1_1.xsd">
    <interceptors>
        <class>com.xxx.ejb.ResourceInjectedInterceptor</class>
    </interceptors>
</beans>

results in this exception during runtime when the interceptor is being instantiated (please see below for the full stacktrace) - the @Resource without name does not work as expected:

Caused by: javax.naming.NameNotFoundException: 
    No object bound to name java:comp/env/com.xxx.ejb.ResourceInjectedInterceptor/managedExecutorService2

Removing managedExecutorService2 "fixes" the problem, but then there is the analog exception for someEjb. Removing someEjb as well results in an working iterceptor which is missing vital parts :)

What I have tried

  • experimenting with various changes of the annotations till i understood that it basically comes down to EJB vs. CDI
  • adding @javax.annotation.ManagedBean to the CDI interceptor
  • searching for Glassfish/Weld relevant bug reports
  • wondering how the lookup-name "java:comp/env/com.xxx.ejb.ResourceInjectedInterceptor/managedExecutorService2" in the error message is generated when it should be "java:comp/DefaultManagedExecutorService" as in the manual lookup
  • reading in the weld documentation not finding any clues

complete stack trace

javax.ejb.EJBException: javax.ejb.EJBException: javax.ejb.CreateException: Could not create stateless EJB
    at com.sun.ejb.containers.StatelessSessionContainer._getContext(StatelessSessionContainer.java:435)
    at com.sun.ejb.containers.BaseContainer.getContext(BaseContainer.java:2579)
    at com.sun.ejb.containers.BaseContainer.preInvoke(BaseContainer.java:1971)
    at com.sun.ejb.containers.EJBLocalObjectInvocationHandler.invoke(EJBLocalObjectInvocationHandler.java:210)
    at com.sun.ejb.containers.EJBLocalObjectInvocationHandlerDelegate.invoke(EJBLocalObjectInvocationHandlerDelegate.java:88)
    at com.sun.proxy.$Proxy969.computeConstructions(Unknown Source)
    at com.xxx.ejb.service.__EJB31_Generated__ConstructionComputation__Intf____Bean__.computeConstructions(Unknown Source)
    at com.xxx.ejb.constructionComputation.web.ConstructionComputationService$1.executeService(ConstructionComputationService.java:52)
    at com.xxx.ejb.constructionComputation.web.ConstructionComputationService$1.executeService(ConstructionComputationService.java:47)
    at com.xxx.ejb.ui.util.GenericService.genericServiceCall(GenericService.java:148)
    at com.xxx.ejb.constructionComputation.web.ConstructionComputationService.computeConstructions(ConstructionComputationService.java:55)
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
    at java.lang.reflect.Method.invoke(Method.java:497)
    at flex.messaging.services.remoting.adapters.JavaAdapter.invoke(JavaAdapter.java:418)
    at flex.messaging.services.RemotingService.serviceMessage(RemotingService.java:183)
    at flex.messaging.MessageBroker.routeMessageToService(MessageBroker.java:1400)
    at flex.messaging.endpoints.AbstractEndpoint.serviceMessage(AbstractEndpoint.java:1005)
    at flex.messaging.endpoints.amf.MessageBrokerFilter.invoke(MessageBrokerFilter.java:103)
    at flex.messaging.endpoints.amf.LegacyFilter.invoke(LegacyFilter.java:158)
    at flex.messaging.endpoints.amf.SessionFilter.invoke(SessionFilter.java:44)
    at flex.messaging.endpoints.amf.BatchProcessFilter.invoke(BatchProcessFilter.java:67)
    at flex.messaging.endpoints.amf.SerializationFilter.invoke(SerializationFilter.java:166)
    at flex.messaging.endpoints.BaseHTTPEndpoint.service(BaseHTTPEndpoint.java:291)
    at flex.messaging.MessageBrokerServlet.service(MessageBrokerServlet.java:353)
    at javax.servlet.http.HttpServlet.service(HttpServlet.java:790)
    at org.apache.catalina.core.StandardWrapper.service(StandardWrapper.java:1682)
    at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:318)
    at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:160)
    at org.apache.catalina.core.StandardPipeline.doInvoke(StandardPipeline.java:734)
    at org.apache.catalina.core.StandardPipeline.invoke(StandardPipeline.java:673)
    at com.sun.enterprise.web.WebPipeline.invoke(WebPipeline.java:99)
    at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:174)
    at org.apache.catalina.connector.CoyoteAdapter.doService(CoyoteAdapter.java:415)
    at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:282)
    at com.sun.enterprise.v3.services.impl.ContainerMapper$HttpHandlerCallable.call(ContainerMapper.java:459)
    at com.sun.enterprise.v3.services.impl.ContainerMapper.service(ContainerMapper.java:167)
    at org.glassfish.grizzly.http.server.HttpHandler.runService(HttpHandler.java:201)
    at org.glassfish.grizzly.http.server.HttpHandler.doHandle(HttpHandler.java:175)
    at org.glassfish.grizzly.http.server.HttpServerFilter.handleRead(HttpServerFilter.java:235)
    at org.glassfish.grizzly.filterchain.ExecutorResolver$9.execute(ExecutorResolver.java:119)
    at org.glassfish.grizzly.filterchain.DefaultFilterChain.executeFilter(DefaultFilterChain.java:284)
    at org.glassfish.grizzly.filterchain.DefaultFilterChain.executeChainPart(DefaultFilterChain.java:201)
    at org.glassfish.grizzly.filterchain.DefaultFilterChain.execute(DefaultFilterChain.java:133)
    at org.glassfish.grizzly.filterchain.DefaultFilterChain.process(DefaultFilterChain.java:112)
    at org.glassfish.grizzly.ProcessorExecutor.execute(ProcessorExecutor.java:77)
    at org.glassfish.grizzly.nio.transport.TCPNIOTransport.fireIOEvent(TCPNIOTransport.java:561)
    at org.glassfish.grizzly.strategies.AbstractIOStrategy.fireIOEvent(AbstractIOStrategy.java:112)
    at org.glassfish.grizzly.strategies.WorkerThreadIOStrategy.run0(WorkerThreadIOStrategy.java:117)
    at org.glassfish.grizzly.strategies.WorkerThreadIOStrategy.access$100(WorkerThreadIOStrategy.java:56)
    at org.glassfish.grizzly.strategies.WorkerThreadIOStrategy$WorkerThreadRunnable.run(WorkerThreadIOStrategy.java:137)
    at org.glassfish.grizzly.threadpool.AbstractThreadPool$Worker.doWork(AbstractThreadPool.java:565)
    at org.glassfish.grizzly.threadpool.AbstractThreadPool$Worker.run(AbstractThreadPool.java:545)
    at java.lang.Thread.run(Thread.java:745)
Caused by: javax.ejb.EJBException: javax.ejb.CreateException: Could not create stateless EJB
    at com.sun.ejb.containers.StatelessSessionContainer$SessionContextFactory.create(StatelessSessionContainer.java:700)
    at com.sun.ejb.containers.util.pool.NonBlockingPool.getObject(NonBlockingPool.java:246)
    at com.sun.ejb.containers.StatelessSessionContainer._getContext(StatelessSessionContainer.java:430)
    ... 54 more
Caused by: javax.ejb.CreateException: Could not create stateless EJB
    at com.sun.ejb.containers.StatelessSessionContainer.createStatelessEJB(StatelessSessionContainer.java:514)
    at com.sun.ejb.containers.StatelessSessionContainer.access$000(StatelessSessionContainer.java:97)
    at com.sun.ejb.containers.StatelessSessionContainer$SessionContextFactory.create(StatelessSessionContainer.java:698)
    ... 56 more
Caused by: java.lang.IllegalStateException: Exception attempting to inject Env-Prop: com.xxx.ejb.ResourceInjectedInterceptor/managedExecutorService2@Field-Injectable Resource. Class name = com.xxx.ejb.ResourceInjectedInterceptor Field name=managedExecutorService2@java.lang.String@@@ into class com.xxx.ejb.ResourceInjectedInterceptor: Lookup failed for 'java:comp/env/com.xxx.ejb.ResourceInjectedInterceptor/managedExecutorService2' in SerialContext[myEnv={java.naming.factory.initial=com.sun.enterprise.naming.impl.SerialInitContextFactory, java.naming.factory.state=com.sun.corba.ee.impl.presentation.rmi.JNDIStateFactoryImpl, java.naming.factory.url.pkgs=com.sun.enterprise.naming}
    at org.glassfish.weld.services.InjectionServicesImpl.aroundInject(InjectionServicesImpl.java:175)
    at org.jboss.weld.injection.InjectionContextImpl.run(InjectionContextImpl.java:46)
    at org.jboss.weld.injection.producer.ResourceInjector.inject(ResourceInjector.java:65)
    at org.jboss.weld.injection.producer.BasicInjectionTarget.inject(BasicInjectionTarget.java:109)
    at org.jboss.weld.interceptor.reader.PlainInterceptorFactory.create(PlainInterceptorFactory.java:58)
    at org.jboss.weld.interceptor.proxy.InterceptionContext.initInterceptorInstanceMap(InterceptionContext.java:87)
    at org.jboss.weld.interceptor.proxy.InterceptionContext.of(InterceptionContext.java:64)
    at org.jboss.weld.interceptor.proxy.InterceptionContext.forNonConstructorInterception(InterceptionContext.java:60)
    at org.jboss.weld.injection.producer.InterceptorApplyingInstantiator.newInstance(InterceptorApplyingInstantiator.java:51)
    at org.jboss.weld.injection.producer.BasicInjectionTarget.produce(BasicInjectionTarget.java:104)
    at org.jboss.weld.injection.producer.BeanInjectionTarget.produce(BeanInjectionTarget.java:190)
    at org.jboss.weld.bean.ManagedBean.create(ManagedBean.java:149)
    at org.jboss.weld.context.unbound.DependentContextImpl.get(DependentContextImpl.java:69)
    at org.jboss.weld.manager.BeanManagerImpl.getReference(BeanManagerImpl.java:740)
    at org.jboss.weld.manager.BeanManagerImpl.getInjectableReference(BeanManagerImpl.java:831)
    at org.jboss.weld.injection.FieldInjectionPoint.inject(FieldInjectionPoint.java:92)
    at org.jboss.weld.util.Beans.injectBoundFields(Beans.java:365)
    at org.jboss.weld.util.Beans.injectFieldsAndInitializers(Beans.java:376)
    at org.jboss.weld.injection.producer.DefaultInjector$1.proceed(DefaultInjector.java:71)
    at org.glassfish.weld.services.InjectionServicesImpl.aroundInject(InjectionServicesImpl.java:172)
    at org.jboss.weld.injection.InjectionContextImpl.run(InjectionContextImpl.java:46)
    at org.jboss.weld.injection.producer.DefaultInjector.inject(DefaultInjector.java:68)
    at org.jboss.weld.injection.producer.StatelessSessionBeanInjector.inject(StatelessSessionBeanInjector.java:58)
    at org.jboss.weld.injection.producer.ejb.SessionBeanInjectionTarget.inject(SessionBeanInjectionTarget.java:117)
    at org.glassfish.weld.services.JCDIServiceImpl.injectEJBInstance(JCDIServiceImpl.java:257)
    at com.sun.ejb.containers.BaseContainer.injectEjbInstance(BaseContainer.java:1748)
    at com.sun.ejb.containers.StatelessSessionContainer.createStatelessEJB(StatelessSessionContainer.java:475)
    ... 58 more
Caused by: com.sun.enterprise.container.common.spi.util.InjectionException: Exception attempting to inject Env-Prop: com.xxx.ejb.ResourceInjectedInterceptor/managedExecutorService2@Field-Injectable Resource. Class name = com.xxx.ejb.ResourceInjectedInterceptor Field name=managedExecutorService2@java.lang.String@@@ into class com.xxx.ejb.ResourceInjectedInterceptor: Lookup failed for 'java:comp/env/com.xxx.ejb.ResourceInjectedInterceptor/managedExecutorService2' in SerialContext[myEnv={java.naming.factory.initial=com.sun.enterprise.naming.impl.SerialInitContextFactory, java.naming.factory.state=com.sun.corba.ee.impl.presentation.rmi.JNDIStateFactoryImpl, java.naming.factory.url.pkgs=com.sun.enterprise.naming}
    at com.sun.enterprise.container.common.impl.util.InjectionManagerImpl._inject(InjectionManagerImpl.java:740)
    at com.sun.enterprise.container.common.impl.util.InjectionManagerImpl.inject(InjectionManagerImpl.java:507)
    at org.glassfish.weld.services.InjectionServicesImpl.aroundInject(InjectionServicesImpl.java:119)
    ... 84 more
Caused by: javax.naming.NamingException: Lookup failed for 'java:comp/env/com.xxx.ejb.ResourceInjectedInterceptor/managedExecutorService2' in SerialContext[myEnv={java.naming.factory.initial=com.sun.enterprise.naming.impl.SerialInitContextFactory, java.naming.factory.state=com.sun.corba.ee.impl.presentation.rmi.JNDIStateFactoryImpl, java.naming.factory.url.pkgs=com.sun.enterprise.naming} [Root exception is javax.naming.NameNotFoundException: No object bound to name java:comp/env/com.xxx.ejb.ResourceInjectedInterceptor/managedExecutorService2]
    at com.sun.enterprise.naming.impl.SerialContext.lookup(SerialContext.java:491)
    at com.sun.enterprise.naming.impl.SerialContext.lookup(SerialContext.java:438)
    at javax.naming.InitialContext.lookup(InitialContext.java:417)
    at javax.naming.InitialContext.lookup(InitialContext.java:417)
    at com.sun.enterprise.container.common.impl.util.InjectionManagerImpl._inject(InjectionManagerImpl.java:636)
    ... 86 more
Caused by: javax.naming.NameNotFoundException: No object bound to name java:comp/env/com.xxx.ejb.ResourceInjectedInterceptor/managedExecutorService2
    at com.sun.enterprise.naming.impl.GlassfishNamingManagerImpl.lookup(GlassfishNamingManagerImpl.java:741)
    at com.sun.enterprise.naming.impl.GlassfishNamingManagerImpl.lookup(GlassfishNamingManagerImpl.java:715)
    at com.sun.enterprise.naming.impl.JavaURLContext.lookup(JavaURLContext.java:159)
    at com.sun.enterprise.naming.impl.SerialContext.lookup(SerialContext.java:471)
    ... 90 more
Community
  • 1
  • 1
Benjamin Seiller
  • 2,875
  • 2
  • 32
  • 42
  • just wondering, could you try `mappedName` instead of `name`? – John Ament Dec 10 '15 at 16:33
  • @JohnAment: Yes I can try tomorrow - but the injection with `@Resource(name = "java:comp/DefaultManagedExecutorService")` actually works in both cases - only the one without the attribute results in the error – Benjamin Seiller Dec 10 '15 at 17:06
  • 1
    @Benjamin so it seems that you've found a solution. Just to add my two cents: So far I've always injected EJB `@Resource`s inside CDI beans via `name` parameter - I've never drill down 'why'. – G. Demecki Dec 11 '15 at 07:48
  • 1
    @G.Demecki: :) There is definitely some wisdom in not asking why. But in this case not knowing the why came around to bite me again at least two times, so it's not only curiosity that makes me ask this question but also the wish to prevent that from happening again. And again... – Benjamin Seiller Dec 11 '15 at 09:33
  • I'm inclined to say that the why is a GF specific issue, where its probably treating the EJB container and CDI container as distinct deployments, and only sharing their contexts. You may want to take it up with them, as I doubt you'll see this in all containers (though now I'm curious to try myself). – John Ament Dec 12 '15 at 06:55
  • @JohnAment: Well, actually I haven't tested with any other application server, since I'm pretty sure the project won't deploy in say Wildfly anyway. So I have to build a minimal version which is probably a good idea anyway. I'll post an update as soon as that's done. After that we could test with Wildfly and co... – Benjamin Seiller Dec 12 '15 at 15:36

0 Answers0