Update of /cvsroot/babeldoc/babeldoc/modules/core/src/com/babeldoc/core/pipeline/processor
In directory sc8-pr-cvs1:/tmp/cvs-serv28668/src/com/babeldoc/core/pipeline/processor
Modified Files:
ThreadPooledPipelineStageProcessor.java
Log Message:
Added the shutdown listener framework. Classes that want to be told when babeldoc is about to be shutdown implement the IShutdownListener and call EnvironmentLoader.addShutdownListener.
Index: ThreadPooledPipelineStageProcessor.java
===================================================================
RCS file: /cvsroot/babeldoc/babeldoc/modules/core/src/com/babeldoc/core/pipeline/processor/ThreadPooledPipelineStageProcessor.java,v
retrieving revision 1.3
retrieving revision 1.4
diff -C2 -d -r1.3 -r1.4
*** ThreadPooledPipelineStageProcessor.java 8 Aug 2003 23:43:11 -0000 1.3
--- ThreadPooledPipelineStageProcessor.java 12 Aug 2003 00:28:29 -0000 1.4
***************
*** 69,72 ****
--- 69,74 ----
import com.babeldoc.core.LogService;
import com.babeldoc.core.I18n;
+ import com.babeldoc.core.IShutdownListener;
+ import com.babeldoc.core.EnvironmentLoader;
import com.babeldoc.core.option.IConfigInfo;
import com.babeldoc.core.option.ConfigOption;
***************
*** 76,80 ****
import org.apache.commons.lang.NumberUtils;
- import EDU.oswego.cs.dl.util.concurrent.Executor;
import EDU.oswego.cs.dl.util.concurrent.PooledExecutor;
--- 78,81 ----
***************
*** 84,94 ****
*/
public class ThreadPooledPipelineStageProcessor
! extends SyncPipelineStageProcessor {
public static final String POOL_SIZE = "poolSize";
public static final int DEFAULT_POOLSIZE = 5;
private int poolsize;
! private Executor threadPool;
/**
--- 85,99 ----
*/
public class ThreadPooledPipelineStageProcessor
! extends SyncPipelineStageProcessor
! implements IShutdownListener {
public static final String POOL_SIZE = "poolSize";
+ public static final String KEEP_ALIVE = "keepAlive";
public static final int DEFAULT_POOLSIZE = 5;
+ public static final int DEFAULT_KEEPALIVE = 15*1000; // 15 seconds
private int poolsize;
! private int keepAlive;
! private PooledExecutor threadPool;
/**
***************
*** 102,107 ****
poolsize = NumberUtils.stringToInt((String)config.get(POOL_SIZE), DEFAULT_POOLSIZE);
! // System.out.println("Setting up a pool with size of: "+poolsize);
threadPool = new PooledExecutor(poolsize);
}
--- 107,114 ----
poolsize = NumberUtils.stringToInt((String)config.get(POOL_SIZE), DEFAULT_POOLSIZE);
! keepAlive = NumberUtils.stringToInt((String)config.get(KEEP_ALIVE), DEFAULT_KEEPALIVE);
threadPool = new PooledExecutor(poolsize);
+ threadPool.setKeepAliveTime(keepAlive);
+ EnvironmentLoader.addShutdownListener(this);
}
***************
*** 120,124 ****
throws PipelineException {
// ensure synchronized access to finalResults Collection if threads are spawned
! final Collection syncResults = Collections.synchronizedCollection(finalResults);
final int numResults = psResults.length;
for (int j = 0; j < numResults; j++) {
--- 127,131 ----
throws PipelineException {
// ensure synchronized access to finalResults Collection if threads are spawned
! final Collection syncResults = finalResults!=null?Collections.synchronizedCollection(finalResults):null;
final int numResults = psResults.length;
for (int j = 0; j < numResults; j++) {
***************
*** 156,160 ****
options.add(new ConfigOption(POOL_SIZE, IConfigOptionType.INTEGER,
! null, false, I18n.get("core.pipeline.processor.threadpool.desc")));
return options;
--- 163,170 ----
options.add(new ConfigOption(POOL_SIZE, IConfigOptionType.INTEGER,
! null, false, I18n.get("core.pipeline.processor.threadpool.option.poolSize")));
! options.add(new ConfigOption(KEEP_ALIVE, IConfigOptionType.INTEGER,
! Integer.toString(DEFAULT_KEEPALIVE), false,
! I18n.get("core.pipeline.processor.threadpool.option.keepAlive")));
return options;
***************
*** 179,182 ****
--- 189,214 ----
}
};
+ }
+
+ /**
+ * Callback method from the loader to signal that the babeldoc
+ * wants to shutdown. Please finalize, etc.
+ */
+ public void shutdown() {
+ new Thread(new Runnable() {
+ public void run() {
+ while(true) {
+ try {
+ Thread.sleep(1000);
+ if(threadPool.getPoolSize()==0) {
+ threadPool.shutdownNow();
+ return;
+ }
+ } catch (InterruptedException e) {
+ e.printStackTrace(); //To change body of catch statement use Options | File Templates.
+ }
+ }
+ }
+ }).start();
}
}
|