Home TOC |
Search
Feedback |
Creating the Enterprise Bean
The source code for the enterprise bean is in the examples/src/ejb/converter directory. An enterprise bean is a server-side component that contains the business logic of an application. At run time, the application clients execute the business logic by invoking the enterprise bean's methods.
Coding the Enterprise Bean
The enterprise bean in this example requires the following code:
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; public interface Converter extends EJBObject { public double dollarToYen(double dollars) throws RemoteException; public double yenToEuro(double 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 in our example is a stateless session bean called ConverterEJB. This bean implements the two business methods, dollarToYen and yenToEuro, that the Converter remote interface defines. The source code for the ConverterEJB class follows.
import java.rmi.RemoteException; import javax.ejb.SessionBean; import javax.ejb.SessionContext; public class ConverterEJB implements SessionBean { public double dollarToYen(double dollars) { return dollars * 121.6000; } public double yenToEuro(double yen) { return yen * 0.0077; } public ConverterEJB() {} 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 (ConverterEJB.java):
- Go to the examples/src directory.
- Open the file build.xml in a text editor. Edit the following line, setting the value of the j2ee-home property to the J2EE SDK installation directory:
<property name="j2ee-home" value="C:\j2sdkee1.3" />- In a terminal window type the following command:
ant converterThis command compiles the source files for the enterprise bean and the J2EE application client. It places the resulting class files in the examples/build/ejb/converter directory. For more information about ant, see How to Build and Run the Examples.
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
In this section you will run the New Enterprise Bean Wizard of the deploytool to perform these tasks:
- Create the bean's deployment descriptor.
- Package the deployment descriptor and the bean's classes in an EJB JAR file.
- Insert the EJB JAR file into the application's ConverterApp.ear file.
To start the New Enterprise Bean Wizard, select File->New Enterprise Bean. The wizard displays the following dialog boxes.
- Introduction Dialog Box
- EJB JAR Dialog Box
- In the combo box labelled Enterprise Bean Will Go In, select ConverterApp.
- In the JAR Display Name field enter ConverterJAR.
- Click the Add button next to the Contents text area.
- In the tree under Available Files, locate the examples/build/ejb/converter directory. (Or, you may type the directory name in the top field.)
- Select the following classes from the text area and click Add: Converter.class, ConverterEJB.class, and ConverterHome.class. (Or, you may drag them to the bottom field.)
- 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 ConverterEJB.
- In the Home Interface combo box, select ConverterHome.
- In the Remote Interface combo box, select Converter.
- In the Enterprise Bean Name field, enter ConverterBean.
- Click Next.
- Environment Entries Dialog Box
Home TOC |
Search
Feedback |