7 Replies Latest reply on Aug 14, 2012 10:26 AM by cfang

    @Resource gets null, but ctx.lookup works jboss 7.1.1

    roko98

      Hi all

       

      I google this, but what seems to work for others doesn't work for me.  I'm triying to send a JMS message from a RESTEasy managed bean, like this:

       

      {code}@Path("/MyRESTApplication")

      @ManagedBean

      public class HelloWorldResource {

          @Resource(lookup = "java:/JmsXA")

          ConnectionFactory connectionFactory;

       

          @Resource(lookup = "java:/jms/queue/test")

          Destination receiverQueue;

       

          @GET()

          @Produces("text/plain")

          public String sayHello() {

              sendEmail();

              sendMessage();

              return "Hello World!";

          }{code}

       

      But connectionFactory and receiverQueue are allways null.  If I do this:

       

      {code}InitialContext ctx;

      try {

          ctx = new InitialContext();

          connectionFactory = (ConnectionFactory) ctx.lookup("java:/JmsXA");

      } catch (NamingException e) {

          // TODO Auto-generated catch block

          e.printStackTrace();

      }{code}

       

      Everithing works as spected.  With @ManagedBean, resource injection is suppose to work right ? what I'm missing here ?

        • 1. Re: @Resource gets null, but ctx.lookup works jboss 7.1.1
          sfcoy

          Here's a couple of things to check:

          1. Make sure you're using javax.annotation.ManagedBean and not javax.faces.bean.ManagedBean.
          2. Make sure your application has a beans.xml file in the appropriate place.
          • 2. Re: @Resource gets null, but ctx.lookup works jboss 7.1.1
            roko98

            Thank you for your quick answer. Here are the imports:

             

            {code}import javax.annotation.ManagedBean;

            import javax.annotation.Resource;

            import javax.ws.rs.GET;

            import javax.ws.rs.Path;

            import javax.ws.rs.Produces;{code}

             

            And I'm using a empty WEB-INF/beans.xml iside the war, like this:

             

            {code}<?xml version="1.0"?>

            <beans xmlns="http://java.sun.com/xml/ns/javaee"

            xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/beans_1_0.xsd"/>{code}

             

            Here is the log output when I redeploy my files:

             

            {code}20:07:11,539 INFO  [org.jboss.as.server.deployment] (MSC service thread 1-4) JBAS015877: Stopped deployment survey-ejb.jar in 69ms

            20:07:11,540 INFO  [org.jboss.as.server.deployment] (MSC service thread 1-10) JBAS015876: Starting deployment of "survey-ejb.jar"

            20:07:11,552 INFO  [org.jboss.weld.deployer] (MSC service thread 1-1) JBAS016009: Stopping weld service for deployment survey-war.war

            20:07:11,569 INFO  [org.jboss.as.server.deployment] (MSC service thread 1-9) JBAS015877: Stopped deployment survey-war.war in 28ms

            20:07:11,569 INFO  [org.jboss.as.server.deployment] (MSC service thread 1-7) JBAS015876: Starting deployment of "survey-war.war"

            20:07:11,593 INFO  [org.jboss.as.ejb3] (MSC service thread 1-9) JBAS014142: Started message driven bean 'HelloWorldMessageBean' with 'hornetq-ra' resource adapter

            20:07:11,607 INFO  [org.jboss.weld.deployer] (MSC service thread 1-12) JBAS016002: Processing weld deployment survey-war.war

            20:07:11,628 INFO  [org.jboss.weld.deployer] (MSC service thread 1-5) JBAS016005: Starting Services for CDI deployment: survey-war.war

            20:07:11,638 INFO  [org.jboss.weld.deployer] (MSC service thread 1-2) JBAS016008: Starting weld service for deployment survey-war.war

            20:07:11,701 INFO  [org.jboss.resteasy.cdi.CdiInjectorFactory] (MSC service thread 1-10) Found BeanManager at java:comp/BeanManager

            20:07:11,704 INFO  [org.jboss.resteasy.spi.ResteasyDeployment] (MSC service thread 1-10) Deploying javax.ws.rs.core.Application: class org.jboss.samples.rs.webservices.MyRESTApplication$Proxy$_$$_WeldClientProxy

            20:07:11,704 INFO  [org.jboss.resteasy.spi.ResteasyDeployment] (MSC service thread 1-10) Adding singleton resource org.jboss.samples.rs.webservices.HelloWorldResource from Application javax.ws.rs.core.Application

            20:07:11,710 INFO  [org.jboss.web] (MSC service thread 1-10) JBAS018210: Registering web context: /survey-war

            20:07:11,796 INFO  [org.jboss.as.server] (DeploymentScanner-threads - 1) JBAS018565: Replaced deployment "survey-war.war" with deployment "survey-war.war"

            20:07:11,797 INFO  [org.jboss.as.server] (DeploymentScanner-threads - 1) JBAS018565: Replaced deployment "survey-ejb.jar" with deployment "survey-ejb.jar"{code}

             

            Do I need to define the class in the beans.xml ?

            • 3. Re: @Resource gets null, but ctx.lookup works jboss 7.1.1
              nickarls

              Haven't tried but are you sure about the lookup attribute? Tried if there are name/mappedName attributes available?

              • 4. Re: @Resource gets null, but ctx.lookup works jboss 7.1.1
                sfcoy

                I just tested this in a JBoss AS 7.1.1 standalone configuration using:

                 

                {code:java}@Resource(lookup="java:jboss/mail/Default")

                private Session mailSession;{code}

                 

                and it worked perfectly (in the jboss-as-helloworld-rs quickstart).

                 

                Maybe you've polluted your configuration somehow?

                • 5. Re: @Resource gets null, but ctx.lookup works jboss 7.1.1
                  sfcoy

                  And using standalone-full.xml with:

                   

                   

                  {code:java}

                  @Resource(lookup="java:/JmsXA")
                  private ConnectionFactory connectionFactory;

                   


                  @Resource(lookup = "java:/queue/test")
                  private Destination receiverQueue;

                  {code}

                   

                   

                  and it works fine too.

                   

                  Also, the @ManagedBean seems to be redundant.

                  • 6. Re: @Resource gets null, but ctx.lookup works jboss 7.1.1
                    roko98

                    You were right! .. the quickstart works...

                     

                    But the same class inside my war doesn't.  Same error.  I generate the war and ejb-jar using JBoss Developer Studio 5.0.1.  In the case of the war file, JDS generates the war in a different way.  It builds (and uses) a class that inherits from javax.ws.rs.core.Application and builds a different web.xml file, with more options.  If I simplify the project that JDS generates, just like the quicksample, everything works fine.

                     

                    I'm just starting with JBoss7 - JDS, so I don't  know the reason why it doesn't works out of the box.  If this is a unexpected behavior and I can help in some way,  just let me know.

                    • 7. Re: @Resource gets null, but ctx.lookup works jboss 7.1.1
                      cfang

                      Can you post the generated web.xml that didn't work?  I'm curious to see what's in there.  It could have an old version of the webapp schema so injections are not processed, or it could have a metadata-complete=true so annotations are skipped.