FileDocCategorySizeDatePackage
StatefulHASessionPersistenceManager.javaAPI DocJBoss 4.2.17984Fri Jul 13 20:52:34 BST 2007org.jboss.ejb.plugins

StatefulHASessionPersistenceManager

public class StatefulHASessionPersistenceManager extends org.jboss.system.ServiceMBeanSupport implements HASessionState.HASessionStateListener, HAPersistentManager, org.jboss.ejb.StatefulSessionPersistenceManager
This persistence manager work with an underlying HASessionState to get clustered state.
see
HASessionState
see
org.jboss.ha.hasessionstate.server.HASessionStateImpl
author
Sacha Labourey
author
Jason Dillon
version
$Revision: 57188 $

Revisions:

Fields Summary
private org.jboss.ejb.StatefulSessionContainer
con
private org.jboss.ha.hasessionstate.interfaces.HASessionState
sessionState
private String
localNodeName
private String
appName
private static String
DEFAULT_HASESSIONSTATE_JNDI_NAME
Constructors Summary
public StatefulHASessionPersistenceManager()
Creates new StatefulHASessionPersistenceManager

   
Methods Summary
public voidactivateSession(org.jboss.ejb.StatefulSessionEnterpriseContext ctx)

      try
      {
         ObjectInputStream in;
         
         // Load state
         PackagedSession state = this.sessionState.getStateWithOwnership (this.appName, ctx.getId ());
         
         if (state == null)
            throw new EJBException ("Could not activate; failed to recover session (session has been probably removed by session-timeout)");
         
         in = new SessionObjectInputStream (ctx, new ByteArrayInputStream (state.getState ()));;
         
         ctx.setInstance ( in.readObject () );
         
         in.close ();
         
         //removePassivated (ctx.getId ());

         
         // Instruct the bean to perform activation logic
         ((SessionBean)ctx.getInstance()).ejbActivate();
      }
      catch (ClassNotFoundException e)
      {
         throw new EJBException ("Could not activate", e);
      }
      catch (IOException e)
      {
         throw new EJBException ("Could not activate", e);
      }
   
public java.lang.ObjectcreateId(org.jboss.ejb.StatefulSessionEnterpriseContext ctx)

      //
      // jason: could probably use org.jboss.util.collection.CompoundKey here
      //        for better uniqueness based on hashCode and such...
      //
      // return new CompoundKey(this.localNodeName, new UID());
      //
      return this.localNodeName + ":" + new UID();
   
protected voidcreateService()

      // Initialize the dataStore

      // Find HASessionState that we will use
      //
      String sessionStateName = org.jboss.metadata.ClusterConfigMetaData.DEFAULT_SESSION_STATE_NAME;
      ClusterConfigMetaData config = con.getBeanMetaData ().getClusterConfigMetaData ();
      if (config != null)
         sessionStateName = config.getHaSessionStateName ();

      Context ctx = new InitialContext ();
      try {
         this.sessionState = (HASessionState)ctx.lookup (sessionStateName);
      }
      finally {
         ctx.close();
      }
      
      this.localNodeName = this.sessionState.getNodeName ();
      this.appName = this.con.getBeanMetaData ().getJndiName ();
      
      this.sessionState.subscribe (this.appName, this);
   
public voidcreatedSession(org.jboss.ejb.StatefulSessionEnterpriseContext ctx)

      this.sessionState.createSession(this.appName, ctx.getId());
   
public voidnewSessionStateTopology(org.jboss.ha.framework.interfaces.HAPartition haSubPartition)

   
public voidpassivateSession(org.jboss.ejb.StatefulSessionEnterpriseContext ctx)

      // do nothing
   
public voidremovePassivated(java.lang.Object key)

      this.sessionState.removeSession (this.appName, key);
   
public voidremoveSession(org.jboss.ejb.StatefulSessionEnterpriseContext ctx)

      try
      {
         // Call bean
         ((SessionBean)ctx.getInstance()).ejbRemove();
      }
      finally
      {
         this.sessionState.removeSession (this.appName, ctx.getId ());         
      }
   
public voidsessionExternallyModified(org.jboss.ha.hasessionstate.interfaces.PackagedSession session)

      // this callback warns us that a session (i.e. a bean) has been externally modified
      // this means that we need to tell to our InstanceCache that this bean is no more under its control
      // and that it should not keep a cached version of it => CACHE MISS
      //
      log.trace ("Invalidating cache for session: " + session.getKey());
      this.con.getInstanceCache ().remove (session.getKey ());
   
public voidsetContainer(org.jboss.ejb.Container c)

   
   // Constructors --------------------------------------------------
   
   // Public --------------------------------------------------------

       
   
      con = (StatefulSessionContainer)c;
   
protected voidstopService()

      this.sessionState.unsubscribe (this.appName, this);
   
public voidsynchroSession(org.jboss.ejb.StatefulSessionEnterpriseContext ctx)

      try
      {
         // Call bean
         ((SessionBean)ctx.getInstance()).ejbPassivate();
         
         // Store state
         ByteArrayOutputStream baos = new ByteArrayOutputStream ();
         ObjectOutputStream out = new SessionObjectOutputStream (baos);
         
         out.writeObject (ctx.getInstance ());
         
         out.close ();
         
         this.sessionState.setState (this.appName, ctx.getId (), baos.toByteArray ());

         ((SessionBean)ctx.getInstance()).ejbActivate(); //needed?         
      }
      catch (IOException e)
      {
         throw new EJBException ("Could not passivate", e);
      }