EJB - Remote versus Local Interfaces

Remote Interfaces

Client is remote

accesses EJB through the bean's remote interface and remote home interface.

  • remote interface extends javax.ejb.EJBObject
  • remote home interface extends javax.ejb.EJBHome
  • all methods in EJB interface and EJB home interface must throw RemoteException as they are remote objects (invoked remotely by remote client).
  • all method return types and argumetns in EJB and EJB interfaces must be only vaild RMI types like primitives, serializable objects and RMI/IIOP remote objects.

 

note: EJB uses Java RMI (Remote Method Invocation) over IIOP as the communication protocol.

Example Home Interface

 

public interface EnrollmentCartHome extends EJBHome {

          EnrollmentCart create() throws CreateException, RemoteException;

}

 

Example Component Interface

public interface EnrollmentCart extends EJBObject {

    public void addCourses(String[] courseIDs) throws RemoteException;

    public Collection getCourses() throws RemoteException;

    public void emptly() throws RemoteException;

}

Example EJB class

public class EnrollmentCartEJB implements javax.ejb.SessionBean {

private SessionContect ctx;
private HashSet cart;

/* callback methods*/
public void setSessionContext(SessionContext ctx) { this.ctx = ctx; }

public void ejbCreate() throws CreateException {
  cart = new HashSet();

public void ejbActivate() {}
public void ejbPassivate() {}
public void ejbRemove() {}

/*business methods*/
public void addCourses(String[] courseIds) {
    if(courseIds == null) { return; }

    for (int i=0; i<courseIDs.length; i++) {
        cart.add(courseIds[i]); }
}

public Collection getCourses() { return cart; }

public void empty() {cart.clear(); }

}

 

Local Interfaces

Client is remote

accesses EJB through the bean's local interface and localhome interface.

  • component interface extends javax.ejb.EJBLocalObject
  • local home interface extends javax.ejb.EJBLocalHome
  • requires client and EJB on same physical machine using same JVM..
  • advantage: less network traffic, RMI call is expensive (involves stubs and skeletons marshalling and unmarshalling the call parameters, etc.).