User: slaboure
  Date: 01/12/31 07:19:49

  Added:       src/main/org/jboss/ha/httpsession/server
                        ClusteredHTTPSessionService.java
                        ClusteredHTTPSessionServiceMBean.java
  Log:
  ClusteredHTTPSession JMX Service: entry point for servlet containers.
  
  Revision  Changes    Path
  1.1                  
jbossmx/src/main/org/jboss/ha/httpsession/server/ClusteredHTTPSessionService.java
  
  Index: ClusteredHTTPSessionService.java
  ===================================================================
  /*
   * JBoss, the OpenSource J2EE webOS
   *
   * Distributable under LGPL license.
   * See terms of license at gnu.org.
   */
  
  package org.jboss.ha.httpsession.server;
  
  import java.util.Collection;
  import java.util.Iterator;
  
  import javax.ejb.EJBException;
  import javax.naming.InitialContext;
  import javax.rmi.PortableRemoteObject;
  
  import org.jboss.system.ServiceMBeanSupport;
  
  import org.jboss.ha.httpsession.interfaces.ClusteredHTTPSession;
  import org.jboss.ha.httpsession.interfaces.ClusteredHTTPSessionBusiness;
  import org.jboss.ha.httpsession.interfaces.ClusteredHTTPSessionHome;
  import org.jboss.ha.httpsession.interfaces.SerializableHttpSession;
  
  /**
   * JMX Service implementation for ClusteredHTTPSessionServiceMBean
   *
   * @see org.jboss.ha.httpsession.server.ClusteredHTTPSessionServiceMBean
   *
   * @author  <a href="mailto:[EMAIL PROTECTED]";>Sacha Labourey</a>.
   * @version $Revision: 1.1 $
   *
   * <p><b>Revisions:</b>
   *
   * <p><b>31. d�cembre 2001 Sacha Labourey:</b>
   * <ul>
   * <li> First implementation </li>
   * </ul>
   */
  
  public class ClusteredHTTPSessionService
  extends ServiceMBeanSupport
  implements ClusteredHTTPSessionServiceMBean
  {
     
     // Constants -----------------------------------------------------
     
     protected final static long CLEANUP_FREQUENCY = 30000; // every 30 seconds
     
     // Attributes ----------------------------------------------------
     
     protected ClusteredHTTPSessionHome httpSessionHome = null;
     protected ClusteredHTTPSessionService.CleanupDaemon cleanup = null;
     protected long sessionTimeout = 15*60*1000;
     
     // Static --------------------------------------------------------
     
     // Constructors --------------------------------------------------
     
     public ClusteredHTTPSessionService ()
     {
     }
     
     // Public --------------------------------------------------------
     
     public String getName ()
     {
        return "Clustered HTTPSession";
     }
     
     // ClusteredHTTPSessionServiceMBean ----------------------------------------------
     
     public void setHttpSession (String sessionId, SerializableHttpSession session) 
throws EJBException
     {
        try
        {
           ClusteredHTTPSession sessionBean = httpSessionHome.findByPrimaryKey 
(sessionId);
           sessionBean.setSession (session);
        }
        catch (Exception e)
        {
           throw new EJBException ("Exception in setHttpSession: " + e.toString ());
        }
     }
     
     public SerializableHttpSession getHttpSession (String sessionId) throws 
EJBException
     {
        try
        {
           ClusteredHTTPSession sessionBean = httpSessionHome.findByPrimaryKey 
(sessionId);
           return sessionBean.getSession ();
        }
        catch (Exception e)
        {
           throw new EJBException ("Exception in setHttpSession: " + e.toString ());
        }
     }
     
     public void removeHttpSession (String sessionId) throws EJBException
     {
        try
        {
           httpSessionHome.remove (sessionId);
        }
        catch (Exception e)
        {
           throw new EJBException ("Exception in removeHttpSession: " + e.toString ());
        }
     }
     
     public long getSessionTimeout () { return this.sessionTimeout; }
     public void setSessionTimeout (long miliseconds) { this.sessionTimeout = 
miliseconds; }
     
     // ServiceMBeanSupport overrides 
---------------------------------------------------
     
     public void startService ()
     throws Exception
     {
        // we (try to) acquire a home reference to the entity bean that stores our 
sessions
        //
        this.initRefToBean ();
        cleanup = new ClusteredHTTPSessionService.CleanupDaemon ();
        cleanup.start ();
     }
     
     public void stopService ()
     {
        cleanup.stop ();
        httpSessionHome = null;
     }
     
     // Package protected ---------------------------------------------
     
     // Protected -----------------------------------------------------
     
     protected void initRefToBean () throws Exception
     {
        InitialContext jndiContext = new InitialContext ();
        Object ref  = jndiContext.lookup (ClusteredHTTPSessionHome.JNDI_NAME);
        httpSessionHome = (ClusteredHTTPSessionHome)
        PortableRemoteObject.narrow (ref, ClusteredHTTPSessionHome.class);
     }
     
     // Private -------------------------------------------------------
     
     // Inner classes -------------------------------------------------
     
     protected class CleanupDaemon implements Runnable
     {
        protected boolean stopping = false;
        
        public CleanupDaemon () throws Exception
        {
        }
        
        public void start ()
        {
           stopping = false;
           Thread t = new Thread (this, "ClusteredHTTPSessionService - CleanupDaemon");
           t.start ();
        }
        
        public void stop ()
        {
           stopping = true;
        }
        
        public void run ()
        {
           while (!stopping)
           {
              try
              {
                 // we get all beans and only check if they have timeouted. If we 
                 // don't ask for the HTTPSession content attribute, this will *not* 
                 // deserialize their content!
                 //
                 Collection allBeans = httpSessionHome.findAll ();
                 Iterator iter = allBeans.iterator ();
                 long now = System.currentTimeMillis ();
                 while (iter.hasNext ())
                 {
                    ClusteredHTTPSession sessionBean = (ClusteredHTTPSession)iter.next 
();
                    long lastAccess = sessionBean.getLastAccessedTime ();
                    
                    if ( ( now - lastAccess) > sessionTimeout )
                       sessionBean.remove ();
                    
                    if (stopping)
                       return;                  
                 }               
                 
                 // and we sleep a while...
                 //
                 synchronized (this)
                 {
                    this.wait (CLEANUP_FREQUENCY);
                 }
              }
              catch (Exception e)
              {
                 e.printStackTrace ();
              }            
                          
           }
        }
     }
     
  }
  
  
  
  1.1                  
