Swapper.java

00001 
00026 package org.objectweb.jonas_ejb.container;
00027 
00028 import java.util.LinkedList;
00029 import java.util.NoSuchElementException;
00030 
00031 import org.objectweb.util.monolog.api.BasicLevel;
00032 
00040 class Swapper extends Thread {
00041 
00045     private JContainer container;
00046 
00050     private boolean valid;
00051 
00055     private boolean syncallbeans;
00056 
00061     private int memorylow;
00062 
00066     private LinkedList bfList = new LinkedList();
00067 
00071     private LinkedList bfsList = new LinkedList();
00072 
00077     private int swapTimeout = 5 * 60;
00078 
00082     private int memoryCount = 10;
00083 
00088     public Swapper(JContainer cont) {
00089         super("JonasSwapper");
00090         valid = true;
00091         syncallbeans = false;
00092         memorylow = 0;
00093         container = cont;
00094     }
00095 
00099     public void run() {
00100 
00101         while (valid) {
00102             BeanFactory bf = null;
00103             BeanFactory bfs = null;
00104             synchronized (this) {
00105                 if (bfsList.size() == 0 && bfList.size() == 0) {
00106                     try {
00107                         wait(swapTimeout * 1000);
00108                     } catch (InterruptedException e) {
00109                         TraceEjb.logger.log(BasicLevel.ERROR, getName() + ": swapper interrupted");
00110                     } catch (Exception e) {
00111                         TraceEjb.logger.log(BasicLevel.ERROR, getName() + ": swapper exception", e);
00112                     }
00113                 }
00114                 try {
00115                     bf = (BeanFactory) bfList.removeFirst();
00116                 } catch (NoSuchElementException e) {
00117                     bf = null;
00118                 }
00119                 try {
00120                     bfs = (BeanFactory) bfsList.removeFirst();
00121                 } catch (NoSuchElementException e) {
00122                     bfs = null;
00123                 }
00124             }
00125             // This can be done with lock off.
00126             if (bf != null) {
00127                 TraceEjb.swapper.log(BasicLevel.DEBUG, "swapper will try to reduce the cache");
00128                 bf.reduceCache();
00129                 continue;
00130             }
00131             if (bfs != null) {
00132                 TraceEjb.swapper.log(BasicLevel.DEBUG, "swapper will sync dirty instances");
00133                 bfs.sync();
00134                 continue;
00135             }
00136             // We were notified on timeout, probably to sync all beans.
00137             if (memorylow >= 10) {
00138                 TraceEjb.swapper.log(BasicLevel.DEBUG, "swapper will passivate all entity factories");
00139                 container.syncAll(true);
00140                 memorylow = 0;
00141             } else if (syncallbeans) {
00142                 TraceEjb.swapper.log(BasicLevel.DEBUG, "swapper will sync all entity factories - " + memorylow);
00143                 container.syncAll(false);
00144                 memorylow++;
00145             }
00146         }
00147     }
00148 
00152     public synchronized void stopIt() {
00153         valid = false;
00154         notify();
00155     }
00156 
00161     public synchronized void addBeanFactorySync(BeanFactory bf) {
00162         if (!bfsList.contains(bf)) {
00163             TraceEjb.swapper.log(BasicLevel.DEBUG, "" + bfsList.size());
00164             bfsList.addLast(bf);
00165             notify();
00166         }
00167     }
00168 
00173     public synchronized void addBeanFactory(BeanFactory bf) {
00174         if (!bfList.contains(bf)) {
00175             TraceEjb.swapper.log(BasicLevel.DEBUG, "" + bfList.size());
00176             bfList.addLast(bf);
00177             // don't notify because it's better to wait that instances are released
00178             // before reducing the pool.
00179             // This is called typically when the pool is empty!
00180         }
00181     }
00182 
00187     public synchronized void setSwapperTimeout(int t) {
00188         TraceEjb.swapper.log(BasicLevel.DEBUG, "" + t);
00189         syncallbeans = true;
00190         if (t < swapTimeout) {
00191             swapTimeout = t;
00192         }
00193         notify();
00194     }
00195 
00201     public int getSwapperTimeout() {
00202         if (syncallbeans) {
00203             return swapTimeout;
00204         } else {
00205             return 0;
00206         }
00207     }
00208 }

Generated on Tue Feb 15 15:05:43 2005 for JOnAS by  doxygen 1.3.9.1