8 Replies Latest reply on Jan 27, 2009 12:40 PM by Jose Alvarez de Lara

    InitialContext reference and definition

    David Jitendranath Novice

      My Session EJB is successfully deployed in JBoss 4.2.2 and here's the entry in the Global JNDI Namespace from the JMX console.

      +- MXIntegrationEAR (class: org.jnp.interfaces.NamingContext)
       | +- BurdenRateFacade (class: org.jnp.interfaces.NamingContext)
       | | +- local (proxy: $Proxy80 implements interface gov.usbr.data.cdw.BurdenRateFacadeLocal,interface org.jboss.ejb3.JBossProxy)
       | | +- remote (proxy: $Proxy78 implements interface gov.usbr.data.cdw.BurdenRateFacadeRemote,interface org.jboss.ejb3.JBossProxy)


      In my client code where I try to get a local reference of the EJB, this runs with a main method from a java class that is not deployed in the App server.
      When I do a
      Context ctx = new InitialContext()
      ctx.lookup("BurdenRateFacade")


      I get a
      javax.naming.NoInitialContextException: Need to specify class name in environment or system property, or as an applet parameter


      Do I need to define an ejb-jar.xml? with some entries like these?
      <ejb-local-ref>
       <ejb-ref-name>ejb/Test</ejb-ref-name>
       <ejb-ref-type>Session</ejb-ref-type>
       <local>Test</local>
      </ejb-local-ref>



        • 1. Re: InitialContext reference and definition
          Peter Johnson Master

          I think you are missing the jndi.properties file. It should contain:

          java.naming.factory.initial=org.jnp.interfaces.NamingContextFactory
          java.naming.provider.url=jnp://localhost:1099
          java.naming.factory.url.pkgs=org.jboss.naming:org.jnp.interfaces


          • 2. Re: InitialContext reference and definition
            David Jitendranath Novice

            Thanks Peter!
            I no longer get the NoInitialContext exception.
            Now when I do a look up with the code

            BurdenRateFacade bean = (BurdenRateFacade)context.lookup("BurdenRateFacade");


            I get a Classcast exception.
            java.lang.ClassCastException: org.jnp.interfaces.NamingContext


            I did a
            context.lookup("BurdenRateFacade").getClass()

            and it prints org.jnp.interfaces.NamingContext

            I have the following entries in my jndi.properties
            java.naming.factory.initial=org.jnp.interfaces.NamingContextFactory
            java.naming.factory.url.pkgs=org.jboss.naming:org.jnp.interfaces
            java.naming.provider.url=jnp://localhost:1099


            • 3. Re: InitialContext reference and definition
              Peter Johnson Master

              You need to lookup "BurdenRateFacade/remote". Note the full name in the JNDI tree that you posted earlier.

              • 5. Re: InitialContext reference and definition
                Jose Alvarez de Lara Novice

                Hi,

                How do I call a local interface?

                This is then Global JNDI Namespace from then JMX Console of
                JBoss 4.2.2 GA,

                +- CalculatorEAR (class: org.jnp.interfaces.NamingContext)
                 | +- CalculatorBean (class: org.jnp.interfaces.NamingContext)
                 | | +- local (proxy: $Proxy62 implements interface jboss.tutorial.Calculator,interface org.jboss.ejb3.JBossProxy)
                


                and then client is,

                package jboss.tutorial;
                
                import java.util.Properties;
                
                import javax.ejb.EJB;
                import javax.naming.InitialContext;
                import javax.naming.Context;
                
                public class CalculatorClient {
                
                 @EJB
                 private static Calculator calculator;
                
                 public CalculatorClient() {
                 try {
                 Properties props = new Properties();
                 props.put("java.naming.factory.initial", "org.jnp.interfaces.NamingContextFactory");
                 props.put("java.naming.factory.url.pkgs", "jnp://localhost:1099");
                 props.put("java.naming.provider.url", "jnp://localhost:1099");
                 Context ctx = new InitialContext(props);
                 calculator = (Calculator) ctx.lookup("CalculatorBean/local");
                 } catch (Exception ex) {
                 System.out.println("Couldn’t create calculator bean. "+
                 ex.getMessage());
                 }
                 }
                
                 /**
                 * @param args
                 */
                 public static void main(String[] args) {
                 // TODO Auto-generated method stub
                 CalculatorClient cClient = new CalculatorClient();
                
                 System.out.println("1 + 1 = " + calculator.add(1, 1));
                 System.out.println("1 - 1 = " + calculator.subtract(1, 1));
                 }
                }
                


                The output gives me the Exception,

                Couldn’t create calculator bean. CalculatorBean not bound
                


                Regards,
                pepelara

                • 6. Re: InitialContext reference and definition
                  jaikiran pai Master

                   

                  ctx.lookup("CalculatorBean/local")


                  Should be

                  ctx.lookup("CalculatorEAR/CalculatorBean/local")


                  • 7. Re: InitialContext reference and definition
                    Jose Alvarez de Lara Novice

                    Hi,

                    It is working ok when I call the remote interface.
                    I do it as you say. It works fine.

                    But about the local interface, then console of Eclipse
                    gives me the following exception,

                    Caught an unexpected exception!
                    javax.ejb.EJBException: Invalid (i.e. remote) invocation of local interface (null container)
                     at org.jboss.ejb3.stateless.StatelessLocalProxy.invoke(StatelessLocalProxy.java:80)
                     at $Proxy0.add(Unknown Source)
                     at jboss.tutorial.CalculatorClient.doOperations(CalculatorClient.java:29)
                     at jboss.tutorial.CalculatorClient.main(CalculatorClient.java:45)
                    


                    This is my new client,

                    public class CalculatorClient {
                    
                     @EJB
                     private static Calculator calculator;
                    
                     public CalculatorClient() {
                     try {
                     Properties props = new Properties();
                     props.put("java.naming.factory.initial", "org.jnp.interfaces.NamingContextFactory");
                     props.put("java.naming.factory.url.pkgs", "jnp://localhost:1099");
                     props.put("java.naming.provider.url", "jnp://localhost:1099");
                     Context ctx = new InitialContext(props);
                     calculator = (Calculator) ctx.lookup("CalculatorEAR/CalculatorBean/local");
                     } catch (Exception ex) {
                     System.out.println("Couldn’t create calculator bean. "+
                     ex.getMessage());
                     }
                     }
                    
                     public void doOperations() {
                     try {
                     System.out.println("1 + 1 = " + calculator.add(1, 1));
                     System.out.println("1 - 1 = " + calculator.subtract(1, 1));
                    
                     System.exit(0);
                     } catch (Exception ex) {
                     System.err.println("Caught an unexpected exception!");
                     ex.printStackTrace();
                     }
                     }
                    
                     /**
                     * @param args
                     */
                     public static void main(String[] args) {
                     // TODO Auto-generated method stub
                     CalculatorClient cClient = new CalculatorClient();
                     cClient.doOperations();
                     }
                    }
                    


                    The exception occurs when I call the doOperations() method.

                    Thanks,
                    pepelara

                    • 8. Re: InitialContext reference and definition
                      Jose Alvarez de Lara Novice

                      Hi,

                      Take a look at this post,

                      http://www.jboss.com/index.html?module=bb&op=viewtopic&p=4105969#4105969

                      I think he has got the solution.
                      But I have no idea about what he is saying.

                      Load balancing and cluster????

                      Help!

                      Regards,
                      pepelara