Click to See Complete Forum and Search --> : Why doesn't the session bean implement the remote interface?


moonriver
07-05-2005, 03:33 AM
I just retrieve some sample codes from SUN's J2EE 1.4 Tutorial:

Source codes of Remote Interface:
==============================================================
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;
}
=======================================================

Source codes of Enterprise Bean:
===============================================================
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) {}
}
===============================================================

It is well known that business methods declared in a remote interface are implemented in an Enterprise Bean. However, in term of interface's definition, a class must implement an interface before implementing the interface's methods. What I am confused is why the class convertBean implements SessionBean instead of the remote interface Convertor. What is the relationship between the SessionBean and the Convertor interfaces? or the SessionBean and the EJBObject interfaces? Could you give me a draft for the relationship between those relevant interfaces of EJB?

Oak
07-06-2005, 09:45 PM
Read the next page:

Creating an Enterprise Bean Instance

To create the bean instance, the client invokes the create method on the ConverterHome object. The create method returns an object whose type is Converter. The remote Converter interface defines the business methods of the bean that the client can call. When the client invokes the create method, the EJB container instantiates the bean and then invokes the ConverterBean.ejbCreate method. The client invokes the create method as follows:

Converter currencyConverter = home.create();

Invoking a Business Method

Calling a business method is easy: you simply invoke the method on the Converter object. The EJB container will invoke the corresponding method on the ConverterBean instance that is running on the server. The client invokes the dollarToYen business method in the following lines of code.

BigDecimal param = new BigDecimal ("100.00");
BigDecimal amount = currencyConverter.dollarToYen(param);

In normal circumstances it would appear that they are missing implementations etc, however it appears that the EJB container automatically knows things and performs things based on the struture of the jar that you have created.

I think if you read it all again and have a good think and perhaps check some more general knowledge of EJB then you should be right!

Good luck :)