0

I'm doing some CDI tests in Eclipse Mars and WildFly 8.2.

I have a Dynamic Web Project (as module of an EAR) ItsProjectWEB hosting a simple session scoped bean

@SessionScoped
public class Cart implements Serializable {

    private static final long serialVersionUID = 1L;

    public Cart() {}

    List<String> list = new ArrayList<>();

    public List<String> addAndGet(String item){
        list.add(item);
        return list;
    }

}

and a servlet where the bean is injected and invoked

@WebServlet("/ItsServlet")
public class ItsServlet extends HttpServlet {

    @Inject Cart cart;

    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {

        StringBuilder text=new StringBuilder();
        text.append("Cart = "+cart.addAndGet("Item #"+LocalTime.now().getSecond()));
        text.append("\nServed at: "+request.getContextPath());

        response.getWriter().append(text);
    }

    //irrelevant code
}

and everything works fine, this is the structure of the project:

enter image description here

My idea is to make a separate Dynamic Web Project where putting the CDI beans, add this project to my EAR and being able to reference it from inside the other modules. This is what I did:

  1. built a separate project ItsProjectCDI by copying the structure of the first one

enter image description here

  1. Added this project to the Java Build Path of ItsProjectWEB (rightclick -> Java Build Path -> Projects -> Add...)

  2. Tried to inject the bean inside the servlet via @Inject ItsCart cart;

but then I get this errors in the server log

13:22:46,089 INFO  [org.jboss.as.server.deployment] (MSC service thread 1-2) JBAS015876: Starting deployment of "ItsProjectEAR.ear" (runtime-name: "ItsProjectEAR.ear")
13:22:46,095 INFO  [org.jboss.as.server.deployment] (MSC service thread 1-2) JBAS015973: Starting subdeployment (runtime-name: "ItsProjectCDI.war")
13:22:46,096 INFO  [org.jboss.as.server.deployment] (MSC service thread 1-2) JBAS015973: Starting subdeployment (runtime-name: "ItsProjectWEB.war")
13:22:46,184 INFO  [org.jboss.weld.deployer] (MSC service thread 1-3) JBAS016002: Processing weld deployment ItsProjectEAR.ear
13:22:46,197 INFO  [org.jboss.weld.deployer] (MSC service thread 1-3) JBAS016002: Processing weld deployment ItsProjectCDI.war
13:22:46,223 ERROR [org.jboss.msc.service.fail] (MSC service thread 1-3) MSC000001: Failed to start service jboss.deployment.subunit."ItsProjectEAR.ear"."ItsProjectWEB.war".POST_MODULE: org.jboss.msc.service.StartException in service jboss.deployment.subunit."ItsProjectEAR.ear"."ItsProjectWEB.war".POST_MODULE: JBAS018733: Failed to process phase POST_MODULE of subdeployment "ItsProjectWEB.war" of deployment "ItsProjectEAR.ear"
    at org.jboss.as.server.deployment.DeploymentUnitPhaseService.start(DeploymentUnitPhaseService.java:166) [wildfly-server-8.2.1.Final.jar:8.2.1.Final]
    at org.jboss.msc.service.ServiceControllerImpl$StartTask.startService(ServiceControllerImpl.java:1948) [jboss-msc-1.2.2.Final.jar:1.2.2.Final]
    at org.jboss.msc.service.ServiceControllerImpl$StartTask.run(ServiceControllerImpl.java:1881) [jboss-msc-1.2.2.Final.jar:1.2.2.Final]
    at java.util.concurrent.ThreadPoolExecutor.runWorker(Unknown Source) [rt.jar:1.8.0_45]
    at java.util.concurrent.ThreadPoolExecutor$Worker.run(Unknown Source) [rt.jar:1.8.0_45]
    at java.lang.Thread.run(Unknown Source) [rt.jar:1.8.0_45]
Caused by: java.lang.RuntimeException: JBAS018757: Error getting reflective information for class com.mui.servlet.ItsServlet with ClassLoader ModuleClassLoader for Module "deployment.ItsProjectEAR.ear.ItsProjectWEB.war:main" from Service Module Loader
    at org.jboss.as.server.deployment.reflect.DeploymentReflectionIndex.getClassIndex(DeploymentReflectionIndex.java:72) [wildfly-server-8.2.1.Final.jar:8.2.1.Final]
    at org.jboss.as.ee.metadata.MethodAnnotationAggregator.runtimeAnnotationInformation(MethodAnnotationAggregator.java:58)
    at org.jboss.as.ee.component.deployers.InterceptorAnnotationProcessor.handleAnnotations(InterceptorAnnotationProcessor.java:107)
    at org.jboss.as.ee.component.deployers.InterceptorAnnotationProcessor.processComponentConfig(InterceptorAnnotationProcessor.java:92)
    at org.jboss.as.ee.component.deployers.InterceptorAnnotationProcessor.deploy(InterceptorAnnotationProcessor.java:77)
    at org.jboss.as.server.deployment.DeploymentUnitPhaseService.start(DeploymentUnitPhaseService.java:159) [wildfly-server-8.2.1.Final.jar:8.2.1.Final]
    ... 5 more
