You are viewing an old version of this page. View the current version.

Compare with Current View Page History

« Previous Version 45 Next »

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 6.0+ (J2SE 1.6)
  2. Eclipse IDE for Java EE Developers, which is platform specific
  3. Apache Geronimo Eclipse Plugin 2.1.x
  4. Apache Geronimo Server 2.1.x

    Geronimo version 2.1.x, Java 1.5 runtime, and Eclipse Ganymede are 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:

Let us briefly understand this application. This application will take you through creating a simple Stateless Session EJB. Later we will develop a Java Application client to access this EJB. EJB development will make use of annotations which are introduced in Java EE5.

Setting up Eclipse IDE for EJB application development

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


This completes the setting of Eclispe IDE for EJB application development.

Developing EJB local interface, EJB remote interface and Bean class

  1. Right 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
    CountryCapital.java
    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.
    CountryCapitalLocal.java
    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 the Remote as well as the 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 the Bean class and will not be visible to application client.

  5. The next step is to create a Bean class which will implement the Business method to be executed. Right click on the ejb package and create a new class.




  6. Enter the class name as CountryCapitalBean.




  7. Populate the bean class with the code as follows
    CountryCapitalBean.java
    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="Beijing";
    		}
    
    		return capital;
    
    	}
    
    }
    

This completes the development of EJB application.

Developing a Java Client

  1. Select File -> New -> Project -> JavaEE -> Application Client Project. Select Next.




  2. In the New Project Window give the Project name as 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.




    Why did we add the EJB project to build path ??

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


  6. Right click on ApplicationClient project and create a new Java Class as shown in the following figure:




  7. Give the package name as appclient and the class name as ApplicationClient. Select Finish.

  8. Populate the ApplicationClient.java class with the following code:
    ApplicationClient.java
    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:
    CodeSnippet
    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");
    

    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.

    Why is the lookup name CountryCapitalBeanRemote ??

    This will be discussed in deploy and run section.


  9. The 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 and select the jar openejb-client-3.0.jar. Select OK.

This completes the Application Client Development.

Deploy and run

This section will take you through the deployment of the EJB application. Later we will run the application client with some sample inputs.

  1. Right click on the Geronimo server in the Java EE perspective and select Add and Remove Projects....
  2. There should be one deployable project in your workspace. Select Add All >> and then select Finish.
  3. Once the deployment is successful open <GERONIMO_HOME>/var/log/geronimo.log and find the following:
    Snippet from geronimo.log after deployment of SimpleEJB
    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)
    
    The OpenEJB container is now aware of both the Remote as well as the Local lookup names. In this tutorial the SimpleEJB application and Java Application Client are running under different JVM instances, so we need to have the CountryCapitalBeanRemote as the lookup name for remote access.
  4. Under Eclipse Right click on the Application Client project and Run as a Java Application.

Sample Input #1: India




Sample Input #2: China




Sample Input #3: United States Of America




Sample Input #4: France


  • No labels