From: <bsc...@us...> - 2007-02-01 13:45:31
|
Revision: 199 http://svn.sourceforge.net/unicore/?rev=199&view=rev Author: bschuller Date: 2007-02-01 05:45:20 -0800 (Thu, 01 Feb 2007) Log Message: ----------- add ehcache based cache on the persistence Modified Paths: -------------- wsrflite/wsrfcore/trunk/src/main/java/de/fzj/unicore/wsrflite/impl/ExpiryChecker.java wsrflite/wsrfcore/trunk/src/main/java/de/fzj/unicore/wsrflite/persistence/HsqldbPersist.java wsrflite/wsrfcore/trunk/src/test/java/de/fzj/unicore/wsrflite/functional/LoadTestExpiryCheck.java wsrflite/wsrfcore/trunk/src/test/java/de/fzj/unicore/wsrflite/functional/LoadTestPersistence.java wsrflite/wsrfcore/trunk/src/test/java/de/fzj/unicore/wsrflite/impl/TestExpireWSResources.java wsrflite/wsrfcore/trunk/src/test/java/de/fzj/unicore/wsrflite/impl/TestWSResourceHomeImpl.java Added Paths: ----------- wsrflite/wsrfcore/trunk/src/main/java/de/fzj/unicore/wsrflite/persistence/EhCache.java Modified: wsrflite/wsrfcore/trunk/src/main/java/de/fzj/unicore/wsrflite/impl/ExpiryChecker.java =================================================================== --- wsrflite/wsrfcore/trunk/src/main/java/de/fzj/unicore/wsrflite/impl/ExpiryChecker.java 2007-02-01 11:52:32 UTC (rev 198) +++ wsrflite/wsrfcore/trunk/src/main/java/de/fzj/unicore/wsrflite/impl/ExpiryChecker.java 2007-02-01 13:45:20 UTC (rev 199) @@ -39,7 +39,6 @@ import de.fzj.unicore.wsrflite.Home; import de.fzj.unicore.wsrflite.ResourceUnknownException; -import de.fzj.unicore.wsrflite.utils.StopWatch; /** * Takes care of lifetime expiry.<br/> Added: wsrflite/wsrfcore/trunk/src/main/java/de/fzj/unicore/wsrflite/persistence/EhCache.java =================================================================== --- wsrflite/wsrfcore/trunk/src/main/java/de/fzj/unicore/wsrflite/persistence/EhCache.java (rev 0) +++ wsrflite/wsrfcore/trunk/src/main/java/de/fzj/unicore/wsrflite/persistence/EhCache.java 2007-02-01 13:45:20 UTC (rev 199) @@ -0,0 +1,100 @@ +package de.fzj.unicore.wsrflite.persistence; + +import java.io.ByteArrayInputStream; +import java.util.logging.Level; +import java.util.logging.Logger; + +import de.fzj.unicore.wsrflite.Kernel; +import de.fzj.unicore.wsrflite.WSRFInstance; + +import net.sf.ehcache.Cache; +import net.sf.ehcache.CacheException; +import net.sf.ehcache.CacheManager; +import net.sf.ehcache.Element; + +/** + * use ehcache for wsresource instances + * + * @author schuller + */ +public class EhCache { + protected static Logger log=Logger.getLogger(EhCache.class.getName()); + + private static CacheManager cacheManager; + + private net.sf.ehcache.Cache cache; + + /** + * default config for ehcache</br> + * This is an in-memory cache only, + * using the default LRU policy + */ + private String defaultConfig="<ehcache>\n" + + "<defaultCache maxElementsInMemory=\"100\"\n"+ + "eternal=\"true\"\n"+ + "timeToIdleSeconds=\"10\"\n"+ + "timeToLiveSeconds=\"10\"\n"+ + "overflowToDisk=\"false\"\n"+ + "diskPersistent=\"false\"\n"+ + "diskExpiryThreadIntervalSeconds=\"120\"/>\n"+ + "</ehcache>"; + + + /** + * factory method + * @param id + * @return + */ + public static EhCache createCache(String id) { + return new EhCache(id); + } + + private EhCache(String id){ + if(cacheManager==null)initCacheManager(); + if(!cacheManager.cacheExists(id))cacheManager.addCache(id); + cache=cacheManager.getCache(id); + } + + private synchronized void initCacheManager(){ + String cFile=null; + try { + cFile=Kernel.getKernel().getProperty("CACHE.configfile"); + }catch(Exception e){} + try{ + if(cFile!=null){ + log.config("EHCache configuration read from "+cFile); + cacheManager=CacheManager.create(cFile); + } + else{ + log.config("Using default EHCache configuration: \n"+defaultConfig); + ByteArrayInputStream bis=new ByteArrayInputStream(defaultConfig.getBytes()); + cacheManager=CacheManager.create(bis); + } + } catch (CacheException e) { + log.log(Level.SEVERE,"Error creating cache manager.",e); + } + } + + public WSRFInstance read(String uniqueID){ + try{ + Element e=cache.get(uniqueID); + if(e!=null){ + return (WSRFInstance)(e.getValue()); + } + }catch(Exception e){ + log.log(Level.WARNING,"",e); + } + return null; + } + public void put(WSRFInstance inst){ + try{ + cache.put(new Element(inst.getUniqueID(),inst)); + }catch(Exception e){ + log.log(Level.WARNING,"",e); + } + } + + public Cache getCache(){ + return cache; + } +} Modified: wsrflite/wsrfcore/trunk/src/main/java/de/fzj/unicore/wsrflite/persistence/HsqldbPersist.java =================================================================== --- wsrflite/wsrfcore/trunk/src/main/java/de/fzj/unicore/wsrflite/persistence/HsqldbPersist.java 2007-02-01 11:52:32 UTC (rev 198) +++ wsrflite/wsrfcore/trunk/src/main/java/de/fzj/unicore/wsrflite/persistence/HsqldbPersist.java 2007-02-01 13:45:20 UTC (rev 199) @@ -75,6 +75,8 @@ private Set<String> uniqueIDs=null; + private EhCache cache; + public static final String clearDBOnStartup="de.fzj.unicore.wsrflite.hsqldb.clearAllOnStartup"; protected String createConnString(String serviceName){ @@ -163,6 +165,12 @@ { logger.log(Level.SEVERE,"Could not add shutdown hook."); } + try{ + cache=EhCache.createCache(serviceName); + } + catch(Exception e){ + logger.log(Level.SEVERE,"Could not create cache."); + } } @@ -221,37 +229,43 @@ public void persist(WSRFInstance inst) { try { synchronized(conn){ - String uniqueId=inst.getUniqueID(); - WSRFInstanceBean bean= - new WSRFInstanceBean( - uniqueId, - inst.getServiceName(), - inst.getClass().getName(), - inst.passivate()); - - if(uniqueIDs.contains(uniqueId)) {//update - psUpdate.setObject(1,bean); - psUpdate.setString(2,uniqueId); - psUpdate.execute(); - if(logger.isLoggable(Level.FINEST)){ - logger.finest("["+serviceName+"] "+psInsert.getUpdateCount()+" updated."); - } - } - else{ //insert - psInsert.setObject(2,bean); - psInsert.setString(1,uniqueId); - psInsert.execute(); - uniqueIDs.add(uniqueId); - if(logger.isLoggable(Level.FINEST)){ - logger.finest("["+serviceName+"] "+psInsert.getUpdateCount()+" inserted."); - } - } + cache.put(inst); + _persist(inst); } } catch (SQLException e) { logger.log(Level.WARNING,"Error persisting.",e); } } + protected void _persist(WSRFInstance inst)throws SQLException{ + String uniqueId=inst.getUniqueID(); + WSRFInstanceBean bean= + new WSRFInstanceBean( + uniqueId, + inst.getServiceName(), + inst.getClass().getName(), + inst.passivate()); + + if(uniqueIDs.contains(uniqueId)) {//update + psUpdate.setObject(1,bean); + psUpdate.setString(2,uniqueId); + psUpdate.execute(); + if(logger.isLoggable(Level.FINEST)){ + logger.finest("["+serviceName+"] "+psInsert.getUpdateCount()+" updated."); + } + } + else{ //insert + psInsert.setObject(2,bean); + psInsert.setString(1,uniqueId); + psInsert.execute(); + uniqueIDs.add(uniqueId); + if(logger.isLoggable(Level.FINEST)){ + logger.finest("["+serviceName+"] "+psInsert.getUpdateCount()+" inserted."); + } + } + } + + public String[] getUniqueIDs() { synchronized(conn){ if(uniqueIDs==null){ @@ -291,42 +305,60 @@ } synchronized(conn){ - try{ - logger.info("Read "+uniqueID); - psRead.setString(1,uniqueID); - ResultSet r=psRead.executeQuery(); - if(r.next()){ - Object o=r.getObject(1); - WSRFInstanceBean bean=(WSRFInstanceBean)o; - WSRFInstance i=(WSRFInstance)(Class.forName(bean.className).newInstance()); - i.setUniqueID(bean.uniqueID); - i.setServiceName(bean.serviceName); - i.activate(bean.state); - return i; - } - else logger.log(Level.FINE, "Not found: "+uniqueID); + WSRFInstance inst=cache.read(uniqueID); + if(inst==null){ + inst=_read(uniqueID); + cache.put(inst); + } + return inst; + } + } + + protected WSRFInstance _read(String uniqueID) { + try{ + //logger.info("Read "+uniqueID); + psRead.setString(1,uniqueID); + ResultSet r=psRead.executeQuery(); + if(r.next()){ + Object o=r.getObject(1); + WSRFInstanceBean bean=(WSRFInstanceBean)o; + WSRFInstance i=(WSRFInstance)(Class.forName(bean.className).newInstance()); + i.setUniqueID(bean.uniqueID); + i.setServiceName(bean.serviceName); + i.activate(bean.state); + return i; + } + else logger.log(Level.FINE, "Not found: "+uniqueID); }catch(Exception e){ logger.log(Level.SEVERE,"Error",e); } return null; - } } - public void remove(String uniqueID){ synchronized (conn) { - try{ - psDelete.setString(1,uniqueID); - psDelete.execute(); - uniqueIDs.remove(uniqueID); - }catch(Exception e){ - logger.log(Level.WARNING,"Error",e); - } + _remove(uniqueID); } } + protected void _remove(String uniqueID){ + try{ + psDelete.setString(1,uniqueID); + psDelete.execute(); + uniqueIDs.remove(uniqueID); + }catch(Exception e){ + logger.log(Level.WARNING,"Error",e); + } + } + public int size(){ return getUniqueIDs().length; } + public int getCacheHits(){ + try{ + return cache.getCache().getHitCount(); + }catch(Exception e){return 0;} + } + } Modified: wsrflite/wsrfcore/trunk/src/test/java/de/fzj/unicore/wsrflite/functional/LoadTestExpiryCheck.java =================================================================== --- wsrflite/wsrfcore/trunk/src/test/java/de/fzj/unicore/wsrflite/functional/LoadTestExpiryCheck.java 2007-02-01 11:52:32 UTC (rev 198) +++ wsrflite/wsrfcore/trunk/src/test/java/de/fzj/unicore/wsrflite/functional/LoadTestExpiryCheck.java 2007-02-01 13:45:20 UTC (rev 199) @@ -11,7 +11,6 @@ import de.fzj.unicore.wsrflite.persistence.PersistenceManager; import de.fzj.unicore.wsrflite.utils.StopWatch; import de.fzj.unicore.wsrflite.xfire.XFireKernel; -import de.fzj.unicore.wsrflite.xmlbeans.impl.WSResourceHomeImpl; public class LoadTestExpiryCheck extends TestCase { Modified: wsrflite/wsrfcore/trunk/src/test/java/de/fzj/unicore/wsrflite/functional/LoadTestPersistence.java =================================================================== --- wsrflite/wsrfcore/trunk/src/test/java/de/fzj/unicore/wsrflite/functional/LoadTestPersistence.java 2007-02-01 11:52:32 UTC (rev 198) +++ wsrflite/wsrfcore/trunk/src/test/java/de/fzj/unicore/wsrflite/functional/LoadTestPersistence.java 2007-02-01 13:45:20 UTC (rev 199) @@ -55,7 +55,6 @@ */ public long runTimed(int n, int size)throws Exception{ - System.out.println("Persisting "+n+" instances of "+size+" bytes"); Calendar c=new GregorianCalendar(2071,3,30); Modified: wsrflite/wsrfcore/trunk/src/test/java/de/fzj/unicore/wsrflite/impl/TestExpireWSResources.java =================================================================== --- wsrflite/wsrfcore/trunk/src/test/java/de/fzj/unicore/wsrflite/impl/TestExpireWSResources.java 2007-02-01 11:52:32 UTC (rev 198) +++ wsrflite/wsrfcore/trunk/src/test/java/de/fzj/unicore/wsrflite/impl/TestExpireWSResources.java 2007-02-01 13:45:20 UTC (rev 199) @@ -33,18 +33,13 @@ package de.fzj.unicore.wsrflite.impl; -import java.io.Serializable; +import java.util.Calendar; import java.util.Map; import java.util.concurrent.Executors; import java.util.concurrent.ScheduledExecutorService; import java.util.concurrent.TimeUnit; -import javax.xml.namespace.QName; - import junit.framework.TestCase; - -import org.codehaus.xfire.MessageContext; - import de.fzj.unicore.wsrflite.Home; import de.fzj.unicore.wsrflite.WSRFInstance; @@ -54,90 +49,58 @@ protected void setUp(){ - home=new Home(){ - - public String getServiceName(){return "test123";} - - public void activateHome(String n){}; - - public void passivateHome(){}; - - public String createWSRFServiceInstance(Map<String,Object> m) { - return null; - } - public void destroyWSRFServiceInstance(String resourceId) { - if(!resourceId.equals("testid")) throw new RuntimeException(); - } - - public Object getWSRFServiceInstance(MessageContext context){ - return null; - } - public WSRFInstance getWSRFServiceInstance(String resourceId) { - if(resourceId.equals(item.getUniqueID()))return item; - return null; - } - public long getNumberOfInstances(){return 0;} - public void postActivate(){} - - }; - item=new WSRFInstance(){ + + item=new WSRFInstanceImpl(){ protected static final long serialVersionUID=20332448L; - private String uniqueid; - private String serviceName; - public String getServiceName() { - return serviceName; - } - public void setServiceName(String n) { - serviceName=n; - } - - public void postActivate(){} - - public void initialise(String serviceName, Map<String, Object> initobjs) { - } - public boolean hasChanged() { return false; } - public void setUniqueID(String id) { - + public String getUniqueID() { + return "testid"; } - public void activate(Map<String, Serializable> context) { - } - - public void destroy() { - - } + @Override public boolean isExpired() { return true; } - public Map<String, Serializable> passivate() { - return null; + @Override + public void initialise(String serviceName, Map<String, Object> initobjs) throws Exception { } - public QName[] getResourcePropertyQNames() { - return null; - } + }; + home=new DefaultHome(){ - public String getUniqueID() { - return "testid"; - } - public Map<String, Object> getSecurityContext() { - // TODO Auto-generated method stub + public String getServiceName(){return "test123";} + + public void activateHome(String n){ + terminationTimes.put("testid", Calendar.getInstance()); + }; + + public void passivateHome(){}; + + @Override + protected WSRFInstance doCreateInstance() { return null; } - public void setSecurityContext(Map<String, Object> ctx) { - // TODO Auto-generated method stub - + + public void destroyWSRFServiceInstance(String resourceId) { + if(!resourceId.equals("testid")) throw new RuntimeException(); } + public WSRFInstance getWSRFServiceInstance(String resourceId) { + if(resourceId.equals(item.getUniqueID()))return item; + return null; + } + }; + home.activateHome("test"); } public void test1(){ + ExpiryChecker ex=new ExpiryChecker(home, new String[0]); + assertTrue(ex.add(item.getUniqueID())); ex.run(); assertFalse(ex.remove(item.getUniqueID())); Modified: wsrflite/wsrfcore/trunk/src/test/java/de/fzj/unicore/wsrflite/impl/TestWSResourceHomeImpl.java =================================================================== --- wsrflite/wsrfcore/trunk/src/test/java/de/fzj/unicore/wsrflite/impl/TestWSResourceHomeImpl.java 2007-02-01 11:52:32 UTC (rev 198) +++ wsrflite/wsrfcore/trunk/src/test/java/de/fzj/unicore/wsrflite/impl/TestWSResourceHomeImpl.java 2007-02-01 13:45:20 UTC (rev 199) @@ -84,7 +84,7 @@ String id=home.createWSRFServiceInstance(new HashMap<String,Object>()); WSResource wsr=(WSResource)home.getWSRFServiceInstance(id); assertNotNull(wsr); - //set expiry to 500 millisec from now + //set expiry to 50 millisec from now SetTerminationTimeDocument st=SetTerminationTimeDocument.Factory.newInstance(); Calendar cal=new GregorianCalendar(); cal.add(Calendar.MILLISECOND,50); @@ -92,7 +92,7 @@ try{ wsr.SetTerminationTime(st); Thread.sleep(1200); - home.getWSRFServiceInstance(id+"xx"); + home.getWSRFServiceInstance(id); fail("Service instance should be null"); }catch(Exception e){ //ok This was sent by the SourceForge.net collaborative development platform, the world's largest Open Source development site. |