3 Replies Latest reply on Jul 12, 2012 1:23 PM by jaikiran pai

    Exception running ejb client locally

    Chaitanya Kulkarni Newbie

      I am getting following:

      javax.naming.NoInitialContextException: Need to specify class name in environment or system property, or as an applet parameter, or in an application resource file:  java.naming.factory.initial

          at javax.naming.spi.NamingManager.getInitialContext(Unknown Source)

          at javax.naming.InitialContext.getDefaultInitCtx(Unknown Source)

          at javax.naming.InitialContext.getURLOrDefaultInitCtx(Unknown Source)

          at javax.naming.InitialContext.lookup(Unknown Source)

          at com.mypackage.client.EJBApplicationClient.doLookup(EJBApplicationClient.java:28)

          at com.mypackage.client.EJBApplicationClient.main(EJBApplicationClient.java:14)

      Exception in thread "main" java.lang.NullPointerException

          at com.mypackage.client.EJBApplicationClient.main(EJBApplicationClient.java:15)

      =======================================================

       

      jboss-client.properties is as below:

      remote.connectionprovider.create.options.org.xnio.Options.SSL_ENABLED=false

       

       

      remote.connections=default

       

       

      remote.connection.default.host=localhost

      remote.connection.default.port = 4447

      remote.connection.default.connect.options.org.xnio.Options.SASL_POLICY_NOANONYMOUS=false

      =======================================================

       

      Here is EJBApplicationClient:

      package com.mypackage.client;

      import javax.naming.Context;

      import javax.naming.NamingException;

       

       

      import com.mypackage.Calculator;

      import com.mypackage.CalculatorBean;

      import com.mypackage.clientutility.ClientUtility;

       

       

       

       

      public class EJBApplicationClient {

       

          public static void main(String[] args) {

              Calculator bean = doLookup();

              System.out.println(bean.add(2, 3)); // 4. Call business logic

          }

       

          private static Calculator doLookup() {

              Context context = null;

              Calculator bean = null;

              try {

                  // 1. Obtaining Context

                  context = ClientUtility.getInitialContext();

                

                  bean = (Calculator) context.lookup("java:module/CalculatorBean!com.mypackage.CalculatorLocal");

       

              } catch (NamingException e) {

                  e.printStackTrace();

              }

              return bean;

          }

       

       

      }

       

      =====================================

      Client Utility is as below:

       

      package com.mypackage.clientutility;

       

       

      import java.util.Properties;

      import javax.naming.Context;

      import javax.naming.InitialContext;

      import javax.naming.NamingException;

       

      public class ClientUtility {

       

          private static Context initialContext;

       

          private static final String PKG_INTERFACES = "org.jboss.ejb.client.naming";

       

          public static Context getInitialContext() throws NamingException {

              if (initialContext == null) {

                  Properties properties = new Properties();

                  properties.put(Context.URL_PKG_PREFIXES, PKG_INTERFACES);

        

                  initialContext = new InitialContext(properties);

              }

              return initialContext;

          }

      }

       

      ====================================================

      Calculator Bean is as below:

      /*

      * JBoss, Home of Professional Open Source.

      * Copyright 2006, Red Hat Middleware LLC, and individual contributors

      * as indicated by the @author tags. See the copyright.txt file in the

      * distribution for a full listing of individual contributors.

      *

      * This is free software; you can redistribute it and/or modify it

      * under the terms of the GNU Lesser General Public License as

      * published by the Free Software Foundation; either version 2.1 of

      * the License, or (at your option) any later version.

      *

      * This software is distributed in the hope that it will be useful,

      * but WITHOUT ANY WARRANTY; without even the implied warranty of

      * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU

      * Lesser General Public License for more details.

      *

      * You should have received a copy of the GNU Lesser General Public

      * License along with this software; if not, write to the Free

      * Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA

      * 02110-1301 USA, or see the FSF site: http://www.fsf.org.

      */

      package com.mypackage;

       

       

      import javax.ejb.Local;

      import javax.ejb.Remote;

      import javax.ejb.Stateless;

       

       

      @Stateless

      @Remote(CalculatorRemote.class)

      @Local(CalculatorLocal.class)

      public class CalculatorBean implements CalculatorRemote, CalculatorLocal

      {

        public int add(int x, int y)

        {

            return x + y;

        }

       

       

        public int subtract(int x, int y)

        {

            return x - y;

        }

      }

      ==============================

       

      Thanks in advance.

        • 1. Re: Exception running ejb client locally
          jaikiran pai Master

          context.lookup("java:module/CalculatorBean!com.mypackage.CalculatorLocal");

           

          Wrong JNDI name. Read this article for invoking on remote beans from a standalone client https://docs.jboss.org/author/display/AS71/EJB+invocations+from+a+remote+client+using+JNDI

          • 2. Re: Exception running ejb client locally
            Chaitanya Kulkarni Newbie

            From Server.log I get :

            java:global/myejb1EAR/myejb1/CalculatorBean!com.mypackage.CalculatorLocal

                      java:app/myejb1/CalculatorBean!com.mypackage.CalculatorLocal

                      java:module/CalculatorBean!com.mypackage.CalculatorLocal

                      java:global/myejb1EAR/myejb1/CalculatorBean!com.mypackage.CalculatorRemote

                      java:app/myejb1/CalculatorBean!com.mypackage.CalculatorRemote

                      java:module/CalculatorBean!com.mypackage.CalculatorRemote

                      java:jboss/exported/myejb1EAR/myejb1/CalculatorBean!com.mypackage.CalculatorRemote

             

            This is my ClientUtility Class:

            package com.mypackage.clientutility;

             

             

            import java.util.Properties;

            import javax.naming.Context;

            import javax.naming.InitialContext;

            import javax.naming.NamingException;

             

            public class ClientUtility {

             

                private static Context initialContext;

             

                private static final String PKG_INTERFACES = "org.jboss.ejb.client.naming";

             

                public static Context getInitialContext() throws NamingException {

                    if (initialContext == null) {

                        Properties properties = new Properties();

                        properties.put(Context.URL_PKG_PREFIXES, PKG_INTERFACES);

                        properties.put("jboss.naming.client.ejb.context", true);

                       

                        initialContext = new InitialContext(properties);

                    }

                    return initialContext;

                }

            }

            ====

            And here is EJBApplicationClient

            package com.mypackage.client;

            import javax.naming.Context;

            import javax.naming.NamingException;

             

             

            import com.mypackage.Calculator;

            import com.mypackage.CalculatorBean;

            import com.mypackage.CalculatorRemote;

            import com.mypackage.clientutility.ClientUtility;

             

             

             

             

            public class EJBApplicationClient {

             

                public static void main(String[] args) {

                    Calculator bean = doLookup();

                    System.out.println(bean.add(2, 3)); // 4. Call business logic

                }

             

                private static Calculator doLookup() {

                    Context context = null;

                    Calculator bean = null;

                    try {

                        // 1. Obtaining Context

                        context = ClientUtility.getInitialContext();

                     

                     

                        // Since we haven't deployed the application as a .ear, the app name for us will be an empty string

                        final String appName = "";

                        // This is the module name of the deployed EJBs on the server. This is typically the jar name of the

                        // EJB deployment, without the .jar suffix, but can be overridden via the ejb-jar.xml

                        // In this example, we have deployed the EJBs in a jboss-as-ejb-remote-app.jar, so the module name is

                        // jboss-as-ejb-remote-app

                        final String moduleName = "ejbmodule";

                        // AS7 allows each deployment to have an (optional) distinct name. We haven't specified a distinct name for

                        // our EJB deployment, so this is an empty string

                        final String distinctName = "";

                        // The EJB name which by default is the simple class name of the bean implementation class

                        final String beanName = CalculatorBean.class.getSimpleName();

                        // the remote view fully qualified class name

                        final String viewClassName = CalculatorRemote.class.getName();

                        // let's do the lookup

                       bean= (CalculatorRemote) context.lookup("ejb:" + appName + "/" + moduleName + "/" + distinctName + "/" + beanName + "!" + viewClassName);

                       

                       

            //            bean = (Calculator) context.lookup("java:module/CalculatorBean!com.mypackage.CalculatorLocal");

             

                    } catch (NamingException e) {

                        e.printStackTrace();

                    }

                    return bean;

                }

             

             

            }

             

            ====

            I am now getting :

            Jul 12, 2012 1:14:33 PM org.jboss.ejb.client.EJBClient <clinit>

            INFO: JBoss EJB Client version 1.0.5.Final

            Exception in thread "main" java.lang.IllegalStateException: No EJB receiver available for handling [appName:,modulename:ejbmodule,distinctname:] combination for invocation context org.jboss.ejb.client.EJBClientInvocationContext@fee4648

                      at org.jboss.ejb.client.EJBClientContext.requireEJBReceiver(EJBClientContext.java:584)

                      at org.jboss.ejb.client.ReceiverInterceptor.handleInvocation(ReceiverInterceptor.java:119)

                      at org.jboss.ejb.client.EJBClientInvocationContext.sendRequest(EJBClientInvocationContext.java:181)

                      at org.jboss.ejb.client.EJBInvocationHandler.doInvoke(EJBInvocationHandler.java:136)

                      at org.jboss.ejb.client.EJBInvocationHandler.doInvoke(EJBInvocationHandler.java:121)

                      at org.jboss.ejb.client.EJBInvocationHandler.invoke(EJBInvocationHandler.java:104)

                      at $Proxy0.add(Unknown Source)

                      at com.mypackage.client.EJBApplicationClient.main(EJBApplicationClient.java:18)

             

            ==========================

            Could you please suggest?

            Also if I am working with local interface I should be using java: rather than ejb: . Could you please correct me.

             

            Thanks in advace.

            • 3. Re: Exception running ejb client locally
              jaikiran pai Master

              You can't use local interfaces from a remote (standalone) client/application.