1 6 7 package org.jfox.ejb; 8 9 import java.rmi.NoSuchObjectException ; 10 import java.util.HashMap ; 11 import java.util.Iterator ; 12 import java.util.Map ; 13 import javax.ejb.SessionBean ; 14 15 import org.jfox.ioc.ComponentName; 16 import org.jfox.ioc.common.AbstractComponent; 17 import org.jfox.persistent.Persistenter; 18 import org.jfox.pool.AbstractObjectPool; 19 import org.jfox.pool.IndexableObjectPool; 20 import org.jfox.pool.ObjectFactory; 21 import org.jfox.pool.PoolableObject; 22 import org.jfox.pool.SimpleObjectPool; 23 24 27 28 public class StatefulObjectPool extends AbstractComponent implements IndexableObjectPool,EJBObjectPool { 29 private AbstractObjectPool pool = null; 30 private EJBObjectFactory factory = null; 31 private Map caches = null; 33 private Map timeouts = null; 35 36 private Bucket bucket = null; 37 38 final static long MAX_LIFE_TIME = 60 * 60 * 1000L; private final static long MAX_PASSIVATE_TIME = MAX_LIFE_TIME * 24 * 7; 42 private static StatefulManager lifecycleManger = new StatefulManager(); 43 private Persistenter persistent = null; 44 45 public StatefulObjectPool() { 46 47 } 48 49 public void setEJBObjectFactory(EJBObjectFactory factory) { 50 this.factory = factory; 51 } 52 53 public PoolableObject createObject() throws Exception { 54 return pool.retrieveObject(); 55 } 56 57 public synchronized PoolableObject retrieveObject(Object key) throws Exception { 58 timeouts.remove(key); 60 if(!caches.containsKey(key)) { 62 try { 63 EJBPoolableObject pobj = activateObject(key); 64 return pobj; 67 } 68 catch(Exception e) { 69 e.printStackTrace(); 70 } 71 throw new NoSuchObjectException (key.toString()); 73 } 74 else { 75 return (PoolableObject) caches.remove(key); 76 } 77 } 78 79 86 public boolean restoreObject(Object key, PoolableObject obj) { 87 timeouts.put(key, new Long (System.currentTimeMillis())); 89 90 if(caches.containsKey(key)) { 91 logger.warn("key " + key + " is existed,overwrite!"); 92 caches.put(key, obj); 93 return false; 94 } 95 else { 96 caches.put(key, obj); 97 return true; 98 } 99 } 100 101 public boolean removeObject(Object key) { 102 logger.debug("removeObject with key - " + key); 103 timeouts.remove(key); 104 try { 105 SessionBean sb = loadObject(key); 106 if(sb != null) { 107 bucket.ejbRemove(sb); 108 persistent.remove(key); 109 return true; 110 } 111 else { 112 return false; 113 } 114 } 115 catch(Exception e) { 116 logger.warn(e); 117 return false; 118 } 119 } 120 121 public boolean passivateObject(Object key) { 122 if(!caches.containsKey(key)) return false; 123 logger.debug("passivateObject with key - " + key); 124 EJBPoolableObject pobj = (EJBPoolableObject) caches.remove(key); 127 try { 128 bucket.ejbPassivate((SessionBean ) pobj.getBeanInstance()); 129 persistent.store(key, pobj.getBeanInstance()); 130 pobj = null; 131 return true; 132 } 133 catch(Exception e) { 134 logger.warn(e); 135 return false; 136 } 137 } 138 139 public EJBPoolableObject activateObject(Object key) throws Exception { 140 logger.debug("activateObject with key - " + key); 141 SessionBean sb = loadObject(key); 142 bucket.ejbActivate(sb); 143 EJBPoolableObject pobj = new EJBPoolableObject(); 144 pobj.setBeanInstance(sb); 145 return pobj; 146 } 147 148 private SessionBean loadObject(Object key) throws Exception { 149 SessionBean sb = (SessionBean ) persistent.load(key); 151 return sb; 152 } 153 154 public void clear() { 155 synchronized(caches) { 156 for(Iterator it = caches.values().iterator(); it.hasNext();) { 157 EJBPoolableObject pobj = (EJBPoolableObject) it.next(); 158 try { 159 bucket.ejbPassivate((SessionBean ) (((EJBPoolableObject) pobj).getBeanInstance())); 160 } 161 catch(Exception e) { 162 logger.warn("ejbRemove failed, exception is: " + e.toString()); 163 } 164 } 165 caches.clear(); 166 } 167 timeouts.clear(); 168 pool.clear(); 169 } 170 171 public ObjectFactory getObjectFactory() { 172 return factory; 173 } 174 175 public int getWorking() { 176 return caches.size(); 177 } 178 179 public int getRest() { 180 return pool.getRest(); 181 } 182 183 protected void doInit() throws Exception { 184 if(factory == null) throw new NullPointerException ("factory is null."); 185 bucket = factory.getBucket(); 186 timeouts = new HashMap (); 187 caches = new HashMap (); 188 pool = new SimpleObjectPool(factory); 189 pool.init(); 190 191 persistent = (Persistenter)context.getRegistry().getComponentInstance(ComponentName.parseString("org.jfox.persistent.Persistenter@File")); 192 193 lifecycleManger.manageIt(this); 194 } 195 196 protected void doDestroy() throws Exception { 197 clear(); 198 lifecycleManger.unmanageIt(this); 199 caches = null; 200 pool = null; 201 } 202 203 206 public void lifecycleCheck() { 207 logger.debug("Lifecycle checking... " + timeouts.size() + ", " + bucket.getEJBDescriptor().getRemoteClassName()); 208 long now = System.currentTimeMillis(); 209 if(timeouts.size() == 0) return; 211 for(Iterator it = new HashMap (timeouts).entrySet().iterator(); it.hasNext();) { 212 Map.Entry entry = (Map.Entry ) it.next(); 213 long lastActive = ((Long ) entry.getValue()).longValue(); 214 215 if((now - lastActive) > MAX_PASSIVATE_TIME) { 216 removeObject(entry.getKey()); 217 continue; 218 } 219 if((now - lastActive) > MAX_LIFE_TIME) { 220 passivateObject(entry.getKey()); 221 } 222 } 223 } 224 225 public static void main(String [] args) { 226 227 } 228 } | Popular Tags |