9 Replies Latest reply on Sep 23, 2007 8:47 PM by David Radunz

    @Service / @Management bean injection

    David Radunz Newbie

      Hey All,

      I am just getting my hands dirty with Seam for the first time after months of deliberation. I have setup an EJB3 service that I would like to inject into a seam component/bean. I have tried using both @In and @EJB using the class name and the interface, as well as @EJB(name="blah") (blah being the objectName for the service). None of these approaches seem to work, all @In attempts result in a Seam error stating the item is null, all @EJB attempts result in the property being left null.

      Any ideas? I am a little lost..

      Cheers,

      David

        • 1. Re: @Service / @Management bean injection
          Stuart Douglas Master

          Have you tried @In(create=true) or annoting the EJB with @AutoCreate ?

          • 2. Re: @Service / @Management bean injection
            David Radunz Newbie

            Yeah, I just tried it then. I didn't try it before because the service is initialized when it's deployed. It didn't help, I am still getting the same error..

            10:39:34,308 ERROR [ExceptionFilter] uncaught exception
            javax.servlet.ServletException: In attribute requires non-null value: authenticator.gajService


            Granted EJB3 and Seam are not the same thing, but because Seam operates within the EJB container, surely there is a way of operating with an EJB3 service? I really don't want to have to do a JNDI lookup :( That would suck.

            Hope someone can help,

            David

            • 3. Re: @Service / @Management bean injection
              David Radunz Newbie

              Perhaps I should rephrase my question. Assuming I implemented a service following the tutorial: http://docs.jboss.com/ejb3/app-server/tutorial/service/service.html - How would I inject that service into my Seam component? Would I need to inject it by a remote or local interface, or by the management interface?

              I currently have the following:

              Management Interface:

              @Management
              public interface GAJServiceMBean extends ServiceMBean {
              ....
              }


              Local Interface:
              @Local
              public interface GAJServiceLocal {
              ....
              }



              Remote Interface:
              @Remote
              public interface GAJServiceRemote {
              ....
              }



              Implementation:
              @Service(objectName = "utiba:service=GAJ")
              public class GAJService extends ServiceMBeanSupport implements GAJServiceMBean, GAJServiceLocal, GAJServiceRemote, Serializable {
               // Serial Version
               protected static final long serialVersionUID = 10234234234L;
              
               ......
              }


              I have tried some of the following injection combinations:

              @In GAJServiceMBean gajService;
              
              @EJB GAJServiceMBean gajService;
              @EJB GAJServiceLocal gajService;
              @EJB GAJServiceRemote gajService;


              The first results in the not-null condition, the rest result in the object remaining null.

              Cheers,

              David

              • 4. Re: @Service / @Management bean injection
                Norman Richards Master

                @Service creates an MBean, not an EJB. We don't provide a way to lookup MBeans (though perhpaps we should?) so you should implement the JNDI lookups yourself - using @Factory or maybe @Unwrap.

                • 5. Re: @Service / @Management bean injection
                  David Radunz Newbie

                  Hey,

                  I found a feature request that suggests there is a way of doing it: http://jira.jboss.org/jira/browse/JBSEAM-1498

                  I am not stuck with the fact that I cannot inject a stateless bean either. Perhaps my understanding of how Seam works is wrong.

                  I have a stateless bean:

                  @Name("utibaServiceGajMediator")
                  @Stateless
                  @Scope(ScopeType.APPLICATION)
                  @AutoCreate
                  public class GAJServiceMediatorBean implements GAJServiceMediator {
                  
                   public int nextTransID() throws Exception {
                   return 1234;
                   }
                  
                  }


                  Implementing interface:

                  @Local
                  public interface GAJServiceMediator {
                  
                   public int nextTransID() throws Exception;
                  
                  }


                  And then used by this class:

                  @Name("utibaTester")
                  @AutoCreate
                  public class UtibaTest {
                  
                   @In GAJServiceMediator gajServiceMediator;
                   @In MyBean myBean;
                  
                   public void takeMagicFlight() {
                   try {
                   myBean.setText("TransID = " + gajServiceMediator.nextTransID());
                   } catch (Exception e) {
                   myBean.setText("Failed");
                   }
                   }
                  
                  }


                  Then elsewhere I have a commandLink:

                  <h:commandButton type="submit" value="Fly Away" action="#{utibaTester.takeMagicFlight}"/>
                  


                  And get the following error message:

                  Caused by: javax.faces.el.EvaluationException: /ajax.xhtml @22,102 action="#{utibaTester.takeMagicFlight}": org.jboss.seam.RequiredException: In attribute requires non-null value: utibaTester.gajServiceMediator
                   at com.sun.facelets.el.LegacyMethodBinding.invoke(LegacyMethodBinding.java:73)
                   at org.apache.myfaces.application.ActionListenerImpl.processAction(ActionListenerImpl.java:63)
                   ... 43 more
                  Caused by: org.jboss.seam.RequiredException: In attribute requires non-null value: utibaTester.gajServiceMediator


                  Well that is the useful portion anyway. As far as I can tell I have followed the documentation, perhaps my understanding of how all this should work together is wrong? Should I be able to have one stateless bean injected as a resource to another bean?

                  Any help would be appreciated.

                  Cheers,

                  David

                  • 6. Re: @Service / @Management bean injection
                    Michael Small Newbie

                    Have you tried @Resource for your injection. Since @Service classes are stored in JNDI, I don't see why the @Resource wouldn't work.

                    • 7. Re: @Service / @Management bean injection
                      Norman Richards Master

                      We've had several requests for this, so I've scheduled that for 2.0.1.

                      • 8. Re: @Service / @Management bean injection
                        Matt Drees Master

                         

                        "rhomber" wrote:

                        @Name("utibaServiceGajMediator")
                        ...
                        public class GAJServiceMediatorBean implements GAJServiceMediator {
                        
                        }


                        And then used by this class:

                        ...
                         @In GAJServiceMediator gajServiceMediator;
                        


                        ...

                        org.jboss.seam.RequiredException: In attribute requires non-null value: utibaTester.gajServiceMediator


                        Your names don't match; gajServiceMediator != utibaServiceGajMediator

                        • 9. Re: @Service / @Management bean injection
                          David Radunz Newbie

                          Hey All,

                          Thanks so much for helping. I didn't know that the instance variable had to be the same name as the @Name, I thought it was injected based on the class. Thanks for clarifying this, I feel a bit silly :). It all works fine now, and I was able to inject the service via another means that seems a little less tacky than the JNDI lookup (@Resource). However, I would love a Seam built-in to handle it as is suggested above. I simply used the method described in: http://jira.jboss.org/jira/browse/JBSEAM-1498 as a work around and it worked fine.

                          Cheers and thank you,

                          David