Hot questions for Using Enterprise JavaBeans in weld

Question:

Using WeldJUnit4Runner and getting error message:

Exception 0 : org.jboss.weld.exceptions.DeploymentException: WELD-001443: Pseudo scoped bean has circular dependencies. Dependency path: - Managed Bean [class com.my.own.bounded_contexts.client.cache.cacheClientCommPriorizedAcceptRequestService] with qualifiers [@Any @Default],

Any known solution for this problem in Weld?

More error message: java.lang.ExceptionInInitializerError at com.my.own.WeldJUnit4Runner.createTest(WeldJUnit4Runner.java:18) at org.junit.runners.BlockJUnit4ClassRunner$1.runReflectiveCall(BlockJUnit4ClassRunner.java:244) at org.junit.internal.runners.model.ReflectiveCallable.run(ReflectiveCallable.java:12) at org.junit.runners.BlockJUnit4ClassRunner.methodBlock(BlockJUnit4ClassRunner.java:241) at org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:70) at org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:50) at org.junit.runners.ParentRunner$3.run(ParentRunner.java:238) at org.junit.runners.ParentRunner$1.schedule(ParentRunner.java:63) at org.junit.runners.ParentRunner.runChildren(ParentRunner.java:236) at org.junit.runners.ParentRunner.access$000(ParentRunner.java:53) at org.junit.runners.ParentRunner$2.evaluate(ParentRunner.java:229) at org.junit.runners.ParentRunner.run(ParentRunner.java:309) at org.eclipse.jdt.internal.junit4.runner.JUnit4TestReference.run(JUnit4TestReference.java:86) at org.eclipse.jdt.internal.junit.runner.TestExecution.run(TestExecution.java:38) at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:459) at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:675) at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.run(RemoteTestRunner.java:382) at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.main(RemoteTestRunner.java:192) Caused by: org.jboss.weld.exceptions.DeploymentException: Exception List with 102 exceptions: Exception 0 : org.jboss.weld.exceptions.DeploymentException: WELD-001443: Pseudo scoped bean has circular dependencies. Dependency path: - Managed Bean [class com.my.own.bounded_contexts.client.cache.cacheClientCommPriorizedAcceptRequestService] with qualifiers [@Any @Default], - [BackedAnnotatedField] @Inject private com.my.own.bounded_contexts.client.cache.cacheClientCommPriorizedAcceptRequestService.listener, - Managed Bean [class com.my.own.bounded_contexts.client.cache.cacheClientCommPriorizedAcceptRequestListener] with qualifiers [@Any @Default], - [BackedAnnotatedField] @Inject private com.my.own.bounded_contexts.client.cache.cacheClientCommPriorizedAcceptRequestListener.clientCommPriorizedAcceptRequestRepository, - Managed Bean [class com.my.own.bounded_contexts.client.repositories.ClientCommPriorizedAcceptRequestRepository] with qualifiers [@Any @Default], - [BackedAnnotatedField] @Inject @ClientCommPriorizedAcceptRequestCache private com.my.own.bounded_contexts.client.repositories.ClientCommPriorizedAcceptRequestRepository.clientCallPriorizedAcceptRequestCache, - Producer Method [RemoteCache] with qualifiers [@ClientCommPriorizedAcceptRequestCache @Any] declared as [[BackedAnnotatedMethod] @Produces @ClientCommPriorizedAcceptRequestCache public com.my.own.bounded_contexts.client.cache.cacheClientCommPriorizedAcceptRequestService.getClientCommPriorizedAcceptRequestRemoteCache()], - Managed Bean [class com.my.own.bounded_contexts.client.cache.cacheClientCommPriorizedAcceptRequestService] with qualifiers [@Any @Default] at org.jboss.weld.bootstrap.Validator.reallyValidatePseudoScopedBean(Validator.java:897)


Answer:

Weld complies with the specification in this respect.

From the CDI spec, section 5:

The container is required to support circularities in the bean dependency graph where at least one bean participating in every circular chain of dependencies has a normal scope, as defined in Normal scopes and pseudo-scopes. The container is not required to support circular chains of dependencies where every bean participating in the chain has a pseudo-scope.

Try to get rid of the dependency cycle in your application, or at least give a normal scope to one of the beans involved.

Question:

While trying to migrate an application from JBoss EAP 5 to JBoss EAP as well as add some extra functionality such as JAX-RS I'm getting the following error:

WELD-000082: Scope interface javax.enterprise.context.RequestScoped is not allowed on stateless session beans for class net.MyCompany.My.service.MyIPAuthJaxRsService. Only @Dependent is allowed.

The full stack trace:

    1:02:29,721 ERROR [org.jboss.msc.service.fail] (MSC service thread 1-3) MSC000001: Failed to start service jboss.deployment.unit."C1Authentication.ear".WeldStartService: org.jboss.msc.service.StartException in service jboss.deployment.unit."C1Authentication.ear".WeldStartService: Failed to start service
        at org.jboss.msc.service.ServiceControllerImpl$StartTask.run(ServiceControllerImpl.java:1904)
        at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1142)
        at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:617)
        at java.lang.Thread.run(Thread.java:745)
    Caused by: org.jboss.weld.exceptions.DefinitionException: WELD-000082: Scope interface javax.enterprise.context.RequestScoped is not allowed on stateless session beans for class net.MyCompany.My.service.MyIPAuthJaxRsService. Only @Dependent is allowed.
        at org.jboss.weld.bean.SessionBean.checkScopeAllowed(SessionBean.java:122)
        at org.jboss.weld.bean.SessionBean.internalInitialize(SessionBean.java:101)
        at org.jboss.weld.bean.RIBean.initialize(RIBean.java:69)
        at org.jboss.weld.bootstrap.ConcurrentBeanDeployer$5.doWork(ConcurrentBeanDeployer.java:121)
        at org.jboss.weld.bootstrap.ConcurrentBeanDeployer$5.doWork(ConcurrentBeanDeployer.java:118)
        at org.jboss.weld.executor.IterativeWorkerTaskFactory$1.call(IterativeWorkerTaskFactory.java:63)
        at org.jboss.weld.executor.IterativeWorkerTaskFactory$1.call(IterativeWorkerTaskFactory.java:56)
        at java.util.concurrent.FutureTask.run(FutureTask.java:266)
        at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1142)
        at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:617)
        at java.lang.Thread.run(Thread.java:745)
        at org.jboss.threads.JBossThread.run(JBossThread.java:320)

