|
Home TOC Index |
|
Search
Feedback |
Creating the Enterprise Bean
An enterprise bean is a server-side component that contains the business logic of an application. At runtime, the application 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 forConverterEJBis in thej2eetutorial/examples/src/ejb/converterdirectory.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
Converterremote 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
ConverterHomeinterface contains a single create method, which returns an object of the remote interface type. Here is the source code for theConverterHomeinterface: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,dollarToYenandyenToEuro, that theConverterremote interface defines. The source code for theConverterBeanclass 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/examplesdirectory.- 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
j2eetutorial/examples/build/ejb/converterdirectory (not thesrcdirectory). For more information aboutant, see How to Build and Run the Examples.
Note: When compiling the code, the precedinganttask includes thej2ee.jarfile in the classpath. This file resides in thelibdirectory 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 thej2ee.jarfile.
Packaging the Enterprise Bean
To package an enteprise bean, you run the New Enterprise Bean wizard of the
deploytoolutility. 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 application's
ConverterApp.earfileDuring the packaging process, you can view the deployment descriptor by selecting Tools
Descriptor Viewer.
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
- 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/converterdirectory. (If theconverterdirectory is many levels down in the tree, you can simplify the tree view by entering all or part of theconverterdirectory's path name in the Starting Directory field.)- Select the following classes from the Available Files tree and click Add:
Converter.class,ConverterBean.class, andConverterHome.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.
|
Home TOC Index |
|
Search
Feedback |