A TUTORIAL TO USING EJBs - PowerPoint PPT Presentation

1 / 16
About This Presentation
Title:

A TUTORIAL TO USING EJBs

Description:

Calulates the compound interest on the sum `principle', with interest rate per ... call the calculateCompoundInterest() method to do the calculation ... – PowerPoint PPT presentation

Number of Views:61
Avg rating:3.0/5.0
Slides: 17
Provided by: ski89
Category:

less

Transcript and Presenter's Notes

Title: A TUTORIAL TO USING EJBs


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,

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)

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.intere
    st'. 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

11
EJB Client
  • // Get a reference to the Interest Bean
  • Object ref jndiContext.lookup("interes
    t/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.calculateComp
    oundInterest(1000, 0.10, 2))
  • catch(Exception e)
  • System.out.println(e.toString())

12
The Deployment Descriptor (DD)
  • ejb-jar.xml
  • lt?xml version"1.0" encoding"UTF-8"?gt
  • ltejb-jargt
  • ltdescriptiongtJBoss Interest Sample
    Applicationlt/descriptiongt
  • ltdisplay-namegtInterest EJBlt/display-namegt
  • ltenterprise-beansgt
  • ltsessiongt
  • ltejb-namegtInterestlt/ejb-namegt
  • lthomegtorg.jboss.docs.interest.InterestHom
    elt/homegt
  • ltremotegtorg.jboss.docs.interest.Interestlt
    /remotegt
  • ltejb-classgtorg.jboss.docs.interest.Intere
    stBeanlt/ejb-classgt
  • ltsession-typegtStatelesslt/session-typegt
  • lttransaction-typegtBeanlt/transaction-typegt
  • lt/sessiongt
  • lt/enterprise-beansgt
  • lt/ejb-jargt

13
Deployment Descriptor
  • lt?xml version"1.0" encoding"UTF-8"?gt
  • ltjbossgt
  • ltenterprise-beansgt
  • ltsessiongt
  • ltejb-namegtInterestlt/ejb-namegt
  • ltjndi-namegtinterest/Interestlt/jndi-namegt
  • lt/sessiongt
  • lt/enterprise-beansgt
  • lt/jbossgt

14
ROLLING THE BALL
  • SET ANT_HOMEY\Ant\Jakarta-ant-1.3
  • SET JBOSS_HOMEY\JBoss-2.4.0_Jetty-3.1.RC8-1\JBos
    s-2.4.0_Jetty-3.1.RC8-1
  • SET JAVA_HOMEC\jdk1.3.1
  • SET PATHPATHANT_HOME\bin
  • SET JBOSS_DISTY\JBoss-2.4.0_Jetty-3.1.RC8-1\JBos
    s-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
Write a Comment
User Comments (0)
About PowerShow.com