Main Page | Packages | Class Hierarchy | Alphabetical List | Class List | File List | Class Members | Related Pages

EagerCacheThread.java

00001 /**
00002  * C-JDBC: Clustered JDBC.
00003  * Copyright (C) 2002-2005 French National Institute For Research In Computer
00004  * Science And Control (INRIA).
00005  * Contact: c-jdbc@objectweb.org
00006  * 
00007  * This library is free software; you can redistribute it and/or modify it
00008  * under the terms of the GNU Lesser General Public License as published by the
00009  * Free Software Foundation; either version 2.1 of the License, or any later
00010  * version.
00011  * 
00012  * This library is distributed in the hope that it will be useful, but WITHOUT
00013  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
00014  * FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License
00015  * for more details.
00016  * 
00017  * You should have received a copy of the GNU Lesser General Public License
00018  * along with this library; if not, write to the Free Software Foundation,
00019  * Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA.
00020  *
00021  * Initial developer(s): Nicolas Modrzyk.
00022  * Contributor(s): ______________________.
00023  */
00024 
00025 package org.objectweb.cjdbc.controller.cache.result.threads;
00026 
00027 import java.util.ArrayList;
00028 import java.util.Iterator;
00029 
00030 import org.objectweb.cjdbc.common.i18n.Translate;
00031 import org.objectweb.cjdbc.common.log.Trace;
00032 import org.objectweb.cjdbc.controller.cache.result.ResultCache;
00033 import org.objectweb.cjdbc.controller.cache.result.entries.ResultCacheEntryEager;
00034 
00035 /**
00036  * This thread manages eager cache entries and remove them from the cache if
00037  * they have expired.
00038  * 
00039  * @author <a href="mailto:Nicolas.Modrzyk@inrialpes.fr">Nicolas Modrzyk </a>
00040  * @version 1.0
00041  */
00042 public final class EagerCacheThread extends Thread
00043 {
00044   private final ResultCache cache;
00045   private long              threadWakeUpTime = 0;
00046   int                       refreshCacheRate = 60;
00047   int                       refreshCacheTime = 60 / refreshCacheRate;
00048   private Trace             logger           = Trace
00049                                                  .getLogger(EagerCacheThread.class
00050                                                      .getName());
00051   private boolean           isKilled         = false;
00052 
00053   /**
00054    * Creates a new <code>EagerCacheThread</code> object
00055    * 
00056    * @param cache ResultCache creating this thread
00057    */
00058   public EagerCacheThread(ResultCache cache)
00059   {
00060     super("EagerCacheThread");
00061     this.cache = cache;
00062   }
00063 
00064   /**
00065    * @see java.lang.Runnable#run()
00066    */
00067   public void run()
00068   {
00069     ResultCacheEntryEager entry;
00070     long now;
00071     long sleep;
00072     // Keep trace of relaxed cache entries to delete
00073     ArrayList toRemoveFromEagerCache = new ArrayList();
00074     while (!isKilled)
00075     {
00076       synchronized (this)
00077       {
00078         try
00079         {
00080           threadWakeUpTime = 0;
00081           if (cache.getEagerCache().isEmpty())
00082           { // Nothing in the cache, just sleep!
00083             if (logger.isDebugEnabled())
00084               logger.debug(Translate.get("cachethread.cache.empty.sleeping"));
00085             wait();
00086           }
00087           else
00088           { // Look for first deadline
00089             now = System.currentTimeMillis();
00090             for (Iterator iter = cache.getEagerCache().iterator(); iter
00091                 .hasNext();)
00092             {
00093               entry = (ResultCacheEntryEager) iter.next();
00094               if (entry.getDeadline() < now)
00095               { // Deadline has expired, remove entry
00096                 toRemoveFromEagerCache.add(entry);
00097                 continue;
00098               }
00099 
00100               // Recompute next wakeup time
00101               if ((threadWakeUpTime == 0)
00102                   || (entry.getDeadline() < threadWakeUpTime))
00103                 threadWakeUpTime = entry.getDeadline();
00104             }
00105 
00106             // Clean up all the entries from the eager cache
00107             int size = toRemoveFromEagerCache.size();
00108             for (int i = 0; i < size; i++)
00109             {
00110               entry = (ResultCacheEntryEager) toRemoveFromEagerCache.get(i);
00111               if (logger.isDebugEnabled())
00112                 logger.debug(Translate.get(
00113                     "cachethread.remove.entry.from.cache", entry.getRequest()
00114                         .getSQL()));
00115               try
00116               {
00117                 cache.removeFromCache(entry.getRequest());
00118               }
00119               catch (Exception e)
00120               {
00121                 logger.warn("cachethread.remove.entry.error", e);
00122               }
00123               try
00124               {
00125                 cache.getEagerCache().remove(entry);
00126               }
00127               catch (Exception e)
00128               {
00129                 logger.warn("cachethread.remove.entry.error", e);
00130               }
00131             }
00132             toRemoveFromEagerCache.clear();
00133             if (threadWakeUpTime == 0)
00134             { // All entries were not kept in the cache, therefore
00135               // there is no next deadline. (and no cache entry to wait for)
00136               continue;
00137             }
00138             else
00139             { // Sleep until the next deadline
00140               sleep = (threadWakeUpTime - now) / 1000 + refreshCacheTime;
00141               if (logger.isDebugEnabled())
00142               {
00143                 logger.debug(Translate.get("cachethread.sleeping", sleep));
00144               }
00145               sleep = (sleep) * 1000;
00146               wait(sleep);
00147             }
00148           }
00149         }
00150         catch (Exception e)
00151         {
00152           logger.warn(e.getMessage(), e);
00153         }
00154       }
00155     }
00156   }
00157 
00158   /**
00159    * Returns the threadWakeUpTime value.
00160    * 
00161    * @return Returns the threadWakeUpTime.
00162    */
00163   public long getThreadWakeUpTime()
00164   {
00165     return threadWakeUpTime;
00166   }
00167 }

Generated on Mon Apr 11 22:01:32 2005 for C-JDBC by  doxygen 1.3.9.1