Download EJBs

Survey
yes no Was this document useful for you?
   Thank you for your participation!

* Your assessment is very important for improving the workof artificial intelligence, which forms the content of this project

Document related concepts
no text concepts found
Transcript
A TUTORIAL TO USING EJBs
by
SHREERAM IYER
09/17/2001
ENVIRONMENT & TOOLS
Application Server … JBOSS
Web Server/ Servlet Engine … Jetty/Tomcat
Build tool … Ant
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
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
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
Home Interface
InterestHome.java
package org.jboss.docs.interest;
import
import
import
import
java.io.Serializable;
java.rmi.RemoteException;
javax.ejb.CreateException;
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;
}
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;}
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;
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)
{}
}
EJB Client…
InterestClient.java
package org.jboss.docs.interest;
//
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");
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());
}
}
}
The Deployment Descriptor
(DD)
ejb-jar.xml
<?xml version="1.0" encoding="UTF-8"?>
<ejb-jar>
<description>JBoss Interest Sample Application</description>
<display-name>Interest EJB</display-name>
<enterprise-beans>
<session>
<ejb-name>Interest</ejb-name>
<home>org.jboss.docs.interest.InterestHome</home>
<remote>org.jboss.docs.interest.Interest</remote>
<ejb-class>org.jboss.docs.interest.InterestBean</ejb-class>
<session-type>Stateless</session-type>
<transaction-type>Bean</transaction-type>
</session>
</enterprise-beans>
</ejb-jar>
Deployment Descriptor
<?xml version="1.0" encoding="UTF-8"?>
<jboss>
<enterprise-beans>
<session>
<ejb-name>Interest</ejb-name>
<jndi-name>interest/Interest</jndi-name>
</session>
</enterprise-beans>
</jboss>
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.RC81\jboss
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
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