EJB - Stateless Session Beans

About

  • Client must be responsible for maintaining the conversaitional state if any exists for the application.
  • The container can reuse the same instance of a session bean to serve multiple clients.
  • Container creates instances as specified in the deployment descriptor (vendor specific)
  • Container can maintain this pool of instances and reuse them when a client is done, as a function of the "caching policy" (described in vendor specific descriptors)
    • benefits beformance...small number of instances in predefined pool can satisfy over time a large number of clients.

A few example uses

  • Stock Quote Component that returns current price
  • Compression Algorithm for media/data...accesss uncompressed data returns compressed buffer
  • Login Component

 

Remote Component Interface

public interface SignOn extends EJBObject {

public void addUser(String username, String password) throws RemoteException;

public boolean validateUser(Stringlogin, String password) throws InvalidLoginException, RemoteException;

}

Remote Home Interface

public interface SignOnHome extends EJBHome {

SignOn create() throws CreateException, RemoteException;

}

EJB Class

public class SignOnEB implements SessionBean {

private SessionContext ctx;

/** callback methods*/

/*container calls to set associated session context */
public void setSessionContext(SessionContext c) {
     ctx = c; }

/*container calls to initialize your session bean instance*/
public void ejbCreate(){}

/*container invokes this method before it ends life of session*/
public void ejbRemove() {}

/*container calls after it activates ejb*/
public void ejbActivate() {}

/*container calls before it passivates ejb*/
public void ejbPassivate() {}

 

/*business methods*/
public void addUser(String userName, String password) {

      .......
}

public boolean validateUser(String userName, String password) throws InvalidLoginException {
      .......
}

}

Deployment descriptor

<session>

<ejb-name>SignOnEJB</ejb-name>
<home>SignOnHome</home>
<remote>SignOn</remote>
<ejb-class>SignOnEJB</ejb-class>
<session-type>Stateless</session-type>

</session>

 

Life Cycle of Session Bean

Stateless:

  • Container decides to instantiate a bean instance. Based on caching policy (vendor specificy descriptor) and client demaind. If more clients want the session bean, the container instantiates more beans.
  • Container instantiates bean using newInstance method and then calles the methods setSessionContext and ejbCreate. The container also sets the transaction context and security attributes (as specified in the deployment descriptor).
  • Container calls business method on instance based on client call. Container can use same bean instance to serve multiple clients
  • The container decides to remove the bean. Maybe because the container wants to reduce the number of instances in teh "method-ready" pool. This is based on caching policy and reduced client demand.
  • Container calls ejbRemove() method of bean instance.