7 Replies Latest reply on May 6, 2009 1:02 PM by Pedro Sena

    Problems trying to access EJB

    Pedro Sena Newbie

      Hi Guys,


      I tried EVERYTHING but I'm unable to make it work.


      I'm trying to make an EJB lookup in my Seam component but it's failing.


      Here the important files:


      The EJB:


      @Stateful(name="busService")
      @Local(IBusService.class)
      public class BusFacade implements IBusService {



      The ejb consumer:


      Context ctx = new InitialContext();
      this.busService = (IBusService) ctx.lookup("busService");



      components.xml:


      <core:init jndi-pattern="#{ejbName}" debug="true"
                precedence="100" />



      Some interesting log from Jboss 4.2.3:


      DEBUG [org.jboss.ejb3.stateful.StatefulDelegateWrapper] Starting jboss.j2ee:ear=travelnetwork-business.ear,jar=ejb-0.0.1.jar,name=busService,service=EJB3
      
      INFO  [org.jboss.ejb3.EJBContainer] STARTED EJB: br.com.netsar.tnw.business.model.bus.BusFacade ejbName: busService
      
      DEBUG [org.jboss.web.tomcat.service.TomcatDeployer] Linking ejb-ref: busService to JNDI name: busService



      And finally my exception:


      javax.naming.NameNotFoundException: busService not bound



      I'm going crazy, don't know what else I can do.


      I'm using jboss 4.2.3 and Seam 2.1.0.GA


      Any directions are appreciated.


      Thanks in advance,


      Pedro Sena

        • 1. Re: Problems trying to access EJB
          Arbi Sookazian Master

          This is merely a JNDI lookup problem (whenever you see not bound, it's JNDI-relate issue).  Did you use seam-gen to create your project?


          For my seam-gen'd app, here's what I have:


          components.xml:


          <core:init debug="@debug@" jndi-pattern="@jndiPattern@"/>



          components.properties:


          jndiPattern \#{ejbName}/local
          debug true



          check your build.xml (there was a bug that was fixed at some point in Seam 2.0.x or 2.1.x so that the Seam debug and jndi-pattern values were read dynamically from the components.properties rather than doing what I have below, which does work by the way, it's just static):


          <target name="war" depends="compile" 
                                  description="Build the distribution .war file">
          
          <copy todir="${war.dir}/WEB-INF">
                                  <fileset dir="${basedir}/resources/WEB-INF">
                                          <include name="*.*"/>
                                          <include name="classes/**/*.*"/>
                                          <exclude name="classes/**/*.class"/>
                                            <exclude name="${basedir}/resources/WEB-INF/web.xml"/>
                                  </fileset>
                                   <!-- filter token replaces @debug@ and @jndiPattern@ in components.xml -->                     
                                  <filterset>                             
                                      <filter token="debug" value="${debug}" />
                                      <filter token="jndiPattern" value="${project.name}/#{ejbName}/local" />
                                  </filterset>
                          </copy>    
          </target>



          To make it simpler at first, you can hard-code the debug and jndi-pattern values directly in components.xml.  Either way should work fine.


          In Seam, we don't do old school JNDI lookups like this anymore:


          Context ctx = new InitialContext();
          this.busService = (IBusService) ctx.lookup("busService");



          instead, use this in your target Seam component:


          @In(required=false)
          private fooLocalDAO fooDAO;



          VERY IMPORTANT: the pattern here is access modifier (private in this case), then local interface name, then Seam component name (e.g., @Name(fooDAO)).


          Of course, if your EJB is not a Seam component for some reason (i.e. no @Name(foo)), then you can revert to @EJB injection as per JSR 220.

          • 2. Re: Problems trying to access EJB
            Arbi Sookazian Master

            btw, if you don't add @AutoCreate to your EJB, then you must use the following:


            @In(required=false, create=true)
            private fooLocalDAO fooDAO;

            • 3. Re: Problems trying to access EJB
              Pedro Sena Newbie

              Hi Arbi,


              I did what you suggested, for my surprise:


              Caused by: org.jboss.seam.InstantiationException: Could not instantiate Seam component: busService
                   at org.jboss.seam.Component.newInstance(Component.java:2066)
                   at org.jboss.seam.Component.getInstance(Component.java:1948)
                   at org.jboss.seam.Component.getInstance(Component.java:1910)
                   at org.jboss.seam.Component.getInstance(Component.java:1904)
                   at org.jboss.seam.Component.getInstanceInAllNamespaces(Component.java:2271)
                   at org.jboss.seam.Component.getValueToInject(Component.java:2223)
                   at org.jboss.seam.Component.injectAttributes(Component.java:1663)
                   at org.jboss.seam.Component.inject(Component.java:1481)
                   at org.jboss.seam.core.BijectionInterceptor.aroundInvoke(BijectionInterceptor.java:61)
                   at org.jboss.seam.intercept.SeamInvocationContext.proceed(SeamInvocationContext.java:68)
                   at org.granite.tide.seam.TideInterceptor.aroundInvoke(TideInterceptor.java:155)
                   at org.jboss.seam.intercept.SeamInvocationContext.proceed(SeamInvocationContext.java:68)
                   at org.jboss.seam.transaction.RollbackInterceptor.aroundInvoke(RollbackInterceptor.java:28)
                   at org.jboss.seam.intercept.SeamInvocationContext.proceed(SeamInvocationContext.java:68)
                   at org.jboss.seam.core.ConversationInterceptor.aroundInvoke(ConversationInterceptor.java:56)
                   at org.jboss.seam.intercept.SeamInvocationContext.proceed(SeamInvocationContext.java:68)
                   at org.jboss.seam.core.MethodContextInterceptor.aroundInvoke(MethodContextInterceptor.java:44)
                   at org.jboss.seam.intercept.SeamInvocationContext.proceed(SeamInvocationContext.java:68)
                   at org.jboss.seam.intercept.RootInterceptor.invoke(RootInterceptor.java:107)
                   at org.jboss.seam.intercept.JavaBeanInterceptor.interceptInvocation(JavaBeanInterceptor.java:166)
                   at org.jboss.seam.intercept.JavaBeanInterceptor.invoke(JavaBeanInterceptor.java:102)
                   at br.com.netsar.tnw.client.model.service.bus.BusFacade_$$_javassist_1.setUpServices(BusFacade_$$_javassist_1.java)
                   at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
                   at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:39)
                   at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25)
                   at java.lang.reflect.Method.invoke(Method.java:597)
                   at org.granite.messaging.service.ServiceInvocationContext.invoke(ServiceInvocationContext.java:71)
                   at org.granite.messaging.service.security.AbstractSecurityService.endAuthorization(AbstractSecurityService.java:42)
                   at org.granite.seam21.security.Seam21SecurityService.authorize(Seam21SecurityService.java:108)
                   at org.granite.messaging.service.ServiceInvoker.invoke(ServiceInvoker.java:144)
                   ... 24 more
              Caused by: javax.naming.NameNotFoundException: busService not bound
                   at org.jnp.server.NamingServer.getBinding(NamingServer.java:529)
                   at org.jnp.server.NamingServer.getBinding(NamingServer.java:537)
                   at org.jnp.server.NamingServer.getObject(NamingServer.java:543)
                   at org.jnp.server.NamingServer.lookup(NamingServer.java:296)
                   at org.jnp.interfaces.NamingContext.lookup(NamingContext.java:667)
                   at org.jnp.interfaces.NamingContext.lookup(NamingContext.java:627)
                   at javax.naming.InitialContext.lookup(InitialContext.java:392)
                   at org.jboss.seam.Component.instantiateSessionBean(Component.java:1327)
                   at org.jboss.seam.Component.instantiate(Component.java:1291)
                   at org.jboss.seam.Component.newInstance(Component.java:2044)
                   ... 53 more




              Any tips?


              Thanks,


              Pedro Sena

              • 4. Re: Problems trying to access EJB
                Pedro Sena Newbie

                What I did:


                @Name("busFacade")
                @Scope(ScopeType.CONVERSATION)
                public class BusFacade {
                     
                @In(create = true)
                private IBusService busService;




                and



                @Name("busService")
                @Stateful(name="busService")
                @Local(IBusService.class)
                public class BusFacade implements IBusService








                • 5. Re: Problems trying to access EJB
                  Arbi Sookazian Master

                  The following is incorrect code for a Seam app:


                  @Stateful(name="busService")
                  @Local(IBusService.class)
                  public class BusFacade implements IBusService {...}



                  do this:


                  @Stateful
                  @Name("busFacade")
                  public class BusFacade implements IBusService {...}



                  @Local
                  public interface IBusService  {
                  ...
                  }



                  In EJB3, the @Local annotation is the default, so you don't need it if the interface is local.


                  Now, when you inject an instance of your SFSB into another SFSB, for example, do this:


                  @In(required=false, create=true) IBusService busFacade;

                  • 6. Re: Problems trying to access EJB
                    Pedro Sena Newbie

                    Hi Arbi,


                    Thanks for the quick response.


                    I fixed as you suggested:


                    @Stateful
                    @Name("busFacade")
                    public class BusFacade implements IBusService, Serializable {



                    The bean that is trying to use it:


                    @Name("busFacadeClient")
                    @Scope(ScopeType.CONVERSATION)
                    public class BusFacade {
                         
                    @In(create = true, value="busFacade")
                    private IBusService busService;



                    In my log:


                    2009-05-05 20:47:53,906 INFO  [org.jboss.seam.Component] Component: busFacade, scope: CONVERSATION, type: STATEFUL_SESSION_BEAN, class: br.com.netsar.tnw.business.model.bus.BusFacade, JNDI: BusFacade/local



                    So I changed the components.xml as follows:


                    <core:init jndi-pattern="#{ejbName}/local" debug="true" precedence="100" />



                    But the error persists:



                    Caused by: org.jboss.seam.InstantiationException: Could not instantiate Seam component: busFacade
                         at org.jboss.seam.Component.newInstance(Component.java:2066)
                         at org.jboss.seam.Component.getInstance(Component.java:1948)
                         at org.jboss.seam.Component.getInstance(Component.java:1910)
                         at org.jboss.seam.Component.getInstance(Component.java:1904)
                         at org.jboss.seam.Component.getInstance(Component.java:1899)
                         at org.granite.tide.seam.AbstractSeamServiceContext.findComponent(AbstractSeamServiceContext.java:172)
                         at org.granite.tide.TideServiceInvoker.adjustInvokee(TideServiceInvoker.java:161)
                         at org.granite.messaging.service.ServiceInvoker.invoke(ServiceInvoker.java:96)
                         at org.granite.messaging.amf.process.AMF3MessageProcessor.processRemotingMessage(AMF3MessageProcessor.java:136)
                         at org.granite.messaging.amf.process.AMF3MessageProcessor.process(AMF3MessageProcessor.java:59)
                         at org.granite.messaging.amf.process.AMF0MessageProcessor.process(AMF0MessageProcessor.java:71)
                         at org.granite.messaging.webapp.AMFMessageServlet.doPost(AMFMessageServlet.java:59)
                         ... 20 more
                    Caused by: javax.naming.NameNotFoundException: BusFacade not bound
                         at org.jnp.server.NamingServer.getBinding(NamingServer.java:529)
                         at org.jnp.server.NamingServer.getBinding(NamingServer.java:537)
                         at org.jnp.server.NamingServer.getObject(NamingServer.java:543)
                         at org.jnp.server.NamingServer.lookup(NamingServer.java:267)
                         at org.jnp.interfaces.NamingContext.lookup(NamingContext.java:667)
                         at org.jnp.interfaces.NamingContext.lookup(NamingContext.java:627)
                         at javax.naming.InitialContext.lookup(InitialContext.java:392)
                         at org.jboss.seam.Component.instantiateSessionBean(Component.java:1327)
                         at org.jboss.seam.Component.instantiate(Component.java:1291)
                         at org.jboss.seam.Component.newInstance(Component.java:2044)
                         ... 31 more



                    Thanks one more time.


                    Pedro Sena

                    • 7. Re: Problems trying to access EJB
                      Pedro Sena Newbie

                      Problem solved using:


                      <core:init jndi-pattern="travelnetwork-business/#{ejbName}/local" debug="true" precedence="100" />



                      Thanks for your time.


                      Pedro Sena