10 Replies Latest reply on Mar 13, 2008 3:36 PM by Sajith Kariyawasam

    application clients in JBOSS 4.2.2

    Sajith Kariyawasam Newbie

      Hi all ,

      Im new to j2ee development..

      in order to execute an application client in JBOSS 4.2.2 do i have to make any configurations additionally ?

      im asking this is , i am working with EJBs , so i developed a session Bean ( EJB 3.0 ) which searches for wine types <an example of an ebook> .
      (for the testing purpoes , i just hard coded the values without using a database).

      That EJB is invoked by an application client (.jar) , and i packaged those two (EJB and app client) into an .ear and deployed to JBOSS. ( im using Netbeans 6.0 )

      but , when the application is run in Netbeans , it gives a Nullpointer exception ... :( ...

      But when i deploed into the Glassfish server , it doesnot give any exception and works fine ...

      so that s why im asking whether JBOSS support running application clients ?? ...

      One more question ... is it a must to package EJB(.jar) and application client(.jar) into an .ear archive ? or cant i deploy those two seperately and then invoke the EJB from the application client ?? if so how can i do that ? are there any commands for that. ?


      I wud b much pleased if anybosy can help me on this..

      Thanks and Regards..
      Sajith


      PS : the code is attached with this..




      package searchfacadejboss;

      import saji.dev.ejb.jboss.stateless.SearchFacade;
      import java.util.List;
      import javax.ejb.EJB;

      public class Main {

      @EJB
      private static SearchFacade searchFacade;


      public Main() {
      }



      public static void main(String[] args) {
      Main searchFacadeTest = new Main();
      searchFacadeTest.doTest();
      }



      void doTest() {


      try {

      System.out.println("Search Facade Lookup");
      System.out.println("Searching wines");

      List winesList = searchFacade.wineSearch("Red");

      System.out.println("Printing wines list");
      for (String wine:(List)winesList ){
      System.out.println(wine);
      }
      }catch(Exception ex) {
      ex.printStackTrace();
      }
      }

      }



      The output
      ==========
      Search Facade Lookup
      Searching wines
      java.lang.NullPointerException
      at searchfacadejboss.Main.doTest(Main.java:46)
      at searchfacadejboss.Main.main(Main.java:31)
      run-searchFacadeJboss-app-client:
      run:
      BUILD SUCCESSFUL (total time: 3 seconds)

        • 1. Re: application clients in JBOSS 4.2.2
          Peter Johnson Master

          If you use annotations, and are happy with default settings, then no additional descriptors are necessary to deploy an EJB.

          Your code appears to be for a standalone client. In that case, you do not need an ear file. Instead, place the EJB jar file into the server/xxx/deploy directory. And include the client's jar file in the classpath when you run the client (you will also need the interface for the EJB in the classpath). Finally, I am not sure if the @EJB annotation works in remote clients in JBossAS 4.2.x, you might have to use 5.0 (this could explain the null pointer exception you are seeing).

          • 2. Re: application clients in JBOSS 4.2.2
            Sajith Kariyawasam Newbie

            thanks Peter ,

            u r the one who has replied for almost all my questions so far .. :)

            finally i got the problem solved ..

            when JBOSS integrates with Netbeans 6.0 , it automatically detects the settings to be conformed with J2EE 1.4 specs.not J2EE 5...so il try with JBOSS 5.0 later..

            so i had to edit the configuration files as well , and had to use JNDI as well.

            but the same application is working in Glassfish server , without the need of any modification of config files , and without using JNDI lookups..

            here , iam deploying the application client's jar also to the deploy directory , and after that clicks on RUN in Netbeans , so no classpath is involving here.Im that couldnt i use the @Local interface instead of the @Remote one ?

            another question , what s the use of @EJB annotations ? are those only for local clients ? won't that be supports for Local clients ?

            also , what is the use of the local interface (@Local) .. i couldnt find any example code which uses that interface..i tried even to use it..but it failed.so i gave that up..so please , if u have any simple example code which uses the @Local interface please send me ( through this or my mail sajhak@hotmail.com ) so that i can get a thorough idea of EJB ..

            Thanks and Regards
            Sajith

            • 3. Re: application clients in JBOSS 4.2.2
              Peter Johnson Master

              You can only use @Local if you call the ejb from a web application or another ejb. You are not doing that, you are calling it from a standalone client. And the IDE is hiding that fact from you, including setting the classpath. (This is one reason why every time I have to break in a new developer I have him, or her, do everything by hand with a basic text editor. Only after I am convinced that they know what they are doing and how things work and why they work that way will I let them use an IDE.)

              The @EJB annotation is used to inject an EJB reference into the code. It is designed to work in both standalone clients and clients deployed to an app server. But as I pointed out earlier, it might not work in standalone clients in 4.2.x.

              What tutorial or book are you using to learn EJB3?

              • 4. Re: application clients in JBOSS 4.2.2
                Sajith Kariyawasam Newbie

                Im refering the e-version of "begining EJB 3 application development - from

                novice to professional " - Apress publishers' and also , SUN's j2EE5 official

                tutorial.If u know abt any other recommended learning materials pls let me

                know..

                so in those materials i have found that @Local can be used when both the client

                application and the EJB are in the same JVM.. since i deplyoed my EJB and the

                client application as well to the JBOSS server , then does not those 2(EJB and

                client app) are in the same JVM.? ( the JVM of the JBOSS server instance ) if so

                , then both these components are running in the same JVM so that it must be

                possible to use the @Local interface..

                to another thread that i posted in this forum under EJB 3.0 category , i got

                replies saying that JBOSS 4.2.x is not fully compliant with EJB 3.0 and j2EE 5

                specs.SO i feel that i cant achive the tasks with out using JNDI lookups( if app

                server is JBOSS )...
                i.e , use the following code segment,



                @EJB
                private static ObjectType obj;

                InitialContext ic = new InitialContext ();
                obj = (ObjectType) ic.lookup(ObjectType.class.getName());




                i can t depend only on @EJB annotations..

                is that right ?? , because the example in the book havent use any JNDI lookup s

                and it works fine with GlassFish - not with JBOSS.

                Thanks n Rgds
                Sajith

                • 5. Re: application clients in JBOSS 4.2.2
                  Peter Johnson Master

                  I already answered these questions in my previous posts. In those posts I explained why @Local and @EJB are not working the way you want them to. But I will try to answer the questions again, though a little differently.

                  I would not expect @EJB to work until JBossAS 5.0. That version is fully Java EE 5 compliant.

                  Regrading @Local, you did *not* deploy your client to JBossAS. Copying a jar file that contains standalone client code to the serer/default/deploy directory (or pushing a button in an IDE which does this) does not mean that the application is deployed to the app server. By "standalone client", I mean a Java program that has a class with a main() method, which can only be run by starting up another JVM and running that Java program. And that is what your client is - it has a main() method, the only way to run the client is to start a JVM and tell the JVM to execute Main.main(). This means that your cannot use @Local because the client is not running in the same JVM as the app server. And all of this is being hidden from you by the IDE. (See my earlier discussion on why I don't let new developers use IDEs. )

                  • 6. Re: application clients in JBOSS 4.2.2
                    Sajith Kariyawasam Newbie

                    thanks for the detailed description PeterJ ,

                    so i thought of trying EJB stuff with a little support of the IDE this time.But it gives me lot of troubles.I ll list the steps i followed to test my EJB , so that will be easy for u to help me on this issue.

                    * My EJB (Bean class and the Remote interface) is packaged into a .jar archive including its ejb-jar.xml file and jboss.xml file ( i have attached both files for your reference ).

                    * My application client ( only the application's main class ) in archived into a .jar .

                    * Both the jars are copied into the JBOSS deploy directory.

                    * Set the classpath to both the .jar archives.

                    but when i run the app client from the command line i get the follwing exception ...


                    C:\Documents and Settings\saji>java dev.saji.ejb.client.SearchNamesClient

                    javax.naming.NoInitialContextException: Need to specify class name in environmen
                    t 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 dev.saji.ejb.client.SearchNamesClient.doTest(SearchNamesClient.java:33)
                    at dev.saji.ejb.client.SearchNamesClient.main(SearchNamesClient.java:24)


                    do i have to package the Remote interface with the application client as well ?
                    if you have , please give me some references about EJB or j2EE that you recommends .


                    PS : here s the config files ..

                    ejb-jar.xml
                    ===========
                    <?xml version="1.0" encoding="UTF-8"?>
                    <ejb-jar xmlns="http://java.sun.com/xml/ns/javaee"
                    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
                    xsi:schemaLocation="http://java.sun.com/xml/ns/javaee
                    http://java.sun.com/xml/ns/javaee/ejb-jar_3_0.xsd"
                    version="3.0">
                    <enterprise-beans>

                    <ejb-name>SearchNames</ejb-name>
                    <ejb-class>dev.saji.ejb.ejb.beans.SearchNamesBean</ejb-class>

                    </enterprise-beans>
                    </ejb-jar>




                    jboss.xml
                    =========
                    <?xml version="1.0"?>
                    <!DOCTYPE jboss PUBLIC "-//JBoss//DTD JBOSS 4.2//EN"
                    "http://www.jboss.org/j2ee/dtd/jboss_4_2.dtd">

                    <enterprise-beans>

                    <ejb-name>SearchNames</ejb-name>
                    <jndi-name>dev.saji.ejb.ejb.SearchNames</jndi-name>

                    </enterprise-beans>





                    • 7. Re: application clients in JBOSS 4.2.2
                      jaikiran pai Master

                       

                      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


                      You will need a jndi.properties file on the client side. See this for more details http://wiki.jboss.org/wiki/Wiki.jsp?page=Getquotjavax.naming.NoInitialContextExceptionquot



                      • 8. Re: application clients in JBOSS 4.2.2
                        Sajith Kariyawasam Newbie

                        hi jaikiran ,

                        i placed the jndi.properties file in client jar , but now it gives the following exception ...


                        C:\Documents and Settings\saji>java dev.saji.ejb.client.SearchNamesClient
                        javax.naming.NoInitialContextException:
                        Cannot instantiate class: org.jnp.interfaces.NamingContextFactory
                        [Root exception is java.lang.ClassNotFoundException: org.jnp.interfaces.NamingContextFactory]
                        at javax.naming.spi.NamingManager.getInitialContext(Unknown Source)
                        at javax.naming.InitialContext.getDefaultInitCtx(Unknown Source)
                        at javax.naming.InitialContext.init(Unknown Source)
                        at javax.naming.InitialContext.(Unknown Source)
                        at dev.saji.ejb.client.SearchNamesClient.doTest(SearchNamesClient.java:32)
                        at dev.saji.ejb.client.SearchNamesClient.main(SearchNamesClient.java:24)

                        Caused by: java.lang.ClassNotFoundException: org.jnp.interfaces.NamingContextFactory
                        at java.net.URLClassLoader$1.run(Unknown Source)
                        at java.security.AccessController.doPrivileged(Native Method)
                        at java.net.URLClassLoader.findClass(Unknown Source)
                        at java.lang.ClassLoader.loadClass(Unknown Source)
                        at sun.misc.Launcher$AppClassLoader.loadClass(Unknown Source)
                        at java.lang.ClassLoader.loadClass(Unknown Source)
                        at java.lang.ClassLoader.loadClassInternal(Unknown Source)
                        at java.lang.Class.forName0(Native Method)
                        at java.lang.Class.forName(Unknown Source)
                        at com.sun.naming.internal.VersionHelper12.loadClass(Unknown Source)
                        ... 6 more

                        C:\Documents and Settings\saji>

                        • 9. Re: application clients in JBOSS 4.2.2
                          jaikiran pai Master

                          In your client's classpath place the jbossall-client.jar file (which you can find in %JBOSS_HOME%/client folder).

                          • 10. Re: application clients in JBOSS 4.2.2
                            Sajith Kariyawasam Newbie

                            thanks JaiKiran..

                            now it works fine..

                            i really appreciate all of you guys' help..