Creating the Enterprise Bean
The enterprise bean in our example is a stateless session bean called
ConverterBean. The source code forConverterBeanis in the<INSTALL>/javaeetutorial5/examples/ejb/converter/converter-ejb/src/javadirectory.Creating
ConverterBeanrequires these steps:Coding the Enterprise Bean
The enterprise bean in this example needs the following code:
Coding the Business Interface
The business interface defines the business methods that a client can call. The business methods are implemented in the enterprise bean class. The source code for the
Converterremote business interface follows.package com.sun.tutorial.javaee.ejb; import java.math.BigDecimal; import javax.ejb.Remote; @Remote public interface Converter { public BigDecimal dollarToYen(BigDecimal dollars); public BigDecimal yenToEuro(BigDecimal yen); }Note the
@Remoteannotation decorating the interface definition. This lets the container know thatConverterBeanwill be accessed by remote clients.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 business interface defines. The source code for theConverterBeanclass follows.package com.sun.tutorial.javaee.ejb; import java.math.BigDecimal; import javax.ejb.*; @Stateless public class ConverterBean implements Converter { private BigDecimal yenRate = new BigDecimal("115.3100"); private BigDecimal euroRate = new BigDecimal("0.0071"); 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); } }Note the
@Statelessannotation decorating the enterprise bean class. This lets the container know thatConverterBeanis a stateless session bean.Compiling and Packaging converter
Now you are ready to compile the remote business interface (
Converter.java) and the enterprise bean class (ConverterBean.java), and package the compiled classes into an enterprise bean JAR.This command calls the
defaulttask, which compiles the source files for the enterprise bean and the application client, placing the class files in thebuildsubdirectories (not thesrcdirectory) of each submodule. Then the default task packages each submodule into the appropriate package file:converter-app-client.jarfor the application client,converter-ejb.jarfor the enterprise bean JAR, andconverter-war.warfor the web client. The web client in this example requires no compilation. For more information aboutant, see Building the Examples (page xxxi).
Note: When compiling the code, the preceding
anttask includes thejavaee.jarfile in the classpath. This file resides in thelibdirectory of your Application Server installation. If you plan to use other tools to compile the source code for Java EE components, make sure that the classpath includes thejavaee.jarfile.