1 2 Previous Next 15 Replies Latest reply on Jan 28, 2003 10:58 AM by joycestack

    Invoking methods from an EJB on another machien

    joycestack

      Hello,

      I got a very simple HelloWorld example working on my own machine using JBoss 3.0. The usual first example. I now want to be able to execute a client on my machine that connects to EJBs on a Linux machine that also runs JBoss 3.0.

      I have my two jar files HelloClient.jar with my client classes and interfaces. My HelloJAR.jar with my DD and jboss.xml etc.

      This is my client class with the ip of the linux machine that has my HelloJAR.jar file in the /server/default/deploy directory.

      Hashtable prop = new Hashtable();
      prop.put ("java.naming.factory.url.pkgs","org.jboss.naming:org.jnp.interfaces");
      prop.put ("java.naming.provider.url","jnp://172.16.220.160:1099");
      prop.put ("java.naming.factory.initial","org.jnp.interfaces.NamingContextFactory");

      try
      {
      Context ctx = new InitialContext(prop);
      Object obj = ctx.lookup("java:comp/env/ejb/helloworld/HelloWorld");
      HelloWorldHome home = (HelloWorldHome)javax.rmi.PortableRemoteObject.narrow(obj, HelloWorldHome.class);
      HelloWorld helloWorld = home.create();
      String str = helloWorld.sayHelloEJB("JOYCE is COOL");
      System.out.println(str);
      helloWorld.remove();
      }
      catch(Exception e)
      {
      e.printStackTrace();
      }
      }



      I have placed all jars on the classpath on the linux machine. I have the HelloJAR.jar in the deploy directory on the linxu machine also. When I execute my client on my windows machine I get a NullPointerException on line 25 which is the line
      HelloWorld helloWorld = home.create();

      Can someone put me ont he right track?

      Thanks
      Joyce

        • 1. Re: Invoking methods from an EJB on another machien

          You have the wrong jndi name

          Regards,
          Adrian

          • 2. Re: Invoking methods from an EJB on another machien
            joycestack

            Adrian,

            Again you come to my rescue. Can you tell me do I need to have ejb-ref's in my jboss.xml. ?

            I have an example here (the JBoss 3 Docs) and it goes on that the jndi name for remote calls (i think i have the correct context) is something like

            <jndi-name>
            jnp://banshee:1099/EncTests/ejbs/RemoteENCBean
            </jndi-name>

            Is it enough to have the ("java.naming.provider.url","jnp://172.16.220.160:1099");
            in my client class.?

            Thanks again,
            Joyce

            • 3. Re: Invoking methods from an EJB on another machien

              The format you quote is for an ejb
              accessing another ejb on a different machine.

              You want to avoid jnp/rmi invocations where possible
              because they involve serialization.

              For a simple ejb

              ejb-jar.xml

              <ejb-name>SomeEjb</ejb-name>
              ...

              jboss.xml

              <ejb-name>SomeEjb</ejb-name>
              <jndi-name>ejbs/SomeEjb</jndi-name>
              </session

              Is usually enough.

              Use lookup("ejbs/SomeEjb")

              If you don't specify jndi-name in jboss.xml
              it defaults to ejb-name

              Regards,
              Adrian

              • 4. Re: Invoking methods from an EJB on another machien
                joycestack

                Warjort,

                Thanks a million for the reply. All my code that allowed me to run HelloWorld on my machine should technically not need to change is that correct ?

                Do I still need to specify the IP of the machine I need to connect to in my client code like below

                Hashtable prop = new Hashtable();
                prop.put ("java.naming.factory.url.pkgs","org.jboss.naming:org.jnp.interfaces");
                prop.put ("java.naming.provider.url","jnp://172.16.220.160:1099");
                prop.put ("java.naming.factory.initial","org.jnp.interfaces.NamingContextFactory");

                try
                {
                Context ctx = new InitialContext(prop);
                Object obj = ctx.lookup("helloworld/HelloWorld");
                HelloWorldHome home = (HelloWorldHome)javax.rmi.PortableRemoteObject.narrow(obj, HelloWorldHome.class);
                HelloWorld helloWorld = home.create();
                String str = helloWorld.sayHelloEJB("JOYCE is COOL");
                System.out.println(str);
                helloWorld.remove();
                }
                catch(Exception e)
                {
                e.printStackTrace();
                }

                Thanks alot,
                Joyce

                • 5. Re: Invoking methods from an EJB on another machien

                  Yes, you specify the ip address
                  in the initial context on the client.

                  Regards,
                  Adrian

                  • 6. Re: Invoking methods from an EJB on another machien
                    joycestack

                    Thank You Adrian.

                    I'll get back to you. A bit sleepy after lunch so takes a little longer to do things.

                    Joyce

                    • 7. Re: Invoking methods from an EJB on another machien
                      joycestack

                      Adrian,

                      Can I ask your help again please? I have my Client jar in my client folder on my windows machine. I have my deployment jar file in the deploy folder on the linux machine. I still get the NullPointerException.

                      Any ideas on how i can debug this ?

                      Joyce

                      • 8. Re: Invoking methods from an EJB on another machien

                        You can try enumerating the initial context
                        to see what is available.

                        Also do you have the redhat problem
                        where etc/hosts points localhost at your ip address?

                        Regards,
                        Adrian

                        • 9. Re: Invoking methods from an EJB on another machien
                          joycestack

                          Hello Adrian,

                          Sorry for the delay in getting back to you. I dont know much about unix but my etc/hosts file I thought was only used to map computer names to IP addresses. If I am using the IP address surely I dont need to edit this file.

                          Thanks,
                          Joyce

                          • 10. Re: Invoking methods from an EJB on another machien

                            RMI will pick the first ip address for your hostname,
                            and pass that with the remote stub.

                            If the first one in /etc/hosts is 127.0.0.1 it won't
                            work. Redhat configure it like this to improve the
                            performance of some unix programs that don't check whether
                            they are local.

                            Regards,
                            Adrian

                            • 11. Re: Invoking methods from an EJB on another machien
                              joycestack

                              Adrian,

                              I have removed everything from my /etc/hosts file except the router IP address. Can I just write down the steps I have done.

                              1. Build the helloworld example on the linux machine and executed this with a script. All the appropiate files are in place.
                              2. Copied the Client.jar file onto my PC. The client class is set to look at the linux machine using the IP address.
                              3. I execute the CLient.jar with a bat file on the PC and get a NullPointerException.


                              Thanks,
                              Joyce

                              • 12. Re: Invoking methods from an EJB on another machien
                                joycestack

                                Adrian,

                                I am so stupid. The runclient.bat file uses the jar files from the jboss installed on my machine.
                                This is what it looks like

                                @echo on

                                set LCP=.
                                REM Default locations of jars we depend on
                                for %%i in (E:\jboss-3.0.4_tomcat-4.1.12\client\*.jar) do call lcp.bat %%i
                                for %%i in (..\..\lib\*.jar) do call lcp.bat %%i
                                for %%i in (..\client\*.jar) do call lcp.bat %%i

                                REM This automatically adds system classes to CLASSPATH
                                if exist C:\jdk1.3.1\jre\lib\tools.jar set LCP=%LCP%;C:\jdk1.3.1\jre\lib\tools.jar

                                echo C:\jdk1.3.1\jre\bin\java.exe -classpath "%LCP%" helloworld.client.HelloClient

                                C:\jdk1.3.1\jre\bin\java.exe -classpath "%LCP%" helloworld.client.HelloClient

                                Need to have this point at my linux machine right.? Whats the easiest way to execute my client on the linux from my PC.? Am I doing things backwards?

                                Joyce

                                • 13. Re: Invoking methods from an EJB on another machien
                                  joycestack

                                  Even if I run on my pc the following

                                  java -classpath .;j2ee.jar;HelloClient.jar HelloClient

                                  I get a NoClassDefFoundException.

                                  How do I execute the HelloClient.jar file on my PC to find the DD etc on the remote linux machine.

                                  Sorry Adrian for this but I need to be able to have this much done for work. A colleague of mine is back tomorrow after a Sun EJB course and I should be alot wiser after her training. But until then Im afraid I am relying on you.

                                  Thanks

                                  • 14. Re: Invoking methods from an EJB on another machien

                                    You want jboss-j2ee.jar
                                    You also need jnp-client.jar

                                    Have you got the quickstart guide?
                                    If I remember correctly, there is a whole chapter on
                                    making a connection from a client.

                                    Regards,
                                    Adrian

                                    1 2 Previous Next