Versions Compared

Key

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

...

This tutorial walks you through configuring, developing and deploying an enterprise application with Eclipse and Geronimo. To run this tutorial, as a minimum you will be required to have installed the following prerequisite software.

  1. Sun JDK

...

  1. 6.0+ (J2SE 1.

...

  1. 6)
  2. Eclipse

...

  1. IDE for Java EE Developers, which is platform specific

...

  1. Apache Geronimo Eclipse Plugin 2.

...

  1. 1.

...

  1. x
  2. Apache Geronimo Server 2.1.x
    Note

    Geronimo version 2.1.x, Java 1.5 runtime, and Eclipse Ganymede are used is used in this tutorial but other versions can be used instead (e.g., Geronimo version 2.2, Java 1.6, Eclipse Europa)

...

Details on installing eclipse are provided in the Development environment section. This tutorial is organized in the following sections:

...

  1. Launch Eclipse and select Window ->Open -> Open Perspective ->Other->JavaEE -> Other --> JavaEE and select Ok OK




  2. Right Click -click under Project Explorer and create a new EJB project. Mention the fields as shown in the figure. For all the other windows give default values and select Finish.





  3. Under Project Explorer, Right-click on SimpleEJB and create a new package ejb. Mention the fields as shown in the figure. Select Finish.





  4. Right-click on SimpleEJB project under project explorer and select properties Properties.





  5. Select Java Build Path and select Add External JARs.





  6. Browse to the directory <GERONIMO_HOME>\repository\org\apache\geronimo\specs\geronimo-ejb_3.0_spec\1.0.1 and select geronimo-ejb_3.0_spec-1.0.1.jar.





  7. Once done select Ok. You can see the geronimo-ejb_3.0_spec-1.0.1.jar added to the build path. This jar provides the essential classes required for the EJB development.





...

Developing EJB Local interface, EJB Remote Interface and Bean Class

  1. Right Click -click on the ejb package and create a new Remote interface CountryCapital as shown in the figure





  2. Enter the fields as shown in the figure and select Finish.





  3. Add the following code as shown below
    Code Block
    titleCountryCapital.java
    borderStylesolid
    package ejb;
    
    import javax.ejb.Remote;
    @Remote
    public interface CountryCapital {
    	public String capitalName(String countryName);
    }
    
    Here @Remote is an annotation used to declare the interface as a Remote Interface.
  4. Similarly create a Local interface CountryCapitalLocal. Add the following code to the interface
    Code Block
    titleCountryCapitalLocal.java
    borderStylesolid
    package ejb;
    
    import javax.ejb.Local;
    @Local
    public interface CountryCapitalLocal {
    	public String capitalName(String countryName);
    
    }
    

    Similarly in this code we have @Local annotation which declares this interface as local.

    In remote as well as local interface we have the declaration for only one Business method. So only this method will be visible to the application client. Methods other than capitalName if implemented in the Bean class will be private to Bean class and will not be visible to application client.

  5. Next step is to create a Bean Class which will implement the Business method to be executed. Right Click -click on the jsf package and create a new class.





  6. Enter the class name as CountryCapitalBean.





  7. Populate the bean class with the code as follows
    Code Block
    titleCountryCapitalBean.java
    borderStylesolid
    package ejb;
    
    import javax.ejb.Stateless;
    @Stateless
    public class CountryCapitalBean implements CountryCapital,CountryCapitalLocal{
    	public String capitalName(String countryName)
    	{
    		String capital=new String("No such country");
    		if (countryName.equalsIgnoreCase("India"))
    		{
    			capital="New Delhi";
    		}
    		if (countryName.equalsIgnoreCase("United States Of America"))
    		{
    			capital="Washington DC";
    		}
    		if (countryName.equalsIgnoreCase("China"))
    		{
    			capital="Bejing";
    		}
    
    		return capital;
    
    	}
    
    }
    

...

Developing a Java Client

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





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





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



  4. Check the box for SimpleEJB and Select 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.


    !


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


    !


  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);
    CountryCapital myejb =(CountryCapital)context.lookup("CountryCapitalBeanRemote");
    

    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.


    Tip
    titleWhy is the lookup name CountryCapitalBeanRemote?? :(

    This will be discussed in deploy and run section.

  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.





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





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

This completes the Application Client Development.

...

  1. To deploy the EJB application on to server Export the EJB application as a jar file. Right Click -click on SimpleEJB application and Select select Export -->EJB > EJB JAR File as shown in the figure.





  2. Browse to the Destination of your choice and Select select Finish. This will export the SimpleEJB application as a jar Jar file. This jar Jar will be later deployed on to Geronimo server
    Warning
    titleWhy to Export EJB? Why cannot I use Eclipse for deploying the EJB application on the server?

    Due to some limitation Eclipse is not able to deploy the EJB application on to server. This issue will be fixed very soon.


  3. Start the server and Launch the Administrative Console. Under Applications, Select select Deploy New and Deploy the SimpleEJB.jar on the server.
  4. Once the Deployment is successful. Open <GERONIMO_HOME>/var/log/server.log. Find the following in server.log
    Code Block
    titleSnippet from server.log after deployment of SimpleEJB
    borderStylesolid
    18:16:39,750 INFO  [startup] Jndi(name=CountryCapitalBeanLocal) --> Ejb(deployment-id=SimpleEJB/CountryCapitalBean)
    18:16:39,750 INFO  [startup] Jndi(name=CountryCapitalBeanRemote) --> Ejb(deployment-id=SimpleEJB/CountryCapitalBean)
    
    As can be seen openEJB container after deployment suggests the Remote as well as Local lookup names.
    In this case since the SimpleEJB application and Java Application Client are running on different instances of JVM. We need to have the CountryCapitalBeanRemote as the lookup name.
  5. Under Eclipse Right Click -click on the Application Client project and Run as a Java application.

...