Versions Compared

Key

  • This line was added.
  • This line was removed.
  • Formatting was changed.

...

  1. Select File->New->Project->Java->Java Project. Select Next.





  2. In the New Project Window give the Project name ss ApplicationClient and select Next.





  3. On the Java Settings window. Select the Projects tab and click add.



  4. Check the box for SimpleEJB and Select OK.


    !


  5. This will add the SimpleEJB project to the build path of the Application Client as shown in the figure. Select Finish.


    !


    Tip
    titleWhy to add EJB project to build path :(

    This is because the build path is used to find the classes referenced by your Client source code. These classes will be required for compilation of Client source code.

  6. Right click on package appclient and create a new Java Class.


    Image Added!


  7. Give the class name as ApplicationClient as shown in the figure. Select Finish.


    Image Added!


  8. Populate the ApplicationClient.java with the following code
    Code Block
    titleApplicationClient.java
    borderStylesolid
    
    package appclient;
    
    import java.io.BufferedReader;
    import java.io.InputStreamReader;
    import java.util.Properties;
    import ejb.CountryCapital;
    import javax.naming.Context;
    import javax.naming.InitialContext;
    
    public class ApplicationClient {
    	 public static void main(String [] args)
    	 {
    		 String capital=new String();
    		 try{
    			 Properties prop=new Properties();
    			 prop.put(Context.INITIAL_CONTEXT_FACTORY, "org.apache.openejb.client.RemoteInitialContextFactory");
    				prop.put("java.naming.provider.url", "ejbd://localhost:4201");
    	 Context context = new InitialContext(prop);
    	 
    	 CountryCapital myejb =
    	 (CountryCapital)context.lookup("CountryCapitalBeanRemote");
    	 System.out.println("Give the name of a country");
    	 BufferedReader in= new BufferedReader(new InputStreamReader(System.in));
    	 String str="";
    	 str=in.readLine();
    	 capital=myejb.capitalName("str");
    	 System.out.println(capital);
    		 }
    		 catch(Exception e)
    		 {
    			 e.printStackTrace();
    		 }
    	 }
    	
    
    }
    
    

    Let us try to understand following code snippet
    Code Block
    titleCodeSnippet
    borderStylesolid
    
    Properties prop=new Properties();
    prop.put(Context.INITIAL_CONTEXT_FACTORY, "org.apache.openejb.client.RemoteInitialContextFactory");
    prop.put("java.naming.provider.url", "ejbd://localhost:4201");
    Context context = new InitialContext(prop);
    

    Info
    titleUseful Information

    The above code suggests that which InitialContextFactory to be used to create the InitialContext. Here we are using org.apache.openejb.client.RemoteInitialContextFactory to create the InitialContext.
    In this case as the EJB server is the naming service provider and has naming service runnning at Port 4201.
    So we need to specify the location of the EJB server. In this case it is ejbd://localhost:4201.
    Once this is done we have the context that provides us the ability to lookup and get objects.


  9. Next step is to add org.apache.openejb.client.RemoteInitialContextFactory Class to the build path. This is because the application client needs this Class to create the InitialContext as explained above.
  10. Right click on ApplicationClient project under project explorer. Select Properties.


    Image Added


  11. Select Java Build Path. Under Java Build Path select Libraries-> Add External JARs.


    Image Added


  12. Browse to <GERONIMO_HOME>\repository\org\apache\openejb\openejb-client\3.0-beta-2 and Select the jar openejb-client-3.0-beta-2.jar. Select Ok.

This completes the Application Client Development.

This completes the development of the Java application client.