In Eclipse, the projects facets are at EJB 3.2 and Java 1.7

My EJB-Jar.xml file contains:

    <?xml version="1.0" encoding="UTF-8"?>
    <ejb-jar xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://xmlns.jcp.org/xml/ns/javaee" xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/ejb-jar_3_2.xsd" version="3.2">
        <display-name>C1AuthService</display-name>
        <enterprise-beans>
            <session>
                <ejb-name>MyWSService</ejb-name>
                <ejb-class>net.MyCompany.My.service.MyWSService</ejb-class>
                <session-type>Stateless</session-type>
                <transaction-type>Container</transaction-type>
            </session>
            <session>
                <ejb-name>MyIPAuthJaxRsService</ejb-name>
                <ejb-class>net.MyCompany.My.service.MyIPAuthJaxRsService</ejb-class>
                <session-type>Stateless</session-type>
                <transaction-type>Container</transaction-type>
            </session>
            <session>
                <ejb-name>MyIPAuthJaxRsServiceApplication</ejb-name>
                <ejb-class>net.MyCompany.My.service.MyIPAuthJaxRsServiceApplication</ejb-class>
                <session-type>Stateless</session-type>
                <transaction-type>Container</transaction-type>
            </session>
        </enterprise-beans>
    </ejb-jar>

The Two JAX-RS Classes are:

    package net.mycompany.my.service;

    import javax.ws.rs.ApplicationPath;
    import javax.ws.rs.core.Application;
    import java.util.HashSet;
    import java.util.Set;

    @ApplicationPath(value="/C1AuthService")
    public class MyIPAuthJaxRsServiceApplication extends Application {

        private Set singletons = new HashSet();

        public MyIPAuthJaxRsServiceApplication() {
            singletons.add(new MyIPAuthJaxRsService());
        }

        @Override
        public Set getSingletons() {
            return singletons;
        }

        @Override
        public Set> getClasses() {
            // TODO Auto-generated method stub
            return null;
        }
    }
    package net.mycompany.my.service;

    import javax.ws.rs.GET;
    import javax.ws.rs.Path;

    @Path("/C1AuthService")
    public class MyIPAuthJaxRsService {
        public MyIPAuthJaxRsService() {
        }

        @GET
        @Path("/test")
        public String test() {
            return "Hello RESTFul 2";
        }

    }

Answer:

It turns out after searching the web a bit I found the following article which provided a clue: https://dzone.com/articles/valid-cdi-scopes-session-ejb

I also stumbled upon this: http://docs.jboss.org/resteasy/docs/2.0.0.GA/userguide/html/CDI.html

What appears to have been happening is in the EJB-Jar.xml i was declaring the EJB's as being stateless session beans and by not annotating them in the java classes themselves the CDI/Weld was defaulting them to being @Dependent, e.g. from the JBoss article A CDI bean that does not explicitly define a scope is @Dependent scoped by default.

Simply adding the following imports and annotations to both classes:

    package net.mycompany.my.service;

    import javax.ejb.Stateless;
    import javax.ws.rs.ApplicationPath;
    import javax.ws.rs.core.Application;
    import java.util.HashSet;
    import java.util.Set;

    @Stateless
    @ApplicationPath(value="/C1AuthService")
    public class MyIPAuthJaxRsServiceApplication extends Application {

        private Set singletons = new HashSet();

        public MyIPAuthJaxRsServiceApplication() {
            singletons.add(new MyIPAuthJaxRsService());
        }

    }
    package net.mycompany.my.service;

    import javax.ejb.Stateless;
    import javax.ws.rs.GET;
    import javax.ws.rs.Path;

    @Stateless
    @Path("/C1AuthService")
    public class MyIPAuthJaxRsService {
        public MyIPAuthJaxRsService() {
        }

        @GET
        @Path("/test")
        public String test() {
            return "Hello RESTFul 2";
        }

    }

Basically check that you've annotated your EJBs' with @Stateless and also added the proper import import javax.ejb.Stateless;

Edit - as @SteveC pointed out I've removed the overrides but when I tried to remove the @Stateless from MyIPAuthJaxRsServiceApplication as he suggested I ended up getting the exception again for MyIPAuthJaxRsServiceApplication. Also of note, this example isn't even working and thus having the EJB's in the EJB-Jar.xml and annotating may not have even been necessary, I was trying to use an EJB project and jar within an EAR, annotating the EJB's with JAX-RS but apparently its not supported as I'm working with the JBoss guys on it here https://developer.jboss.org/message/972433#972433

Looks like you have to have a servlet container for JAX-RS and the fact that EJB's can have web service annotations doesn't mean they're using a servlet container - oh well I tried!. Also, this Q & A was more for fixing the A CDI bean that does not explicitly define a scope is @Dependent scoped by default. which I struggled with for a bit and thought I'd help others if they run into the same thing.