©1994-2003 Kevin Boone
Home     Section index     K-Zone home

Site search

Glossary
Confused by computer jargon? Look it up!

Shameless plug


Now available!

Articles
- Ten-minute guide to setting up a WAP site

- Talk like your boss: new developments in managerese

More...

Development
File handling in the Linux kernel

Java development for the Sony-Ericsson P800

SunONE Application Server 7 FAQ

More...

Linux
Using Linux with the Treo 600

- Linux on the Tecra M1

- Some notes on openzaurus

More...

Download
Java stuff

Linux stuff

More...

(Please read the download policy)

Home automation
The X10 system

Linux TW723 driver

More...

The K-Zone
K-Zone computing

K-Zone law

K-Zone education and science

K-Zone motorcycles

K-Zone DIY

K-Zone railways

K-Zone martial arts

About the author

K-Zone home page

 
Step 1: installing jBoss
contents
Step 3: the deployment descriptor
Step 2: creating the Bean

n this step we will write and compile a simple Enterprise JavaBean. You can download the source code interestEJB.tar.gz for this example; you will need to unpack it into an empty directory. The example -- which is called `Interest' -- is about as simple as an EJB can get: it is a `stateless session bean'. Its job is to calculate the amount of compound interest payable on a sum of money borrowed over a specified term with a specified interest rate. In fact, there is only one functional line of code in the whole package.
       If you want to compile the classes yourself, you'll need to create a directory hierarchy that reflects the structure of the pacakage. The package in this example is com.web_tomorrow.interest so you'll need to create the directory structure:

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.

  • The remote interface. This is the class that exposes the methods of the Bean to the outside world. In the example, the remote interface is the class com.web_tomorrow.interest.Interest
  • The Bean class. This implements the methods specified by the remote interface. In this example, the Bean class is com.web_tomorrow.interest.InterestBean
  • The home interface. This specifies how a new Bean is created, managed and deleted. As a minimum it should specify at least one create() method. There should be an ejbCreate() method in the Bean class for each create() method in the home interface. In this example, the home interface is com.web_tomorrow.InterestHome
Of course, a Bean can include other classes, or even other packages, but the classes listed above are the minimum. The classes must be packaged into a JAR archive with a directory structure that reflects the hierarchy of packages. In the example, the classes are in the package 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 ./com/web_tomorrow/interest
       The remote interface in this example is very simple.

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/*.java
substituting 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.