Caused by: java.lang.NoClassDefFoundError: Lcom/mui/cdi/ItsCart;
    at java.lang.Class.getDeclaredFields0(Native Method) [rt.jar:1.8.0_45]
    at java.lang.Class.privateGetDeclaredFields(Unknown Source) [rt.jar:1.8.0_45]
    at java.lang.Class.getDeclaredFields(Unknown Source) [rt.jar:1.8.0_45]
    at org.jboss.as.server.deployment.reflect.ClassReflectionIndex.<init>(ClassReflectionIndex.java:57) [wildfly-server-8.2.1.Final.jar:8.2.1.Final]
    at org.jboss.as.server.deployment.reflect.DeploymentReflectionIndex.getClassIndex(DeploymentReflectionIndex.java:68) [wildfly-server-8.2.1.Final.jar:8.2.1.Final]
    ... 10 more
Caused by: java.lang.ClassNotFoundException: com.mui.cdi.ItsCart from [Module "deployment.ItsProjectEAR.ear.ItsProjectWEB.war:main" from Service Module Loader]
    at org.jboss.modules.ModuleClassLoader.findClass(ModuleClassLoader.java:213) [jboss-modules.jar:1.3.3.Final]
    at org.jboss.modules.ConcurrentClassLoader.performLoadClassUnchecked(ConcurrentClassLoader.java:459) [jboss-modules.jar:1.3.3.Final]
    at org.jboss.modules.ConcurrentClassLoader.performLoadClassChecked(ConcurrentClassLoader.java:408) [jboss-modules.jar:1.3.3.Final]
    at org.jboss.modules.ConcurrentClassLoader.performLoadClass(ConcurrentClassLoader.java:389) [jboss-modules.jar:1.3.3.Final]
    at org.jboss.modules.ConcurrentClassLoader.loadClass(ConcurrentClassLoader.java:134) [jboss-modules.jar:1.3.3.Final]
    ... 15 more

