Home > Documentation > Developer's guide > Tutorials > Application clients > Developing an Application Client to access EJB |
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.
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.
This completes the setting of Eclispe IDE for EJB application development.
package ejb; import javax.ejb.Remote; @Remote public interface CountryCapital { public String capitalName(String countryName); }
package ejb; import javax.ejb.Local; @Local public interface CountryCapitalLocal { public String capitalName(String countryName); }
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; } }
This completes the development of EJB application.
Warning
Due to some limitations in Geronimo Eclipse Plugin the generated deployment plan(openejb-jar.xml) does not have the correct namespace. Replace the existing namespace as shown in the figure with the following
<openejb-jar xmlns="http://www.openejb.org/xml/ns/openejb-jar-2.2" xmlns:nam="http://geronimo.apache.org/xml/ns/naming-1.2" xmlns:pkgen="http://www.openejb.org/xml/ns/pkgen-2.0" xmlns:sec="http://geronimo.apache.org/xml/ns/security-1.2" xmlns:sys="http://geronimo.apache.org/xml/ns/deployment-1.2">
Why 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.
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(); } } }
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");
Useful 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.
Why is the lookup name CountryCapitalBeanRemote?? :(
This will be discussed in deploy and run section.
This completes the Application Client Development.
This section will take you through the deployment of the EJB application. Later we will run the application client with some sample inputs.
Why 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.
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)
Sample Input1: India
Sample Input2: China
Sample Input3: United States Of America
Sample Input4: France
Bookmark this on Delicious Digg this | Privacy Policy - Copyright © 2003-2011, The Apache Software Foundation, Licensed under ASL 2.0. |