12 Replies Latest reply on May 3, 2006 1:45 PM by rpiaggio

    @EJB annotation not working in Java Bean

    adver11

      In EJB 3.0 RC5
      javabean code:

      public class Examples {
       @EJB
       MyStateless mystateless;
      
       public void process() {
       System.out.println(mystateless.getString());
       }
      }


      another java bean code:
      ...........
       Examples es = new Examples();
       es.process();
      ...........


      another EJB code:
      @Stateless
      @Local(TestEjb.class)
      public class TestEjbBean implements TestEjb {
       ........
      
       public dosomething() {
       Examples es = new Examples();
       es.process();
       }
      
       ........
      }


      I found @EJB in the class Examples, not working. mystateless is null.
      Can uesing @EJB in java bean?

        • 1. Re: @EJB annotation not working in Java Bean
          asack

           

          "adver11" wrote:
          In EJB 3.0 RC5
          javabean code:
          public class Examples {
           @EJB
           MyStateless mystateless;
          
           public void process() {
           System.out.println(mystateless.getString());
           }
          }


          another java bean code:
          ...........
           Examples es = new Examples();
           es.process();
          ...........


          another EJB code:
          @Stateless
          @Local(TestEjb.class)
          public class TestEjbBean implements TestEjb {
           ........
          
           public dosomething() {
           Examples es = new Examples();
           es.process();
           }
          
           ........
          }


          I found @EJB in the class Examples, not working. mystateless is null.
          Can uesing @EJB in java bean?


          Works for me. How is @EJB returning nulll? If your module is really getting deployed by the EJB3 deployer than it will tell you (i.e. barf with an exception) if it can't find an instance of MyStateless. I suspect that your module is not really getting deployed (make sure MyStateless is in your JNDI namespce by using the JMX-Console).

          • 2. Re: @EJB annotation not working in Java Bean
            adver11

            thank asack.I found my problem.

            • 3. Re: @EJB annotation not working in Java Bean
              adver11

              but, I have another problem.
              Stateless EJB code:

              @Depends({
               "jmx.service.wsmq:service=WSMQ_QXL",
               "jmx.service.wsmq:service=WSMQQueueConnectionFactory"
              })
              public @Stateless class TestEJBBean implements TestEJB {
               private static EJBUtil pu = EJBUtil.getInstance();
              
               @Resource(mappedName = "WSMQQueueConnectionFactory")
               private QueueConnectionFactory qf;
              
               @Resource(mappedName = "wsmq/QXL")
               private Queue qxl;
              
               public void dosomthing() {
               if(qf != null)
               System.out.println("EJB qf ok");
               else
               System.out.println("EJB qf null");
               if(qxl != null)
               System.out.println("EJB qxl ok");
               else
               System.out.println("EJB qxl null");
               pu.process();
               }
              }

              java bean code:
              public class EJBUtil {
               private static EJBUtil instance;
              
               @Resource(mappedName = "WSMQQueueConnectionFactory")
               private QueueConnectionFactory qf;
              
               @Resource(mappedName = "wsmq/QXL")
               private Queue qxl;
              
               public synchronized void process() {
               if(qf != null)
               System.out.println("qf ok");
               else
               System.out.println("qf null");
               if(qxl != null)
               System.out.println("qxl ok");
               else
               System.out.println("qxl null");
               }
              
               public static synchronized EJBUtil getInstance() {
               if(instance == null)
               instance = new EJBUtil();
               return instance;
               }
              }

              or like this:
              public class EJBUtil {
               private static EJBUtil instance;
              
               @EJB(mappedName = "WSMQQueueConnectionFactory")
               private QueueConnectionFactory qf;
              
               @EJB(mappedName = "wsmq/QXL")
               private Queue qxl;
              
               public synchronized void process() {
               if(qf != null)
               System.out.println("qf ok");
               else
               System.out.println("qf null");
               if(qxl != null)
               System.out.println("qxl ok");
               else
               System.out.println("qxl null");
               }
              
               public static synchronized EJBUtil getInstance() {
               if(instance == null)
               instance = new EJBUtil();
               return instance;
               }
              }


              call EJB TestEJB's method "dosomething", the result id:
              23:14:50,014 INFO [STDOUT] EJB qf ok
              23:14:50,014 INFO [STDOUT] EJB qxl ok
              23:14:50,014 INFO [STDOUT] qf null
              23:14:50,014 INFO [STDOUT] qxl null

              it means @EJB @Resource about mappedName not working in java bean.

              • 4. Re: @EJB annotation not working in Java Bean
                adver11

                I take another test:

                @Depends({
                 "jmx.service.wsmq:service=WSMQ_QXL",
                 "jmx.service.wsmq:service=WSMQQueueConnectionFactory"
                })
                public @Stateless class TestEJBBean implements TestEJB {
                 private static EJBUtil pu = EJBUtil.getInstance();
                
                 @Resource(mappedName = "WSMQQueueConnectionFactory")
                 private QueueConnectionFactory qf;
                
                 @Resource(mappedName = "wsmq/QXL")
                 private Queue qxl;
                
                 @Resource
                 private SessionContext ctx;
                
                 @PersistenceContext(unitName = "fids")
                 protected EntityManager em;
                
                 public void dosomthing() {
                 if(qf != null)
                 System.out.println("EJB qf ok");
                 else
                 System.out.println("EJB qf null");
                 if(qxl != null)
                 System.out.println("EJB qxl ok");
                 else
                 System.out.println("EJB qxl null");
                 if(ctx != null)
                 System.out.println("EJB ctx ok");
                 else
                 System.out.println("EJB ctx null");
                 if(em != null)
                 System.out.println("EJB em ok");
                 else
                 System.out.println("EJB em null");
                 pu.process();
                 }
                }

                public class EJBUtil {
                 private static EJBUtil instance;
                
                 @EJB(mappedName = "WSMQQueueConnectionFactory")
                 private QueueConnectionFactory qf;
                
                 @EJB(mappedName = "wsmq/QXL")
                 private Queue qxl;
                
                 @Resource
                 private SessionContext ctx;
                
                 @PersistenceContext(unitName = "fids")
                 private EntityManager em;
                
                 public synchronized void process() {
                 if(qf != null)
                 System.out.println("qf ok");
                 else
                 System.out.println("qf null");
                 if(qxl != null)
                 System.out.println("qxl ok");
                 else
                 System.out.println("qxl null");
                 if(ctx != null)
                 System.out.println("ctx ok");
                 else
                 System.out.println("ctx null");
                 if(em != null)
                 System.out.println("em ok");
                 else
                 System.out.println("em null");
                 }
                
                 public static synchronized EJBUtil getInstance() {
                 if(instance == null)
                 instance = new EJBUtil();
                 return instance;
                 }
                }


                Class EJBUtil and TestEJBBean in one deploying file (TestEJB.ejb3).
                result is:
                23:39:16,743 INFO [STDOUT] EJB qf ok
                23:39:16,743 INFO [STDOUT] EJB qxl ok
                23:39:16,743 INFO [STDOUT] EJB ctx ok
                23:39:16,743 INFO [STDOUT] EJB em ok
                23:39:16,743 INFO [STDOUT] qf null
                23:39:16,743 INFO [STDOUT] qxl null
                23:39:16,743 INFO [STDOUT] ctx null
                23:39:16,743 INFO [STDOUT] em null


                so @EJB,@Resource,@PersistenceContext can't working in java bean.

                • 5. Re: @EJB annotation not working in Java Bean
                  asack

                  Hold on. @EJB works fromwithin another EJB not just a regular JavaBean. The @EJB annotations are picked up by the EJB3 deployer and processed accordingly (AOP based).

                  If you are using @EJB from a regular JavaBean, that's not going to work and from my understaning never intended to work that way. If you want access to an EJB from a non-managed object, then use a JNDI style lookup.

                  • 6. Re: @EJB annotation not working in Java Bean
                    xinu

                     

                    "adver11" wrote:
                    thank asack.I found my problem.


                    I'm having similar problems--I can't get the @EJB annotation to work at all, but everything is fine when I lookup() myself.

                    adver11, what was your problem that you found?

                    • 7. javax.annotation.EJB vs javax.ejb.EJB (was Re: @EJB annotati
                      xinu

                       

                      "xinu" wrote:
                      "adver11" wrote:
                      thank asack.I found my problem.


                      I'm having similar problems--I can't get the @EJB annotation to work at all, but everything is fine when I lookup() myself.

                      adver11, what was your problem that you found?


                      Never mind, I figured out my problem too.

                      My problem was that I was using javax.ejb.EJB, which presumely superceded javax.annotation.EJB in the specs and Sun's docs. Or vice versa--Sun's docs all say javax.ejb.EJB, but I'm rather surprised I can't find any references to it changing packages.

                      Using javax.annotation.EJB instead does work for me, just as it does in the (JBoss) examples.

                      • 8. Re: @EJB annotation not working in Java Bean
                        yakamax

                        I can't import javax.ejb.EJB;
                        but i can import import javax.annotation.EJB;

                        Where can I find javax.ejb.EJB; please?

                        • 9. Re: javax.annotation.EJB vs javax.ejb.EJB (was Re: @EJB anno
                          rpiaggio

                           

                          "xinu" wrote:

                          My problem was that I was using javax.ejb.EJB, which presumely superceded javax.annotation.EJB in the specs and Sun's docs. Or vice versa--Sun's docs all say javax.ejb.EJB, but I'm rather surprised I can't find any references to it changing packages.

                          Using javax.annotation.EJB instead does work for me, just as it does in the (JBoss) examples.


                          I am having the same problem. Cannot get @EJB to work at all. Deployment works fine and if I do a .lookup also works. Any hints?

                          xinu: What examples are you referring to?

                          Thanks!

                          • 10. Re: javax.annotation.EJB vs javax.ejb.EJB (was Re: @EJB anno
                            rpiaggio

                             

                            "rpiaggio" wrote:

                            I am having the same problem. Cannot get @EJB to work at all. Deployment works fine and if I do a .lookup also works. Any hints?


                            Actually, after toying a bit more, I got it to work in some cases.

                            But there's a case where it doesn't work. I am working on an application that I deply as a EAR with a JAR and WAR inside. The WAR has a controller bean that uses session beans from the JAR. What doesn't work is an @EJB annotiation in the WAR that refers to a bean in the JAR.

                            Actually: should this work? Maybe it is correct that it doesn't work...

                            • 11. Re: javax.annotation.EJB vs javax.ejb.EJB (was Re: @EJB anno
                              asack

                               

                              "rpiaggio" wrote:
                              "rpiaggio" wrote:

                              I am having the same problem. Cannot get @EJB to work at all. Deployment works fine and if I do a .lookup also works. Any hints?


                              Actually, after toying a bit more, I got it to work in some cases.

                              But there's a case where it doesn't work. I am working on an application that I deply as a EAR with a JAR and WAR inside. The WAR has a controller bean that uses session beans from the JAR. What doesn't work is an @EJB annotiation in the WAR that refers to a bean in the JAR.

                              Actually: should this work? Maybe it is correct that it doesn't work...


                              As I stated before and in the FAQ/WIKI, @EJB works within EJB modules, not outside them, i.e. dependency injection from a WAR. The good news is that Servlet spec 2.5 (I think that's the version, i.e. the next one), will add depedency injection notation to WARs but the current release of JBoss doesn't support that.

                              • 12. Re: javax.annotation.EJB vs javax.ejb.EJB (was Re: @EJB anno
                                rpiaggio

                                 

                                "asack" wrote:
                                As I stated before and in the FAQ/WIKI, @EJB works within EJB modules, not outside them, i.e. dependency injection from a WAR. The good news is that Servlet spec 2.5 (I think that's the version, i.e. the next one), will add depedency injection notation to WARs but the current release of JBoss doesn't support that.


                                Cool. After taking a look at Servlet 2.5, I anxiously wait for it.

                                So, for the moment I have to use lookup, which raises another important question. JBoss seems to bind session beans to the name "EARName/BeanName/[remote|local]". However, other application servers use different conventions (some just use "BeanName").

                                - Is there a way to access the "EARName" from within the application? (This is useful if I decide to reuse a bean in another application).
                                - Is there a way to program an application that is portable across servers with regards to JNDI naming conventions?
                                - Does the EJB specification specify a naming convention? (I guess not).

                                Thank you!