From: Sasa M. <sa...@us...> - 2004-09-27 13:58:48
|
Update of /cvsroot/jrobin/src/org/jrobin/core In directory sc8-pr-cvs1.sourceforge.net:/tmp/cvs-serv10260/org/jrobin/core Modified Files: RrdDbPool.java Log Message: Added shutdown hook to close all open RrdDb files held in the pool when the JVM is about to exit. Added method to close() the pool manualy (it's not harmful to close the file more than once). Index: RrdDbPool.java =================================================================== RCS file: /cvsroot/jrobin/src/org/jrobin/core/RrdDbPool.java,v retrieving revision 1.15 retrieving revision 1.16 diff -C2 -d -r1.15 -r1.16 *** RrdDbPool.java 23 Sep 2004 13:12:54 -0000 1.15 --- RrdDbPool.java 27 Sep 2004 13:58:36 -0000 1.16 *************** *** 102,108 **** public class RrdDbPool implements Runnable { static final String GC_THREAD_NAME = "RrdDbPool GC thread"; ! private static RrdDbPool ourInstance; private static final boolean DEBUG = false; /** * Constant to represent the maximum number of internally open RRD files --- 102,126 ---- public class RrdDbPool implements Runnable { static final String GC_THREAD_NAME = "RrdDbPool GC thread"; ! static final String CLOSING_THREAD_NAME = "RrdDbPool closing thread"; private static final boolean DEBUG = false; + // singleton pattern + private static RrdDbPool ourInstance; + + static { + Runtime.getRuntime().addShutdownHook(new Thread(CLOSING_THREAD_NAME) { + public void run() { + if(ourInstance != null) { + try { + ourInstance.close(); + } + catch (IOException e) { + e.printStackTrace(); + } + } + } + }); + } + /** * Constant to represent the maximum number of internally open RRD files *************** *** 111,114 **** --- 129,133 ---- public static final int INITIAL_CAPACITY = 500; private int capacity = INITIAL_CAPACITY, maxUsedCapacity; + private boolean active = true; /** *************** *** 166,169 **** --- 185,189 ---- */ public synchronized RrdDb requestRrdDb(String path) throws IOException, RrdException { + proveActive(); poolRequestsCount++; String canonicalPath = getCanonicalPath(path); *************** *** 227,230 **** --- 247,251 ---- private RrdDb requestNewRrdDb(String path, Object creationDef) throws IOException, RrdException { + proveActive(); poolRequestsCount++; String canonicalPath = getCanonicalPath(path); *************** *** 326,329 **** --- 347,351 ---- */ public synchronized void release(RrdDb rrdDb) throws IOException, RrdException { + proveActive(); if (rrdDb == null) { // we don't want NullPointerException *************** *** 354,358 **** public void run() { // debug("GC: started"); ! for (; ;) { synchronized (this) { if (rrdMap.size() >= capacity && rrdIdleMap.size() > 0) { --- 376,380 ---- public void run() { // debug("GC: started"); ! while (active) { synchronized (this) { if (rrdMap.size() >= capacity && rrdIdleMap.size() > 0) { *************** *** 381,385 **** protected void finalize() throws IOException { ! reset(); } --- 403,407 ---- protected void finalize() throws IOException { ! close(); } *************** *** 400,403 **** --- 422,441 ---- } + /** + * Closes the pool and all RRD files currently held in the pool. + * No further operations on the pool are allowed. + * @throws IOException Thrown in case of I/O error. + */ + public synchronized void close() throws IOException { + if(active) { + active = false; + reset(); + // debug("The pool is closed."); + } + else { + // debug("The pool is already closed!"); + } + } + private static String getCanonicalPath(String path) throws IOException { return RrdFileBackend.getCanonicalPath(path); *************** *** 583,587 **** * limit on the number of simultaneously open files), <code>false</code> otherwise. */ ! public boolean isLimitedCapacity() { return limitedCapacity; } --- 621,625 ---- * limit on the number of simultaneously open files), <code>false</code> otherwise. */ ! public synchronized boolean isLimitedCapacity() { return limitedCapacity; } *************** *** 599,605 **** * limit on the number of simultaneously open files), <code>false</code> otherwise. */ ! public void setLimitedCapacity(boolean limitedCapacity) { this.limitedCapacity = limitedCapacity; } } --- 637,658 ---- * limit on the number of simultaneously open files), <code>false</code> otherwise. */ ! public synchronized void setLimitedCapacity(boolean limitedCapacity) { this.limitedCapacity = limitedCapacity; } + + private void proveActive() throws IOException { + if(!active) { + throw new IOException("RrdDbPool is already closed"); + } + } + + /** + * Checks if the pool is active. You can request RrdDb references only from the active pool. The + * pool is deactived when the {@link #close()} method is called. + * @return <code>true</code> if active, <code>false</code> otherwise. + */ + public synchronized boolean isActive() { + return active; + } } |