Review the EJB Classes

We need three classes: the remote interface, the home interface, and the bean implementation. The remote interface in this example is very simple and given in the following figure.

Figure 1.3. Remote interface for the interest EJB, file name 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 remote interface specifies only one `business method' calculateCompoundInterest. The home interface is even simpler and is given in the next figure.

Figure 1.4. Home interface for the interest EJB, file name 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;
}

Finally, the bean implementation class is given in the next figure.

Figure 1.5. Implementation class for the interest EJB, file name 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;
   }

   /** Empty method body
    */
   public void ejbCreate()
   {}
   /** Every ejbCreate() method ALWAYS needs a corresponding 
       ejbPostCreate() method with exactly the same parameter types.
    */
   public void ejbPostCreate()
   {}
   /** 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 not needed for this simple example EJB.