|
©1994-2003 Kevin Boone |
| Home Section index K-Zone home |
|
com
web_tomorrow
interest
{java source and class files here}
If you unpack the archive interestEJB.tar.gz it will create this
structure automatically.
EJBs: review As a reminder, and Enterprise JavaBean has a minimum of three classes.
com.web_tomorrow.interest, so they need to be in the directory
./com/web_tomorrow/interest/where the `.' represents the current working directory, wherever that is. You will also need a directory called META-INF to store the
deployment descriptor (always called ejb-jar.xml) and -- optionally -- another
XML file to tell the server about name mappings. With jBoss, this file must be
called jboss.xml.
So before writing the classes, we need a directory structure like this:
com
web_tomorrow
interest
{java source and class files here}
META-INF
ejb-jar.xml
jboss.xml
If the jar utility is run at the top level of this directory
structure, it will put the files in the right ordering in the archive. We will
discuss the creation of the XML files later.
Of course, in your own work you will create a directory hierarchy that reflect the package hierarchy you need, rather than `com.web_tomorrow.XXXX' Coding the classes
We need three classes: the remote interface, the Bean, and the home interface.
All the .java files will go in the subdirectory
Interest.java: remote interface for the `interest' EJB
package com.web_tomorrow.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 remote interface specifies only one `business method' calculateCompoundInterest.
The home interface is even simpler.
Interest.java: remote interface for the `interest' EJB
package com.web_tomorrow.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;
}
Finally, here is the Bean class. This is the only one that does any real work in this simple example.
Interest.java: remote interface for the `interest' EJB
package com.web_tomorrow.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;
}
/**
Empty method body
*/
public InterestBean() {}
/**
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) {}
}
Notice that most of the methods are empty; they have to exist because they're specified by the SessionBean interface, but they don't need to do anything in this case. If you haven't already done so, you should create these .java files in the directory ./com/web_tomorrow/interest (or unpack the archive with
them in). Then you can compile them using the command
javac -classpath /usr/lib/jboss/lib/ext/ejb.jar com/web_tomorrow/interest/*.javasubstituting the correct path to the jBoss class EJB library if you haven't installed jBoss in /usr/lib/jboss. This should create three class
files: InterestBean.class, Interest.class,
and InterestHome.class. If you have `make' on your system, and you
have unpacked the archive, you can build all the classes just be running
make in the same directory as the Makefile.
With the classes compiled, it's time to create the deployment descriptor. |