EJB - Stateful Session Beans

About

  • Session bean maintains state associated with a client.
  • Each session bean instance serves only ONE client.
  • Container creates an implicit identity for a stateful session bean to manage its passivation and activation.
  • Number of stateful session beans is equal to number of active clients.
  • Client should always explicity remove stateful session bean by calling remove() method on component interface.......otherwise will tie up resouces on container until it times out.
  • Can have more than one create(...) method (stateless can only have create() )
  • Any multi-threaded client (such as a servlet) must serialize all its calls to a stateful session bean.....simultaneous access to a stateful session bean results in a java.rmi.RemoteException.
  • In deployment descriptors can specify maximum number of beans and sometimes max age of a bean as part of caching policy ....determines when an instance might be passivated.

A few example uses

  • Shopping cart
  • Any kind of session state applications.

 

Remote Component Interface

public interface EnrollmentCart extends EJBObject {

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

public Collection getCourses() throws RemoteException;

public void empty() throws RemoteException;

}

Remote Home Interface

public interface EnrollmentCartHome extends EJBHome {

EnrollmentCart create() throws CreateException, RemoteException;

}

EJB Class

public class EnrollmentCartEJB implements javax.ejb.SessionBean {

private SessionContext ctx;
private HashSet cart;

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

pubic void ejbCreate() throws CreateException {
      cart = new HasSet();    }

public void ejbActivate() {}

public void ejbPassivate() {}

public void ejbRemove() {}

/* implement all business methods as defined in the component interface*/
public void addCourses(String[] courseIds) {
   if(couseIds == null)
      return;
   for(int i=0; i<courseIDs.length; i++)
      cart.add(courseIds[i]);
}

public Collection getCourses() {
   return cart; }

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

}

Deployment descriptor

<session>

<ejb-name>SignOnEJB</ejb-name>
<home>SignOnHome</home>
<remote>SignOn</remote>
<ejb-class>SignOnEJB</ejb-class>
<session-type>Stateful</session-type>
<transaction-type>Container</transaction-type>

</session>

Vendor-Specific Deployment descriptor for weblogic-ejb-jar.xml

<weblogic-ejb-jar>
  <weblogic-enterprise-bean>
      <ejb-name>EntrollmentCart</ejb-name>
           <stateful-session-descriptor>
               <stateful-session-cache>
                  <max-beans-in-cache>5</max-beans-in-cache>
               </stateful-session-cache>
           </stateful-session-descriptor>
      <jndi-name>packagename/EnrollmentCartHome</jndi-name>
  </weblogic-enterprise-bean>
</weblogic-ejb-jar>

transaction-type: specifiess container-mananaged transactions.

Life Cycle of Session Bean

 

Stateful:

  • Bean instance starts life when client invokes create(..) method on bean's home interface.
  • Container instantiates new bean using newInstance method and then calles setSessionContext and ejbCreate(..)
  • Instance now ready to serve client's business methods.
  • Container decides to evict instance from memory....based on caching policy and demand. Invokes ejbPassivate() and puts bean in secondary storage
  • Client invokes a session object whose session bean instance has been passivated......container activates instance.....calls ejbActivate()
  • Client calls remove() on home or component interface. Container calls ejbRemove() on bean instance.