1 25 26 package org.objectweb.jonas_ejb.container; 27 28 import java.util.LinkedList ; 29 import java.util.NoSuchElementException ; 30 31 import org.objectweb.util.monolog.api.BasicLevel; 32 33 40 class Swapper extends Thread { 41 42 45 private JContainer container; 46 47 50 private boolean valid; 51 52 55 private boolean syncallbeans; 56 57 61 private int memorylow; 62 63 66 private LinkedList bfList = new LinkedList (); 67 68 71 private LinkedList bfsList = new LinkedList (); 72 73 77 private int swapTimeout = 5 * 60; 78 79 82 private int memoryCount = 10; 83 84 88 public Swapper(JContainer cont) { 89 super("JonasSwapper"); 90 valid = true; 91 syncallbeans = false; 92 memorylow = 0; 93 container = cont; 94 } 95 96 99 public void run() { 100 101 while (valid) { 102 BeanFactory bf = null; 103 BeanFactory bfs = null; 104 synchronized (this) { 105 if (bfsList.size() == 0 && bfList.size() == 0) { 106 try { 107 wait(swapTimeout * 1000); 108 } catch (InterruptedException e) { 109 TraceEjb.logger.log(BasicLevel.ERROR, getName() + ": swapper interrupted", e); 110 } catch (Exception e) { 111 TraceEjb.logger.log(BasicLevel.ERROR, getName() + ": swapper exception", e); 112 } 113 } 114 try { 115 bf = (BeanFactory) bfList.removeFirst(); 116 } catch (NoSuchElementException e) { 117 bf = null; 118 } 119 try { 120 bfs = (BeanFactory) bfsList.removeFirst(); 121 } catch (NoSuchElementException e) { 122 bfs = null; 123 } 124 } 125 if (bf != null) { 127 if (TraceEjb.isDebugSwapper()) { 128 TraceEjb.swapper.log(BasicLevel.DEBUG, "swapper will try to reduce the cache"); 129 } 130 bf.reduceCache(); 131 continue; 132 } 133 if (bfs != null) { 134 if (TraceEjb.isDebugSwapper()) { 135 TraceEjb.swapper.log(BasicLevel.DEBUG, "swapper will sync dirty instances"); 136 } 137 bfs.sync(); 138 continue; 139 } 140 if (memorylow >= 10) { 142 if (TraceEjb.isDebugSwapper()) { 143 TraceEjb.swapper.log(BasicLevel.DEBUG, "swapper will passivate all entity factories"); 144 } 145 container.syncAll(true); 146 memorylow = 0; 147 } else if (syncallbeans) { 148 if (TraceEjb.isDebugSwapper()) { 149 TraceEjb.swapper.log(BasicLevel.DEBUG, "swapper will sync all entity factories - " + memorylow); 150 } 151 container.syncAll(false); 152 memorylow++; 153 } 154 } 155 } 156 157 160 public synchronized void stopIt() { 161 valid = false; 162 notify(); 163 } 164 165 169 public synchronized void addBeanFactorySync(BeanFactory bf) { 170 if (!bfsList.contains(bf)) { 171 if (TraceEjb.isDebugSwapper()) { 172 TraceEjb.swapper.log(BasicLevel.DEBUG, "" + bfsList.size()); 173 } 174 bfsList.addLast(bf); 175 notify(); 176 } 177 } 178 179 183 public synchronized void addBeanFactory(BeanFactory bf) { 184 if (!bfList.contains(bf)) { 185 if (TraceEjb.isDebugSwapper()) { 186 TraceEjb.swapper.log(BasicLevel.DEBUG, "" + bfList.size()); 187 } 188 bfList.addLast(bf); 189 } 193 } 194 195 199 public synchronized void setSwapperTimeout(int t) { 200 if (TraceEjb.isDebugSwapper()) { 201 TraceEjb.swapper.log(BasicLevel.DEBUG, "" + t); 202 } 203 syncallbeans = true; 204 if (t < swapTimeout) { 205 swapTimeout = t; 206 } 207 notify(); 208 } 209 210 215 public int getSwapperTimeout() { 216 if (syncallbeans) { 217 return swapTimeout; 218 } else { 219 return 0; 220 } 221 } 222 } 223 | Popular Tags |