6 Replies Latest reply on Nov 12, 2012 7:46 AM by mylos78

    Resource Injection in plain Java class fails

    mylos78

      Hi all !

      I'm having some issues when injecting JMS resources in my application. I have a JMS client Servlet where resource injection works like a charm:

       

      public class JMSServlet extends HttpServlet {

       

          @Resource(mappedName = "java:jboss/exported/jms/queue/mainQueue")

          private Queue queue;

       

          @Resource(mappedName = "java:/ConnectionFactory")

          private ConnectionFactory cf;

          . . . . .

      }

       

      Now I'd like to move all the JMS stuff in an Utility class, which just contains the same code as the Servlet:

       

      public class JMSUtility  {

       

          @Resource(mappedName = "java:jboss/exported/jms/queue/mainQueue")

          private Queue queue;

       

          @Resource(mappedName = "java:/ConnectionFactory")

          private ConnectionFactory cf;

          . . . . .

          public void sendMessage() {

               // NPE! Connection factory is null !

          }

      }

       

      Now if I try to invoke the JMSUtility from the Servlet with a simple

      new JMSUtility().sendMessage();

      I get a NPE because injections are null in the method sendMessage().

      Forgive my ignorance, but shouldn't that work the same either in a Servlet or in a Java class -provided that the Java class is running on the server ?

      Am I missing something, apparentely silly that I cannot see ? for completeness, I'm running this test against JBoss AS 7.1.1

      Thanks

      Mylos

        • 1. Re: Resource Injection in plain Java class fails
          nickarls

          Injection is not an automagic happening. The AS takes the servlet class and does all the injection for it (forcing it into the fields), that is why the "new" operator doesn't work with injection either.

          If you want to separate the stuff, you could use CDI by adding an empty beans.xml, move logic to the JMSUtility and do an @Inject JMSUtility util; in the servlet class.

          • 2. Re: Resource Injection in plain Java class fails
            sfcoy

            Alternatively, you could add an @Stateless annotation to JMSUtility and inject it into your servlet with @Inject or @EJB.

             

            JMS is a transactional resource, so this might be considered more appropriate than straight CDI.

             

            Either way, the thing to remember is that you only get injection when the bean lifecycle is managed by the container.

            1 of 1 people found this helpful
            • 3. Re: Resource Injection in plain Java class fails
              nickarls

              True, and no need bringing in new technologies if you're not using it for anything else and it can be done with the existing stack. Although I don't think it matters in this case since the methods being called would still be the transactional ones and not the helper methods directly.

              • 4. Re: Resource Injection in plain Java class fails
                mylos78

                Thank you very much for the kind replies (both correct I guess so I had to pick up just the first reply as the correct one :-) )

                Thats very clear, so it looks pretty the same as Spring container (where injections will not work if you are using the new operator for creating your beans).

                Out of curiosity, am I allowed to install as module the POJO class (in the modules folder), with a beans.xml file in the JAR file, and then @Inject the JMSUtility class in my applications ?

                Thanks & have a nice day

                • 5. Re: Resource Injection in plain Java class fails
                  nickarls

                  There is a high change the CDI approach wouldn't work in that case since the visiblity rules of the EE deployment is followed, per spec, and the JBoss module is alien to the WAR archive (even if class visiblity is established to the JBoss Modules thanks to the AS)

                  • 6. Re: Resource Injection in plain Java class fails
                    mylos78

                    Thanks. By the way I have just tried installing the Utility class as a module (without injections) but I get an error:

                     

                    JBAS011843: Failed instantiate InitialContextFactory org.jboss.naming.remote.client.InitialContextFactory from classloader ModuleClassLoader for Module "org.jboss.as.connector:main" from local module loader @d9660d (roots: C:\jboss-as-7.1.1.Final\modules)

                     

                    I will open another thread for this issue.

                    Regards

                    Mylos