1 22 package org.jboss.ejb.plugins; 23 24 import java.util.TimerTask ; 25 26 import org.jboss.deployment.DeploymentException; 27 import org.jboss.metadata.MetaData; 28 import org.w3c.dom.Element ; 29 30 36 public class LRUStatefulContextCachePolicy extends LRUEnterpriseContextCachePolicy 37 { 38 40 42 private long m_maxBeanLife; 43 44 private TimerTask m_remover; 45 46 private long m_removerPeriod; 47 50 private StatefulSessionInstanceCache ssiCache; 51 52 54 59 public LRUStatefulContextCachePolicy(AbstractInstanceCache eic) 60 { 61 super(eic); 62 ssiCache = (StatefulSessionInstanceCache) eic; 63 } 64 65 67 69 public void start() 71 { 72 super.start(); 73 if (m_maxBeanLife > 0) 74 { 75 m_remover = new RemoverTask(m_removerPeriod); 76 long delay = (long) (Math.random() * m_removerPeriod); 77 tasksTimer.schedule(m_remover, delay, m_removerPeriod); 78 } 79 } 80 81 public void stop() 82 { 83 if (m_remover != null) 84 { 85 m_remover.cancel(); 86 } 87 super.stop(); 88 } 89 90 94 public void importXml(Element element) throws DeploymentException 95 { 96 super.importXml(element); 97 98 String rp = MetaData.getElementContent(MetaData.getOptionalChild(element, "remover-period")); 99 String ml = MetaData.getElementContent(MetaData.getOptionalChild(element, "max-bean-life")); 100 try 101 { 102 if (rp != null) 103 { 104 int p = Integer.parseInt(rp); 105 if (p <= 0) 106 { 107 throw new DeploymentException("Remover period can't be <= 0"); 108 } 109 m_removerPeriod = p * 1000; 110 } 111 if (ml != null) 112 { 113 int a = Integer.parseInt(ml); 114 if (a <= 0) 115 { 116 throw new DeploymentException("Max bean life can't be <= 0"); 117 } 118 m_maxBeanLife = a * 1000; 119 } 120 } 121 catch (NumberFormatException x) 122 { 123 throw new DeploymentException("Can't parse policy configuration", x); 124 } 125 } 126 127 129 131 133 135 139 protected class RemoverTask extends OveragerTask 140 { 141 protected RemoverTask(long period) 142 { 143 super(period); 144 } 145 146 protected String getTaskLogMessage() 147 { 148 return "Removing from cache bean"; 149 } 150 151 protected void kickOut(LRUCacheEntry entry) 152 { 153 remove(entry.m_key); 154 } 155 156 protected long getMaxAge() 157 { 158 return m_maxBeanLife; 159 } 160 161 public void run() 162 { 163 if (ssiCache == null) 164 { 165 cancel(); 166 return; 167 } 168 169 synchronized (ssiCache.getCacheLock()) 170 { 171 log.debug("Running RemoverTask"); 172 super.run(); 174 log.debug("RemoverTask, PassivatedCount=" + ssiCache.getPassivatedCount()); 175 } 176 try 177 { 178 ssiCache.removePassivated(getMaxAge() - super.getMaxAge()); 180 log.debug("RemoverTask, done"); 181 } 182 catch (Throwable t) 183 { 184 log.debug("Ignored error trying to remove passivated beans from cache", t); 185 } 186 } 187 } 188 } 189 | Popular Tags |