jbossmx/src/main/org/jboss/ha/httpsession/server/ClusteredHTTPSessionServiceMBean.java
  
  Index: ClusteredHTTPSessionServiceMBean.java
  ===================================================================
  /*
   * JBoss, the OpenSource J2EE webOS
   *
   * Distributable under LGPL license.
   * See terms of license at gnu.org.
   */
  
  package org.jboss.ha.httpsession.server;
  
  import javax.ejb.EJBException;
  
  import org.jboss.ha.httpsession.interfaces.SerializableHttpSession;
  
  /**
   * Service that provide unified access to clustered HTTPSessions for servlets.
   *
   * @see org.jboss.ha.httpsession.interfaces.ClusteredHTTPSession
   * @see org.jboss.ha.httpsession.interfaces.ClusteredHTTPSessionHome
   *
   * @author  <a href="mailto:[EMAIL PROTECTED]";>Sacha Labourey</a>.
   * @version $Revision: 1.1 $
   *   
   * <p><b>Revisions:</b>
   *
   * <p><b>31. d�cembre 2001 Sacha Labourey:</b>
   * <ul>
   * <li> First implementation </li> 
   * </ul>
   */
  
  public interface ClusteredHTTPSessionServiceMBean extends 
org.jboss.system.ServiceMBean
  {
     /**
      * Return the HttpSession associated to a session id.
      * As all session id are shared for all Web Applications, all sessions ids accross
      * all applications and all nodes must be distincts!
      * The creation and last access time of the session may not be correct if they
      * are the only thing that has been modified on a distant node (and no attribute).
      * See setHttpSession for more information.
      */   
     public SerializableHttpSession getHttpSession (String sessionId) throws 
EJBException;
     /**
      * Associate a new session with the session id. To reduce the cluster 
communication,
      * if the only thing that has changed in the session is the last accessed time, 
the
      * new session is kept in cache but not replicated on the other nodes. Thus, if 
you
      * use a front-end load-balancer that support sticky session, that is not a 
problem
      * because a client will always target the same node and receive the updated 
session
      * available in cache.
      * Nevertheless, as soon as an attribute is modified in the session, it is 
replicated
      * in the cluster.
      */   
     public void setHttpSession (String sessionId, SerializableHttpSession session) 
throws EJBException;
     /**
      * Remove an HttpSession from the cluster (log off for example)
      */   
     public void removeHttpSession (String sessionId) throws EJBException;
     
     /**
      * Indicate the duration, in ms, after which the session can be cleaned if no
      * access occurs.
      */   
     public long getSessionTimeout (); // defaults to 15 minutes i.e. 15*60*1000 = 
900'000
     /**
      * Indicate the duration, in ms, after which the session can be cleaned if no
      * access occurs.
      */   
     public void setSessionTimeout (long miliseconds);   
  }
  
  
  

_______________________________________________
Jboss-development mailing list
[EMAIL PROTECTED]
https://lists.sourceforge.net/lists/listinfo/jboss-development

Reply via email to