Creating the Enterprise Bean
An enterprise bean is a server-side component that contains the business logic of an app. At runtime, the app clients execute the business logic by invoking the enterprise bean's methods. The enterprise bean in our example is a stateless session bean called ConverterEJB. The source code for ConverterEJB is in the j2eetutorial/examples/src/ejb/converter directory.
Coding the Enterprise Bean
The enterprise bean in this example requires the following code:
- Remote interface
- Home interface
- Enterprise bean class
Coding the Remote Interface
A remote interface defines the business methods that a client may 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.io.Serializable; 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), home interface (ConverterHome.java), and the enterprise bean class (ConverterBean.java).
- In a terminal window, go to the j2eetutorial/examples directory.
- Type the following command:
ant converterThis command compiles the source files for the enterprise bean and the J2EE app client. It places the resulting class files in the j2eetutorial/examples/build/ejb/converter directory (not the src directory).
Note: When compiling the code, the preceding ant task includes the j2ee.jar file in the classpath. This file resides in the lib directory of your J2EE SDK installation. If you plan on using 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 New Enterprise Bean wizard of the deploytool utility. During this process, the wizard performs the following tasks:
- Creates the bean's deployment descriptor
- Packages the deployment descriptor and the bean's classes in an EJB JAR file
- Inserts the EJB JAR file into the app's ConverterApp.ear file
After the packaging process, you can view the deployment descriptor by selecting ToolsDescriptor Viewer.
To start the New Enterprise Bean wizard, select FileNewEnterprise Bean. The wizard displays the following dialog boxes.
- Introduction dialog box
- Read the explanatory text for an overview of the wizard's features.
- Click Next.
- EJB JAR dialog box
- Select the Create New JAR File In Application button.
- In the combo box, select ConverterApp.
- In the JAR Display Name field, enter ConverterJAR.
- Click Edit.
- In the tree under Available Files, locate the j2eetutorial/examples/build/ejb/converter directory. (If the converter directory is many levels down in the tree, you can simplify the tree view by entering all or part of the converter directory's path name in the Starting Directory field.)
- Select the following classes from the Available Files tree and click Add: Converter.class, ConverterBean.class, and ConverterHome.class. (You may also drag and drop these class files to the Contents text area.)
- Click OK.
- Click Next.
- General dialog box
- Under Bean Type, select the Session radio button.
- Select the Stateless radio button.
- In the Enterprise Bean Class combo box, select ConverterBean.
- In the Enterprise Bean Name field, enter ConverterEJB.
- In the Remote Home Interface combo box, select ConverterHome.
- In the Remote Interface combo box, select Converter.
- Click Next.
- Transaction Management dialog box
- Because you may skip the remaining dialog boxes, click Finish.