1 2 Previous Next 16 Replies Latest reply on Oct 1, 2013 8:31 AM by sfcoy

    @EJB does not work from JSF 2.1 ManagedBean inside EAR in JBoss 7.2.0.Final

    mgrossmann

      Hi,

       

      i have an ear which contains a stateless session bean annotated with @Remote in an ejb-jar.

      Additionally i have a jsf 2.1 web application with some ManagedBeans that wants to Inject the above EJB with @EJB.

      This war is inside the above ear and besides the ejb-jar.

       

      In Jboss 7.1.2.Final this works fine but since 7.2 the Injection fails for some reason and i got a NullPointerException.

      the SLB is bound to JNDI during deployment correctly and is visible in the JNDIView

       

      Are there any major changes in classloading bahavior between 7.1.2 and 7.2 ?

      Do i have to add a manifest dependency to the ejb-jar in the war module?

       

      I'm a little bit confused since i do not want to make an InitialContext lookup when i'm inside the same ear

       

      Thanks for your help

       

      Regards, Matthias

        • 1. Re: @EJB does not work from JSF 2.1 ManagedBean inside EAR in JBoss 7.2.0.Final
          ssilvert

          Mi Matthias,

           

          Have you tried it on the latest version of WildFly?  If it is still broken on the latest build please create a jira and assign to me.

           

          Thanks,

           

          Stan

          • 2. Re: @EJB does not work from JSF 2.1 ManagedBean inside EAR in JBoss 7.2.0.Final
            ybxiang.china

            i have an ear which contains a stateless session bean annotated with @Remote in an ejb-jar.

            ~~~~~~~Please try @Local. since your JSF web app and your EJB app are in the same EAR.

             

             

            In Jboss 7.1.2.Final this works fine but since 7.2 the Injection fails for some reason and i got a NullPointerException.

            ~~~~~~~Very strange, I used 7.1.1, 7.1.2, 7.1.3, 7.2.0, all of them works well. Now I am using 7.2.0, everything is perfect.

            • 3. Re: @EJB does not work from JSF 2.1 ManagedBean inside EAR in JBoss 7.2.0.Final
              ybxiang.china

              Have you tried it on the latest version of WildFly?

              ~~~~~~I will not try the alpha or beta version if it is not necessary. After all, our product version is 7.2.0 and we can not use non-final version.

              • 4. Re: @EJB does not work from JSF 2.1 ManagedBean inside EAR in JBoss 7.2.0.Final
                ybxiang.china

                Would you please post some of your codes and the full exception stack?

                • 5. Re: @EJB does not work from JSF 2.1 ManagedBean inside EAR in JBoss 7.2.0.Final
                  mgrossmann

                  The strange thing is, there is no exception stack trace. The Nullpointer occurs if a method should be called on the injected bean.

                  But there is no classloading, dependency or injection error during deployment or runtime

                  the injected variable is just null.

                   

                  My EJB lokks like the following:

                   

                  @Stateless

                  @TransactionAttribute(TransactionAttributeType.REQUIRES_NEW)

                  @Remote( { ConfigBaseRemote.class } )

                  @EJB(name = "java:global/ConfigBase", beanInterface = ConfigBaseRemote.class)

                  public class ConfigBaseBean implements ConfigBaseRemote {

                   

                   

                  And my ManagedBean

                   

                  @ManagedBean(name = "comp")

                  @RequestScoped

                  public class Component implements Serializable {  

                    

                      private static final long serialVersionUID = -3996744800905996092L;

                    

                      @EJB

                      private ConfigBaseRemote configbase;

                   

                   

                  The structure in the ear is this:

                   

                  ear

                  |

                  |___ejb-jar (includes ConfigBaseBean)

                  |

                  |___api-jar (includes ConfigBaseRemote)

                  |

                  |___jsf-war

                   

                   

                  I need a Remote Interface because the Bean is also needed by remote clients via jndi-lookup which works in this case.

                   

                  I do not think that this is a bug, i think it could be a missing module or dependency configuration

                   

                  P.S.:

                   

                  i also tried with

                   

                      @EJB(lookup="java:global/ConfigBase")

                      private ConfigBaseRemote configbase;

                     

                  and

                   

                     @EJB(name="java:global/ConfigBase")

                      private ConfigBaseRemote configbase;

                   

                  with the same result.

                  • 6. Re: @EJB does not work from JSF 2.1 ManagedBean inside EAR in JBoss 7.2.0.Final
                    mgrossmann

                    Ok, there is definitly somthing going wrong.

                     

                    I changed my structure a little bit to the following:

                     

                    I created a Stateless Local ConnectorBean inside the war itself and tried to inject this to the ManagedBean

                     

                    @ManagedBean(name = "comp")

                    @RequestScoped

                    public class Component implements Serializable {

                           

                        private static final long serialVersionUID = -3996744800905996092L;

                       

                        @EJB

                        private ConfigBaseConnectorLocal cbConn;

                     

                     

                    The LocalBean is simple

                     

                    @Stateless

                    @Local

                    public class ConfigBaseConnector implements ConfigBaseConnectorLocal{

                       

                        @EJB

                        ConfigBaseRemote configbase;

                       

                        public ConfigBaseRemote getConfigBase() {

                              return configbase;

                        }

                     

                    But even this injection fails. It seems that the @EJB Annotation in the ManagedBean is ignored completely.

                     

                    With the change from 7.1.2 to 7.2 i had to add a module dependency to jsf-api and jsf-impl which was not necessary in 7.1.2

                     

                    Do i have to add a dependency to some ejb stuff as well?

                    • 7. Re: @EJB does not work from JSF 2.1 ManagedBean inside EAR in JBoss 7.2.0.Final
                      ybxiang.china

                      @EJB(name = "java:global/ConfigBase", beanInterface = ConfigBaseRemote.class)

                       

                      ~~~~I think this is the root cause! Please do NOT set jndiName through @EJB annotaiton unless you know it well.

                                   JBoss AS 7's default generated jndiName is different with that of JBoss AS 5!

                       

                      Please do things step by step, you should start from the simplest example. (You Example is too complicated.)

                       

                       

                       

                      Please try my codes(I have tested it in jboss as 7.1.3, 7.2.0) and post your result here:

                      ****************************************************************************************************

                      public interface IUserSession {

                          public User getUserByIdFromDB(Long userId);

                      }

                       

                       

                      @Stateless

                      @Local(IUserSession.class)

                      public class UserSession implements IUserSession{

                          @PersistenceContext()

                          private EntityManager em;

                       

                          @PermitAll()

                          public User getUserByIdFromDB(Long userId){

                              return em.find(User.class, userId);

                          }

                      }

                       

                       

                      @ManagedBean

                      @RequestScoped

                      public class UserRegisterMBean {

                          @EJB

                          private IUserSession userSession;

                       

                          public String create(){

                              //debug here to test if userSession is null

                              userSession.getUserByIdFromDB(1L);

                          }

                      }

                      ****************************************************************************************************

                      • 8. Re: @EJB does not work from JSF 2.1 ManagedBean inside EAR in JBoss 7.2.0.Final
                        ybxiang.china

                        1.

                        By default the jboss as 7 will generate many jndiName for both local and remote calling, if you do NOT use @EJB on your EJB.

                        I think your jndi name "java:global/ConfigBase" does NOT match any generated one.

                         

                        2.

                        If you dislike the generated one, you should find out if your jndi name "java:global/ConfigBase" is registered successfully from server.log.

                         

                        So, If you stick to your jndi name (for example: migrate from jboss as 5 to jboss as 7), please post your clean and full server.log here.

                         

                        I hate migration! I would rather rewrite all of them.

                        • 9. Re: @EJB does not work from JSF 2.1 ManagedBean inside EAR in JBoss 7.2.0.Final
                          mgrossmann

                          The manually set JNDI Name is purposed. But the binding works, when i make a jndi lookup with InitialContext

                           

                          ConfigBaseRemote cb = null;

                                  try {

                                      InitialContext ctx = new InitialContext();

                                      cb = (ConfigBaseRemote) ctx.lookup("java:global/ConfigBase");

                           

                          it works.

                           

                          But i would prefer to inject the bean to prevent a lookup on every click in the frontend.

                          • 10. Re: @EJB does not work from JSF 2.1 ManagedBean inside EAR in JBoss 7.2.0.Final
                            sfcoy

                            Matthias Grossmann wrote:

                             

                            ...

                             

                            @Stateless

                            @TransactionAttribute(TransactionAttributeType.REQUIRES_NEW)

                            @Remote( { ConfigBaseRemote.class } )

                            @EJB(name = "java:global/ConfigBase", beanInterface = ConfigBaseRemote.class)

                            public class ConfigBaseBean implements ConfigBaseRemote {

                             

                            ...

                             

                            This @EJB annotation has a couple of issues:

                            1. It provides EJB reference information that is scoped only to the bean on which it is declared. Therefore it is self-referential in this instance. I don't think this was your real intention;
                            2. "name" should be "The logical name of the ejb reference within the declaring component's (e.g., java:comp/env) environment." I think you've tried to use it to define the bean's global JNDI name

                            Therefore, you should probably just remove it.

                            Matthias Grossmann wrote:

                             

                            ...

                                @EJB

                                private ConfigBaseRemote configbase;

                             

                             

                            The structure in the ear is this:

                             

                            ear

                            |

                            |___ejb-jar (includes ConfigBaseBean)

                            |

                            |___api-jar (includes ConfigBaseRemote)

                            |

                            |___jsf-war

                             

                            ...

                            With this EAR structure both your ejb-jar and jsf-war will need correct manifest class path entries (or a suitable jboss-deployment-structure.xml file). Otherwise none of it's classes will be visible, which could manifest as a lookup problem. The best place for this jar is in the ear/lib directory.

                            • 11. Re: @EJB does not work from JSF 2.1 ManagedBean inside EAR in JBoss 7.2.0.Final
                              ybxiang.china

                              Agree with .

                              • 12. Re: @EJB does not work from JSF 2.1 ManagedBean inside EAR in JBoss 7.2.0.Final
                                mgrossmann

                                Many thanks for your help.

                                 

                                I will try your suggestions.

                                 

                                By the way.. is there another way to define a fix global jndi name?

                                • 13. Re: @EJB does not work from JSF 2.1 ManagedBean inside EAR in JBoss 7.2.0.Final
                                  ybxiang.china

                                  maybe jboss-ejb.xml is a good place.

                                  • 14. Re: Re: @EJB does not work from JSF 2.1 ManagedBean inside EAR in JBoss 7.2.0.Final
                                    sfcoy

                                    I just knocked up a quick test of this scenario for myself (running on 7.2.0.Final).

                                     

                                    I have this remote interface packaged in an api jar:

                                    public interface ConfigBaseRemote {
                                    
                                    
                                        void somePossiblyRemoteOperation();
                                       
                                    }
                                    
                                    

                                     

                                    and this implementation in an ejb jar:

                                    @Stateless
                                    @TransactionAttribute(TransactionAttributeType.REQUIRES_NEW)
                                    @Remote(ConfigBaseRemote.class)
                                    public class ConfigRemoteBean implements ConfigBaseRemote {
                                    
                                    
                                        @Inject
                                        private Logger log;
                                    
                                    
                                        /* (non-Javadoc)
                                         * @see org.jboss.demos.forum2609.remote.ConfigBaseRemote#somePossiblyRemoteOperation()
                                         */
                                        @Override
                                        public void somePossiblyRemoteOperation() {
                                            log.info("Performing some possibly remote operation");
                                        }
                                    
                                    
                                    }
                                    
                                    

                                     

                                    and this reference in a managed bean:

                                        @EJB
                                        private ConfigBaseRemote config;
                                    
                                    

                                    And it works just fine. I did not have to do anything special to get JSF 2.1 either.

                                     

                                    Therefore, we need to track down what you have done to your server and environment and/or application.

                                     

                                    Example attached.

                                     

                                    PS. This new Jive version looks pretty, but it's complete crap to use...

                                    1 2 Previous Next