13:22:46,232 ERROR [org.jboss.as.controller.management-operation] (DeploymentScanner-threads - 1) JBAS014613: Operation ("full-replace-deployment") failed - address: ([]) - failure description: {
    "JBAS014671: Failed services" => {"jboss.deployment.subunit.\"ItsProjectEAR.ear\".\"ItsProjectWEB.war\".POST_MODULE" => "org.jboss.msc.service.StartException in service jboss.deployment.subunit.\"ItsProjectEAR.ear\".\"ItsProjectWEB.war\".POST_MODULE: JBAS018733: Failed to process phase POST_MODULE of subdeployment \"ItsProjectWEB.war\" of deployment \"ItsProjectEAR.ear\"
    Caused by: java.lang.RuntimeException: JBAS018757: Error getting reflective information for class com.mui.servlet.ItsServlet with ClassLoader ModuleClassLoader for Module \"deployment.ItsProjectEAR.ear.ItsProjectWEB.war:main\" from Service Module Loader
    Caused by: java.lang.NoClassDefFoundError: Lcom/mui/cdi/ItsCart;
    Caused by: java.lang.ClassNotFoundException: com.mui.cdi.ItsCart from [Module \"deployment.ItsProjectEAR.ear.ItsProjectWEB.war:main\" from Service Module Loader]"},
    "JBAS014771: Services with missing/unavailable dependencies" => [
        "jboss.deployment.unit.\"ItsProjectEAR.ear\".weld.weldClassIntrospector is missing [jboss.deployment.unit.\"ItsProjectEAR.ear\".beanmanager]",
        "jboss.deployment.subunit.\"ItsProjectEAR.ear\".\"ItsProjectCDI.war\".weld.weldClassIntrospector is missing [jboss.deployment.subunit.\"ItsProjectEAR.ear\".\"ItsProjectCDI.war\".beanmanager]"
    ]
}
13:22:46,312 INFO  [org.jboss.as.server] (DeploymentScanner-threads - 1) JBAS018565: Replaced deployment "ItsProjectEAR.ear" with deployment "ItsProjectEAR.ear"
13:22:46,315 INFO  [org.jboss.as.controller] (DeploymentScanner-threads - 1) JBAS014774: Service status report
JBAS014777:   Services which failed to start:      service jboss.deployment.subunit."ItsProjectEAR.ear"."ItsProjectWEB.war".POST_MODULE: org.jboss.msc.service.StartException in service jboss.deployment.subunit."ItsProjectEAR.ear"."ItsProjectWEB.war".POST_MODULE: JBAS018733: Failed to process phase POST_MODULE of subdeployment "ItsProjectWEB.war" of deployment "ItsProjectEAR.ear"
      service jboss.deployment.subunit."ItsProjectEAR.ear"."ItsProjectWEB.war".POST_MODULE

I'm not being able to understand what's happening here, the new project is identical to the old one, but injection won't succeed. What am I doing wrong?

Luigi Cortese
  • 10,841
  • 6
  • 37
  • 48
  • I would start by checking your generated EAR. In general, EAR deployment would be discouraged, too much over head. Its possible that you don't have the modules declared indicating which is where. – John Ament Mar 11 '16 at 12:56
  • @JohnAment What do you mean with "EAR deployment would be discouraged"? When working with CDI? In general? – Luigi Cortese Mar 11 '16 at 12:57
  • According to the log information *Processing weld deployment ItsProjectCDI.war*, Eclipse is assuming that your `ItsProjectCDI` is a web application while in fact it is not. Since you've separated it from your EAR project, I'm assuming that you'd like it to behave as a library (a JAR). Therefore, you could start by changing `ItsProjectCDI` to a simple Java project and then add it to the build path of your EAR project. – António Ribeiro Mar 11 '16 at 12:58
  • @aribeiro you're definitely right, I just came to this conclusion too and I'm testing it. I probably messed a bit too much and I'm getting confused... – Luigi Cortese Mar 11 '16 at 13:00
  • Then, when you finish, let us know the outcome of your tests. – António Ribeiro Mar 11 '16 at 13:08
  • @aribeiro created a simple java project, added as EAR library, EAR compiles and deploy, but when executing a nullpointer exception is thrown for the bean. Still working on it... – Luigi Cortese Mar 11 '16 at 13:25
  • Don't forget to put the *beans.xml* inside your META-INF folder to trigger the CDI scan. – António Ribeiro Mar 11 '16 at 13:29
  • @aribeiro my god, that's so frustrating... I did put the *beans.xml* file in the project but it's not working. I'm thinking about posting another question if I can't manage to go any further... – Luigi Cortese Mar 11 '16 at 13:37
  • 1
    Have you added the jar to the classpath? – The Bitman Mar 11 '16 at 13:49
  • 1
    @TheBitman that was the *last* thing I was missing! I thought I had done it, but done it again and now it works! – Luigi Cortese Mar 11 '16 at 13:59
  • 1
    Ahahaha finally! :D @LuigiCortese, perhaps you could post an answer describing the steps you took in order to solve your problem and mark it as accepted. – António Ribeiro Mar 11 '16 at 13:59
  • @aribeiro If you want to answer I'll be happy to upvote, otherwise I'll post an answer myself in a while, describing the whole process with screenshots – Luigi Cortese Mar 11 '16 at 14:00
  • Thanks for your upvote for the solution (almost) – The Bitman Mar 11 '16 at 14:07
  • @Balus you're right, I got confused when I read in the spec (ch. 12 - packaging and deployment) that a bean archive could be either a war or a jar. I definitely misunderstood that part, I'll answer my own question as soon as I have some free time – Luigi Cortese Mar 11 '16 at 15:44
  • @BalusC no well, comments here helped me a lot, nothing more needed. I just thought it could be a good idea posting a detailed answer, since comments are not the easiest to read. If you think that's inappropriate I won't do it though – Luigi Cortese Mar 11 '16 at 15:46
  • @BalusC an the end of the day my problem was a trivial one, but imho it's not related with [this](http://stackoverflow.com/questions/30046979/splitting-up-shared-code-and-web-xml-from-war-project-to-common-jar-project) at all, there user's need was to *"refactor the common part of [his] web.xml to a common project, and let all the application-specific web.xml's extend the common web.xml."*, my problem was building a jar cdi bean archive, those 3 answers wouldn't have helped to solve it – Luigi Cortese Mar 11 '16 at 15:59
  • Let us [continue this discussion in chat](http://chat.stackoverflow.com/rooms/106033/discussion-between-luigi-cortese-and-balusc). – Luigi Cortese Mar 11 '16 at 16:02

0 Answers0