7 Replies Latest reply on Jul 8, 2011 8:00 AM by jaikiran

    Struggling with injecting singleton in 6.0

    rhinmass

      Hello,

       

      I am having trouble figuring out how to inject a singleton into another class - in this case a servlet. 

       

      My singleton looks like this:

       

      package com.robin.ejb

       

      import javax.ejb.Singleton;

      import javax.ejb.Startup;

       

      @Singleton

      @Startup

      public class HelloWorld

      {

          public HelloWorld()

          {

              System.out.println("constructing HelloWorld");

          }  

       

          public String sayHello() {

              return "Hello World !!!";

          }

       

      }

       

       

      My servlet looks like this:

       

      import ...

       

      import com.robin.ejb.HelloWorld;

       

      @WebServlet(name="helloServlet", urlPatterns={"/hello"},

              initParams={ @WebInitParam(name="nameParam", value="world")})

       

      public class HelloServlet extends HttpServlet {

       

          private static final long serialVersionUID = 1L;

       

          @Inject

          private HelloWorld hw;

       

          @Override

          protected void doGet(HttpServletRequest request, HttpServletResponse response)

                  throws ServletException, IOException {

       

              PrintWriter out = response.getWriter();

              String nameParam = request.getParameter("nameParam");

              if (nameParam == null)

                  nameParam = getServletConfig().getInitParameter("nameParam");

              out.println("hello " + nameParam + " " + hw.sayHello());

              out.flush();

              out.close();

          }

       

      }

       

      When I run this in JBoss AS 6.0.0.Final I get the following errors:

       

       

      2011-04-01 10:23:43,299 ERROR [org.jboss.kernel.plugins.dependency.AbstractKernelController] (Thread-2) Error installing to Start: name=vfs:///C:/jboss/jboss-6.0.0.Final/server/rbh/deploy/myapp-0.0.1.ear_WeldBootstrapBean state=Create: org.jboss.weld.exceptions.DeploymentException: WELD-001409 Ambiguous dependencies for type [HelloWorld] with qualifiers [@Default] at injection point [[field] @Inject private com.robin.webapp.HelloServlet.hw]. Possible dependencies [[Session bean [class com.robin.ejb.HelloWorld with qualifiers [@Any @Default]; local interfaces are [HelloWorld], Session bean [class com.robin.ejb.HelloWorld with qualifiers [@Any @Default]; local interfaces are [HelloWorld]]]

          at org.jboss.weld.bootstrap.Validator.validateInjectionPoint(Validator.java:309) [:6.0.0.Final]

          at org.jboss.weld.bootstrap.Validator.validateBean(Validator.java:139) [:6.0.0.Final]

          at org.jboss.weld.bootstrap.Validator.validateRIBean(Validator.java:162) [:6.0.0.Final]

          at org.jboss.weld.bootstrap.Validator.validateBeans(Validator.java:377) [:6.0.0.Final]

          at org.jboss.weld.bootstrap.Validator.validateDeployment(Validator.java:363) [:6.0.0.Final]

          at org.jboss.weld.bootstrap.WeldBootstrap.validateBeans(WeldBootstrap.java:388) [:6.0.0.Final]

        • 1. Struggling with injecting singleton in 6.0
          rhinmass

          I have a new clue, but not a solution yet.  It seems that 2 instances of HelloWorld are being created.

           

          11:49:32,356 INFO  [STDOUT] constructing HelloWorld

          11:49:32,372 INFO  [org.jboss.ejb3.nointerface.impl.jndi.AbstractNoInterfaceViewBinder] Binding the following entry in Global JNDI for bean:HelloWorld

           

                  myapp-0.0.1/HelloWorld/no-interface -> EJB3.1 no-interface view

           

          11:49:32,372 INFO  [STDOUT] constructing HelloWorld

          11:49:32,372 INFO  [org.jboss.ejb3.nointerface.impl.jndi.AbstractNoInterfaceViewBinder] Binding the following entry in Global JNDI for bean:HelloWorld

           

                  myapp-0.0.1/HelloWorld/no-interface -> EJB3.1 no-interface view

           

          11:49:32,403 INFO  [org.apache.coyote.http11.Http11Protocol] Starting Coyote HTTP/1.1 on http-127.0.0.1-8080

          11:49:32,403 INFO  [org.apache.coyote.ajp.AjpProtocol] Starting Coyote AJP/1.3 on ajp-127.0.0.1-8009

          11:49:32,403 INFO  [org.jboss.bootstrap.impl.base.server.AbstractServer] JBossAS [6.0.0.Final "Neo"] Started in 34s:63ms

           

          Of course I only get this far if I remove the injection of HelloWorld from the servlet.  (I removed "@Startup" but it did not make a difference.)

          • 2. Struggling with injecting singleton in 6.0
            rhinmass

            OK, this is weird but it looks like two instances of my application are getting deployed:

             

            14:20:33,517 INFO  [Ejb3DependenciesDeployer] Encountered deployment AbstractVFSDeploymentContext@7390509{vfs:///C:/jboss/jboss-6.0.0.Final/server/rbh/deploy/myapp-0.0.1.ear/ejb-0.0.1.jar/}

            14:20:33,517 INFO  [Ejb3DependenciesDeployer] Encountered deployment AbstractVFSDeploymentContext@7390509{vfs:///C:/jboss/jboss-6.0.0.Final/server/rbh/deploy/myapp-0.0.1.ear/ejb-0.0.1.jar/}

            14:20:33,517 INFO  [Ejb3DependenciesDeployer] Encountered deployment AbstractVFSDeploymentContext@3192484{vfs:///C:/jboss/jboss-6.0.0.Final/server/rbh/deploy/myapp-0.0.1.ear/servlet-0.0.1.war/}

            14:20:33,517 INFO  [Ejb3DependenciesDeployer] Encountered deployment AbstractVFSDeploymentContext@3192484{vfs:///C:/jboss/jboss-6.0.0.Final/server/rbh/deploy/myapp-0.0.1.ear/servlet-0.0.1.war/}

             

            I am certain I don't have duplicate ears - infact you can see in the message that the full file-paths are identical.

             

            See also http://community.jboss.org/thread/164062?tstart=0

             

            Any thoughts?

            • 3. Struggling with injecting singleton in 6.0
              rhinmass

              I changed @Inject to @EJB in the servlet and it works now.  Can anyone explain to me the difference?

               

              I am still having problems, however.  I introduced a new stateless session bean to the ejb jar, and what was working no longer works.  I will start a new discussion for this problem.

              • 4. Re: Struggling with injecting singleton in 6.0
                miragpl

                I got similar problem.

                 

                10:54:49,582 INFO  [Ejb3DependenciesDeployer] Encountered deployment AbstractVFSDeploymentContext@24082605{vfs:///D:/dev/jboss-6.0.0.Final/server/myApp/deploy/myApp.ejb-1.0-SNAPSHOT.jar}

                10:54:49,582 INFO  [Ejb3DependenciesDeployer] Encountered deployment AbstractVFSDeploymentContext@24082605{vfs:///D:/dev/jboss-6.0.0.Final/server/myApp/deploy/myApp.ejb-1.0-SNAPSHOT.jar}

                 

                 

                In my case i got the problem only with the EJB module.Despite the error the application works fine (until till now..).

                Could anyone explain what's going on?

                 

                I'm using jboss-6-final and java 1.6.20.

                 

                Thanks in advance!

                • 5. Re: Struggling with injecting singleton in 6.0
                  jaikiran

                  M K wrote:

                   

                  I got similar problem.

                  Which problem? The INFO level logs?

                   

                   

                  M K wrote:

                   

                   

                   

                  10:54:49,582 INFO  [Ejb3DependenciesDeployer] Encountered deployment AbstractVFSDeploymentContext@24082605{vfs:///D:/dev/jboss-6.0.0.Final/server/myApp/deploy/myApp.ejb-1.0-SNAPSHOT.jar}

                  10:54:49,582 INFO  [Ejb3DependenciesDeployer] Encountered deployment AbstractVFSDeploymentContext@24082605{vfs:///D:/dev/jboss-6.0.0.Final/server/myApp/deploy/myApp.ejb-1.0-SNAPSHOT.jar}

                   

                   

                  • 6. Re: Struggling with injecting singleton in 6.0
                    miragpl

                    Yep, i know it's only a INFO but it's looks weird.

                    • 7. Re: Struggling with injecting singleton in 6.0
                      jaikiran

                      But unlike you say, it's not an error and it's no surprise that the application works fine even with that log.