Download
FAQ History |
API
Search Feedback |
Creating the Enterprise Bean
The enterprise bean in our example is a stateless session bean called
ConverterBean
. The source code forConverterBean
is in the<
INSTALL
>/j2eetutorial14/examples/ejb/converter/src/
directory.Creating
ConverterBean
requires these steps: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 theConverterHome
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
andyenToEuro
) that theConverter
remote interface defines. The source code for theConverterBean
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
).This command compiles the source files for the enterprise bean and the application client, placing the class files in the
converter/build
subdirectory (not thesrc
directory). The Web client in this example requires no compilation. For more information aboutasant
, see Building the Examples.
Note: When compiling the code, the preceding
asant
task includes thej2ee.jar
file in the classpath. This file resides in thelib
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 thej2ee.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 FileNewEnterprise Bean. The wizard displays the following dialog boxes.
- Introduction dialog box
- EJB JAR dialog box
- Select the button labeled Create New JAR Module in Application.
- In the combo box below this button, select
ConverterApp
.- In the JAR Display Name field, enter
ConverterJAR
.- Click Edit Contents.
- 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.)- In the Available Files tree select these classes:
Converter.class
,ConverterBean.class
, andConverterHome.class
. (You can also drag and drop these class files to the Contents text area.)- Click Add.
- Click OK.
- Click Next.
- General dialog box
- Under Bean Type, select the Stateless Session.
- In the Enterprise Bean Class combo box, select
converter.ConverterBean
.- In the Enterprise Bean Name field, enter
ConverterBean
.- In the Remote Home Interface combo box, select
converter.ConverterHome
.- In the Remote Interface combo box, select
converter.Converter
.- Click Next.
- In the Expose as Web Service Endpoint dialog box, select No and click Next.
- Click Finish.
Download
FAQ History |
API
Search Feedback |
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.