Presentation is loading. Please wait.

Presentation is loading. Please wait.

A TUTORIAL TO USING EJBs by SHREERAM IYER 09/17/2001.

Similar presentations


Presentation on theme: "A TUTORIAL TO USING EJBs by SHREERAM IYER 09/17/2001."— Presentation transcript:

1 A TUTORIAL TO USING EJBs by SHREERAM IYER 09/17/2001

2 ENVIRONMENT & TOOLS Application Server … JBOSS Web Server/ Servlet Engine … Jetty/Tomcat Build tool … Ant

3 JBOSS Implementation of EJB 1.1 Open Source endeavor In-built SQL database Server to handle persistent beans Very lightweight Written completely in Java Requires Java System compatible with JDK 1.3

4 JETTY Open Source, 100% Java HTTP Servlet Server Lightweight Extensible Implements HTTP 1.1, Servlet API 2.2 and JSP 1.1 standards Jetty integrated into JBOSS

5 ANT Standard build tool used with most open source Java projects Java based build tool Configuration files are XML-based Calls out target trees where various tasks are executed Build.xml

6 Home Interface InterestHome.java package org.jboss.docs.interest; import java.io.Serializable; import java.rmi.RemoteException; import javax.ejb.CreateException; import javax.ejb.EJBHome; /** This interface defines the `home' interface for the `Interest' EJB. */ public interface InterestHome extends EJBHome { /** Creates an instance of the `InterestBean' class on the server, and returns a remote reference to an Interest interface on the client. */ Interest create() throws RemoteException, CreateException; }

7 Remote Interface Interest.java package org.jboss.docs.interest; import javax.ejb.EJBObject; import java.rmi.RemoteException; /** This interface defines the `Remote' interface for the `Interest' EJB. Its single method is the only method exposed to the outside world. The class InterestBean implements the method. */ public interface Interest extends EJBObject { /** Calulates the compound interest on the sum `principle', with interest rate per period `rate' over `periods' time periods. This method also prints a message to standard output; this is picked up by the EJB server and logged. In this way we can demonstrate that the method is actually being executed on the server, rather than the client. */ public double calculateCompoundInterest(double principle, double rate, double periods) throws RemoteException;}

8 The EJB… InterestBean.java package org.jboss.docs.interest; import java.rmi.RemoteException; import javax.ejb.SessionBean; import javax.ejb.SessionContext; /** This class contains the implementation for the `calculateCompoundInterest' method exposed by this Bean. It includes empty method bodies for the methods prescribe by the SessionBean interface; these don't need to do anything in this simple example.*/ public class InterestBean implements SessionBean { /** Calulates the compound interest on the sum `principle', with interest rate per period `rate' over `periods' time periods. This method also prints a message to standard output; this is picked up by the EJB server and logged. In this way we can demonstrate that the method is actually being executed on the server, rather than the client. */ public double calculateCompoundInterest(double principle, double rate, double periods) { System.out.println("Someone called `calculateCompoundInterest!'"); return principle * Math.pow(1+rate, periods) - principle; }

9 The EJB /** Empty method body */ public void ejbCreate() {} /** Empty method body */ public void ejbRemove() {} /** Empty method body */ public void ejbActivate() {} /** Empty method body */ public void ejbPassivate() {} /** Empty method body */ public void setSessionContext(SessionContext sc) {} }

10 EJB Client… package org.jboss.docs.interest; // InterestClient.java import javax.naming.InitialContext; import javax.rmi.PortableRemoteObject; import org.jboss.docs.interest.Interest; import org.jboss.docs.interest.InterestHome; /** This simple application tests the 'Interest' Enterprise JavaBean which is implemented in the package 'org.jboss.docs.interest'. For this to work, the Bean must be deployed on an EJB server.*/ class InterestClient { /** This method does all the work. It creates an instance of the Interest EJB on the EJB server, and calls its `calculateCompoundInterest()' method, then prints the result of the calculation.*/ public static void main(String[] args) { // Enclosing the whole process in a single `try' block is not an ideal way // to do exception handling, but I don't want to clutter the program up // with catch blocks try { // Get a naming context InitialContext jndiContext = new InitialContext(); System.out.println("Got context");

11 EJB Client // Get a reference to the Interest Bean Object ref = jndiContext.lookup("interest/Interest"); System.out.println("Got reference"); // Get a reference from this to the Bean's Home interface InterestHome home = (InterestHome) PortableRemoteObject.narrow(ref, InterestHome.class); // Create an Interest object from the Home interface Interest interest = home.create(); // call the calculateCompoundInterest() method to do the calculation System.out.println("Interest on 1000 units, at 10% per period, compounded over 2 periods is:"); System.out.println(interest.calculateCompoundInterest(1000, 0.10, 2)); } catch(Exception e) { System.out.println(e.toString()); }

12 The Deployment Descriptor (DD) ejb-jar.xml JBoss Interest Sample Application Interest EJB Interest org.jboss.docs.interest.InterestHome org.jboss.docs.interest.Interest org.jboss.docs.interest.InterestBean Stateless Bean

13 Deployment Descriptor Interest interest/Interest

14 ROLLING THE BALL SET ANT_HOME=Y:\Ant\Jakarta-ant-1.3 SET JBOSS_HOME=Y:\JBoss-2.4.0_Jetty-3.1.RC8-1\JBoss-2.4.0_Jetty-3.1.RC8-1 SET JAVA_HOME=C:\jdk1.3.1 SET PATH=%PATH%;%ANT_HOME%\bin SET JBOSS_DIST=Y:\JBoss-2.4.0_Jetty-3.1.RC8-1\JBoss-2.4.0_Jetty-3.1.RC8- 1\jboss

15 STEPS TO BE TAKEN Set up all the environment variables Start the AppServer using the command run jetty Compile the java files Make the Deployment Descriptor Deploy the EJB Run the Application

16 Useful links www.jboss.org http://jakarta.apache.org http://jakarta.apache.org/ant/ http://jetty.mortbay.com/ http://jakarta.apache.org/tomcat/ http://java.sun.com/products/ejb


Download ppt "A TUTORIAL TO USING EJBs by SHREERAM IYER 09/17/2001."

Similar presentations


Ads by Google