background image

Coding the Enterprise Bean

<< Getting Started with Enterprise Beans | Packaging the converter Example >>
<< Getting Started with Enterprise Beans | Packaging the converter Example >>

Coding the Enterprise Bean

Coding the Enterprise Bean
The enterprise bean in this example needs the following code:
Remote business interface
Enterprise bean class
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 Converter remote
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 @Remote annotation decorating the interface definition. This lets the container know
that ConverterBean will 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 (dollarToYen and yenToEuro) that the Converter remote business
interface defines. The source code for the ConverterBean class 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);
}
Creating the Enterprise Bean
The Java EE 5 Tutorial · September 2007
646