Download
FAQ
History
PrevHomeNext API
Search
Feedback
Divider

Creating the Enterprise Bean

The enterprise bean in our example is a stateless session bean called ConverterBean. The source code for ConverterBean is in the <INSTALL>/j2eetutorial14/examples/ejb/converter/src/ directory.

Creating ConverterBean requires these steps:

  1. Coding the bean's interfaces and class (the source code is provided)
  2. Compiling the source code with asant
  3. With deploytool, packaging the bean into an EJB JAR file and inserting the EJB JAR file into the application's ConverterApp.ear file

Coding the Enterprise Bean

The enterprise bean in this example needs the following code:

Coding the Remote Interface

A remote interface defines the business methods that a client can call. The business methods are implemented in the enterprise bean code. The source code for the Converter remote interface follows.

import javax.ejb.EJBObject;
import java.rmi.RemoteException;
import java.math.*;

public interface Converter extends EJBObject {
   public BigDecimal dollarToYen(BigDecimal dollars) 
      throws RemoteException;
   public BigDecimal yenToEuro(BigDecimal yen) 
      throws RemoteException;
} 

Coding the Home Interface

A home interface defines the methods that allow a client to create, find, or remove an enterprise bean. The ConverterHome interface contains a single create method, which returns an object of the remote interface type. Here is the source code for the ConverterHome interface:

import java.rmi.RemoteException;
import javax.ejb.CreateException;
import javax.ejb.EJBHome;

public interface ConverterHome extends EJBHome {
  Converter create() throws RemoteException, CreateException;
} 

Coding the Enterprise Bean Class

The enterprise bean class for this example is called ConverterBean. This class implements the two business methods (dollarToYen and yenToEuro) that the Converter remote interface defines. The source code for the ConverterBean class follows.

import java.rmi.RemoteException; 
import javax.ejb.SessionBean;
import javax.ejb.SessionContext;
import java.math.*;

public class ConverterBean implements SessionBean {

  BigDecimal yenRate = new BigDecimal("121.6000");
  BigDecimal euroRate = new BigDecimal("0.0077");

  public BigDecimal dollarToYen(BigDecimal dollars) {
    BigDecimal result = dollars.multiply(yenRate);
    return result.setScale(2,BigDecimal.ROUND_UP);
  }

   public BigDecimal yenToEuro(BigDecimal yen) {
      BigDecimal result = yen.multiply(euroRate);
      return result.setScale(2,BigDecimal.ROUND_UP);
   }

   public ConverterBean() {}
   public void ejbCreate() {}
   public void ejbRemove() {}
   public void ejbActivate() {}
   public void ejbPassivate() {}
   public void setSessionContext(SessionContext sc) {}
} 

Compiling the Source Files

Now you are ready to compile the remote interface (Converter.java), the home interface (ConverterHome.java), and the enterprise bean class (ConverterBean.java).

  1. In a terminal window, go to this directory:
  2. <INSTALL>/j2eetutorial14/examples/ejb/converter/

  3. Type the following command:
  4. asant build

This command compiles the source files for the enterprise bean and the application client, placing the class files in the converter/build subdirectory (not the src directory). The Web client in this example requires no compilation. For more information about asant, see Building the Examples.


Note: When compiling the code, the preceding asant task includes the j2ee.jar file in the classpath. This file resides in the lib directory of your Application Server installation. If you plan to use other tools to compile the source code for J2EE components, make sure that the classpath includes the j2ee.jar file.


Packaging the Enterprise Bean

To package an enterprise bean, you run the Edit Enterprise Bean wizard of the deploytool utility. During this process, the wizard performs the following tasks:

To start the Edit Enterprise Bean wizard, select FileRight ArrowNewRight ArrowEnterprise Bean. The wizard displays the following dialog boxes.

  1. Introduction dialog box
    1. Read the explanatory text for an overview of the wizard's features.
    2. Click Next.
  2. EJB JAR dialog box
    1. Select the button labeled Create New JAR Module in Application.
    2. In the combo box below this button, select ConverterApp.
    3. In the JAR Display Name field, enter ConverterJAR.
    4. Click Edit Contents.
    5. In the tree under Available Files, locate the build/converter subdirectory. (If the target directory is many levels down in the tree, you can simplify the tree view by entering all or part of the directory's path name in the Starting Directory field.)
    6. In the Available Files tree select these classes: Converter.class, ConverterBean.class, and ConverterHome.class. (You can also drag and drop these class files to the Contents text area.)
    7. Click Add.
    8. Click OK.
    9. Click Next.
  3. General dialog box
    1. Under Bean Type, select the Stateless Session.
    2. In the Enterprise Bean Class combo box, select converter.ConverterBean.
    3. In the Enterprise Bean Name field, enter ConverterBean.
    4. In the Remote Home Interface combo box, select converter.ConverterHome.
    5. In the Remote Interface combo box, select converter.Converter.
    6. Click Next.
  4. In the Expose as Web Service Endpoint dialog box, select No and click Next.
  5. Click Finish.
Divider
Download
FAQ
History
PrevHomeNext API
Search
Feedback
Divider

All of the material in The J2EE(TM) 1.4 Tutorial is copyright-protected and may not be published in other works without express written permission from Sun Microsystems.