User: fleury  
  Date: 00/09/26 11:57:17

  Modified:    src/main/org/jboss/ejb/plugins
                        RandomEntityInstanceCache.java
  Log:
  The cache does not do locking also the cachekey structure is streamlined
  
  Revision  Changes    Path
  1.8       +152 -162  
jboss/src/main/org/jboss/ejb/plugins/RandomEntityInstanceCache.java
  
  Index: RandomEntityInstanceCache.java
  ===================================================================
  RCS file: 
/products/cvs/ejboss/jboss/src/main/org/jboss/ejb/plugins/RandomEntityInstanceCache.java,v
  retrieving revision 1.7
  retrieving revision 1.8
  diff -u -r1.7 -r1.8
  --- RandomEntityInstanceCache.java    2000/08/25 02:27:37     1.7
  +++ RandomEntityInstanceCache.java    2000/09/26 18:57:16     1.8
  @@ -1,163 +1,153 @@
  -/*
  -* jBoss, the OpenSource EJB server
  -*
  -* Distributable under GPL license.
  -* See terms of license at gnu.org.
  -*/
  -package org.jboss.ejb.plugins;
  -
  -import java.rmi.RemoteException;
  -import java.rmi.ServerException;
  -import java.util.Collection;
  -import java.util.Iterator;
  -import java.util.HashMap;
  -import java.util.LinkedList;
  -
  -import org.jboss.ejb.Container;
  -import org.jboss.ejb.EntityContainer;
  -import org.jboss.ejb.InstanceCache;
  -import org.jboss.ejb.InstancePool;
  -import org.jboss.ejb.EntityPersistenceManager;
  -import org.jboss.ejb.EnterpriseContext;
  -import org.jboss.ejb.EntityEnterpriseContext;
  -
  -import org.jboss.logging.Logger;
  -
  -/**
  -*    <description> 
  -*      
  -*    @see <related>
  -*    @author Rickard �berg ([EMAIL PROTECTED])
  -*  @author <a href="mailto:[EMAIL PROTECTED]">Marc Fleury</a>
  -*    @version $Revision: 1.7 $
  -*/
  -public class RandomEntityInstanceCache
  -extends NoPassivationEntityInstanceCache
  -{
  -
  -     // Constants -----------------------------------------------------
  -     
  -     // Attributes ----------------------------------------------------
  -     boolean running = false; // Passivator thread running?
  -     
  -     int minActive = 100; // Always try to passivate if more than this nr are active
  -     
  -     long timeout = 60*1000L; // Passivation sweep sleep time
  -     
  -     // Static --------------------------------------------------------
  -     
  -     // Constructors --------------------------------------------------
  -     
  -     // Public --------------------------------------------------------
  -     public void start()
  -     throws Exception
  -     {
  -             running = true;
  -             new Thread(new Passivator()).start();
  -     }
  -     
  -     public void stop()
  -     {
  -             running = false;
  -     }
  -     
  -     // Z implementation ----------------------------------------------
  -     
  -     // Package protected ---------------------------------------------
  -     
  -     // Protected -----------------------------------------------------
  -     
  -     // Private -------------------------------------------------------
  -     
  -     // Inner classes -------------------------------------------------
  -     class Passivator
  -     implements Runnable
  -     {
  -             //RandomEntityInstanceCache cache;
  -             
  -             public void run()
  -             {
  -                     Logger.debug("Passivator started");
  -                     // Passivation loop
  -                     while(running)
  -                     {
  -                             //            Logger.log("Clearing cache");
  -                             // Passivate old. Lock cache first
  -                             //synchronized(RandomEntityInstanceCache.this)
  -                             synchronized(cache)
  -                             {
  -                                     // Do not use cache (many to one entries)
  -                                     int currentActive = cacheKeys.size();
  -                                     if (currentActive > minActive)
  -                                     {
  -                                             InstancePool pool = 
((EntityContainer)con).getInstancePool();
  -                                             
  -                                             Logger.debug("Too many active 
instances:"+currentActive);
  -                                             
  -                                             // Passivate some instance; they need 
to be unlocked though
  -                                             
  -                                             //KeySet has keys (currentActive>0)    
 
  -                                             Iterator ids = 
cacheKeys.keySet().iterator();
  -                                             
  -                                             while(ids.hasNext())
  -                                             {
  -                                                     
  -                                                     Object id = ids.next();
  -                                                     
  -                                                     //Get the context
  -                                                     EntityEnterpriseContext ctx =  
  -                                                             
(EntityEnterpriseContext) cache.get(((LinkedList) cacheKeys.get(id)).getFirst());
  -                                                     
  -                                                     
  -                                                     // Make sure we can work on it
  -                                                     
Logger.debug("Checking:"+ctx.getId());
  -                                                     InstanceInfo info = 
(InstanceInfo)ctx.getCacheContext();
  -                                                     
  -                                                     //We we locked?
  -                                                     if (!info.isLocked())
  -                                                     {
  -                                                             // Nope then Passivate
  -                                                             try
  -                                                             {
  -                                                                     
Logger.debug("Passivating:"+ctx.getId());
  -                                                                     
((EntityContainer)con).getPersistenceManager().passivateEntity(ctx);
  -                                                                     
  -                                                                     
  -                                                                     // Get the 
List by removing from cacheKeys 
  -                                                                     LinkedList 
keysList = (LinkedList) cacheKeys.remove(ids.next());
  -                                                                     
  -                                                                     // Remove all 
the cacheKeys from the cache
  -                                                                     Iterator 
iterator = keysList.listIterator();
  -                                                                     
  -                                                                     while 
(iterator.hasNext()) {
  -                                                                             
  -                                                                             
cache.remove(iterator.next());
  -                                                                     }
  -                                                                     
  -                                                                     
currentActive--;
  -                                                             }
  -                                                             
  -                                                             catch (Exception e) { 
Logger.log("Could not passivate instance");}
  -                                                     }
  -                                                     
  -                                                     if (currentActive == 
minActive) break;
  -                                             
  -                                             }
  -                                     
  -                                     }    
  -                             }       
  -                             //            Logger.log("Passivation done");
  -                             
  -                             // Sleep
  -                             try
  -                             {
  -                                     Thread.sleep(timeout);
  -                             } catch (InterruptedException e)
  -                             {
  -                                     // Ignore
  -                             }
  -                     
  -                     }
  -             }
  -     }
  +/*
  +* jBoss, the OpenSource EJB server
  +*
  +* Distributable under GPL license.
  +* See terms of license at gnu.org.
  +*/
  +package org.jboss.ejb.plugins;
  +
  +import java.rmi.RemoteException;
  +import java.rmi.ServerException;
  +import java.util.Collection;
  +import java.util.Iterator;
  +import java.util.HashMap;
  +import java.util.LinkedList;
  +
  +import org.jboss.ejb.Container;
  +import org.jboss.ejb.EntityContainer;
  +import org.jboss.ejb.InstanceCache;
  +import org.jboss.ejb.InstancePool;
  +import org.jboss.ejb.EntityPersistenceManager;
  +import org.jboss.ejb.EnterpriseContext;
  +import org.jboss.ejb.EntityEnterpriseContext;
  +
  +import org.jboss.logging.Logger;
  +
  +/**
  +*    <description> 
  +*      
  +*    @see <related>
  +*    @author Rickard �berg ([EMAIL PROTECTED])
  +*  @author <a href="mailto:[EMAIL PROTECTED]">Marc Fleury</a>
  +*    @version $Revision: 1.8 $
  +*/
  +public class RandomEntityInstanceCache
  +extends NoPassivationEntityInstanceCache
  +{
  +
  +    // Constants -----------------------------------------------------
  +    
  +    // Attributes ----------------------------------------------------
  +    boolean running = false; // Passivator thread running?
  +    
  +    int minActive = 100; // Always try to passivate if more than this nr are active
  +    
  +    long timeout = 60*1000L; // Passivation sweep sleep time
  +    
  +    // Static --------------------------------------------------------
  +    
  +    // Constructors --------------------------------------------------
  +    
  +    // Public --------------------------------------------------------
  +    public void start()
  +    throws Exception
  +    {
  +       running = true;
  +       new Thread(new Passivator()).start();
  +    }
  +    
  +    public void stop()
  +    {
  +       running = false;
  +    }
  +    
  +    // Z implementation ----------------------------------------------
  +    
  +    // Package protected ---------------------------------------------
  +    
  +    // Protected -----------------------------------------------------
  +    
  +    // Private -------------------------------------------------------
  +    
  +    // Inner classes -------------------------------------------------
  +    class Passivator
  +    implements Runnable
  +    {
  +       //RandomEntityInstanceCache cache;
  +       
  +       public void run()
  +       {
  +         Logger.debug("Passivator started");
  +         // Passivation loop
  +         while(running)
  +         {
  +          //            Logger.log("Clearing cache");
  +          // Passivate old. Lock cache first
  +          //synchronized(RandomEntityInstanceCache.this)
  +          synchronized(cache)
  +          {
  +              // Do not use cache (many to one entries)
  +              int currentActive = cache.size();
  +              if (currentActive > minActive)
  +              {
  +                 InstancePool pool = ((EntityContainer)con).getInstancePool();
  +                 
  +                 Logger.debug("Too many active instances:"+currentActive);
  +                 
  +                 // Passivate some instance; they need to be unlocked though
  +                 
  +                 //KeySet has cacheKeys (currentActive>0)    
  +                 Iterator keys = cache.keySet().iterator();
  +                 
  +                 while(keys.hasNext())
  +                 {
  +                   
  +                   Object key = keys.next();
  +                   
  +                   //Get the context
  +                   EntityEnterpriseContext ctx =  
  +                    (EntityEnterpriseContext) cache.get(key);
  +                   
  +                   
  +                   // Make sure we can work on it
  +                   Logger.debug("Checking:"+ctx.getId());
  +                            
  +                   //TODO do the Locking logic
  +                            try
  +                   {
  +                    Logger.debug("Passivating:"+ctx.getId());
  +                    
  +                                // Passivate the entry
  +                                
((EntityContainer)con).getPersistenceManager().passivateEntity(ctx);
  +                        
  +                    // Remove the entry      
  +                    cache.remove(key);
  +                    
  +                                //keep the count
  +                                currentActive--;
  +                   }
  +                    
  +                   catch (Exception e) { Logger.log("Could not passivate 
instance");}
  +                 
  +                   // Are we done?
  +                   if (currentActive == minActive) break;
  +                 }
  +              
  +              }    
  +          }  
  +          // DEBUG Logger.log("Passivation done");
  +                Logger.log("Passivation done");
  +          
  +          // Sleep
  +          try
  +          {
  +              Thread.sleep(timeout);
  +          } catch (InterruptedException e)
  +          {
  +              // Ignore
  +          }
  +         
  +         }
  +       }
  +    }
   }
  
  
  

Reply via email to