10.12.2012 Views

The Java EE 5 Tutorial (PDF) - Oracle Software Downloads

The Java EE 5 Tutorial (PDF) - Oracle Software Downloads

The Java EE 5 Tutorial (PDF) - Oracle Software Downloads

SHOW MORE
SHOW LESS

Create successful ePaper yourself

Turn your PDF publications into a flip-book with our unique Google optimized e-Paper software.

Creating the Enterprise Bean<br />

644<br />

Coding the Enterprise Bean<br />

<strong>The</strong> enterprise bean in this example needs the following code:<br />

■ Remote business interface<br />

■ Enterprise bean class<br />

Coding the Business Interface<br />

<strong>The</strong> business interface defines the business methods that a client can call. <strong>The</strong> business methods<br />

are implemented in the enterprise bean class. <strong>The</strong> source code for the Converter remote<br />

business interface follows.<br />

package com.sun.tutorial.javaee.ejb;<br />

import java.math.BigDecimal;<br />

import javax.ejb.Remote;<br />

@Remote<br />

public interface Converter {<br />

public BigDecimal dollarToYen(BigDecimal dollars);<br />

public BigDecimal yenToEuro(BigDecimal yen);<br />

}<br />

Note the @Remote annotation decorating the interface definition. This lets the container know<br />

that ConverterBean will be accessed by remote clients.<br />

Coding the Enterprise Bean Class<br />

<strong>The</strong> enterprise bean class for this example is called ConverterBean. This class implements the<br />

two business methods (dollarToYen and yenToEuro) that the Converter remote business<br />

interface defines. <strong>The</strong> source code for the ConverterBean class follows.<br />

package com.sun.tutorial.javaee.ejb;<br />

import java.math.BigDecimal;<br />

import javax.ejb.*;<br />

@Stateless<br />

public class ConverterBean implements Converter {<br />

private BigDecimal yenRate = new BigDecimal("115.3100");<br />

private BigDecimal euroRate = new BigDecimal("0.0071");<br />

public BigDecimal dollarToYen(BigDecimal dollars) {<br />

BigDecimal result = dollars.multiply(yenRate);<br />

return result.setScale(2, BigDecimal.ROUND_UP);<br />

}<br />

<strong>The</strong> <strong>Java</strong> <strong>EE</strong> 5<strong>Tutorial</strong> • June 2010

Hooray! Your file is uploaded and ready to be published.

Saved successfully!

Ooh no, something went wrong!