1 18 package org.apache.roller.business; 19 20 import EDU.oswego.cs.dl.util.concurrent.BoundedBuffer; 21 import EDU.oswego.cs.dl.util.concurrent.DirectExecutor; 22 import EDU.oswego.cs.dl.util.concurrent.PooledExecutor; 23 import EDU.oswego.cs.dl.util.concurrent.ThreadFactory; 24 25 import java.util.Date ; 26 import java.util.Timer ; 27 import java.util.TimerTask ; 28 29 import org.apache.roller.model.ThreadManager; 30 import org.apache.roller.util.DateUtil; 31 32 39 public class ThreadManagerImpl implements ThreadManager 40 { 41 private PooledExecutor backgroundExecutor; 42 private DirectExecutor nodelayExecutor; 43 private Timer scheduler; 44 45 public ThreadManagerImpl() 46 { 47 backgroundExecutor = new PooledExecutor(new BoundedBuffer(10), 25); 48 backgroundExecutor.setMinimumPoolSize(4); 49 backgroundExecutor.setKeepAliveTime(1000 * 60 * 5); 50 backgroundExecutor.waitWhenBlocked(); 51 backgroundExecutor.createThreads(9); 52 53 backgroundExecutor.setThreadFactory(new ThreadFactory() { 54 public Thread newThread(Runnable command) 55 { 56 Thread t = new Thread (command); 57 t.setDaemon(false); 58 t.setName("Background Execution Threads"); 59 t.setPriority(Thread.NORM_PRIORITY); 60 61 return t; 62 } 63 }); 64 65 nodelayExecutor = new DirectExecutor(); 66 scheduler = new Timer (true); 67 } 68 69 public void executeInBackground(Runnable runnable) 70 throws InterruptedException 71 { 72 backgroundExecutor.execute(runnable); 73 } 74 75 public void executeInForeground(Runnable runnable) 76 throws InterruptedException 77 { 78 nodelayExecutor.execute(runnable); 79 } 80 81 public void scheduleDailyTimerTask(TimerTask task) 82 { 83 scheduler.scheduleAtFixedRate(task, 84 DateUtil.getEndOfDay(new Date ()), DateUtil.millisInDay); 85 } 86 87 public void scheduleHourlyTimerTask(TimerTask task) 88 { 89 scheduler.scheduleAtFixedRate(task, new Date (), 60*60*1000); 90 } 91 92 public void scheduleFixedRateTimerTask(TimerTask task, long delayMins, long periodMins) { 93 if (periodMins < MIN_RATE_INTERVAL_MINS) { 94 throw new IllegalArgumentException ("Period (" + periodMins + 95 ") shorter than minimum allowed (" + MIN_RATE_INTERVAL_MINS + ")"); 96 } 97 scheduler.scheduleAtFixedRate(task, delayMins * 60 * 1000, periodMins * 60 * 1000); 98 } 99 100 public void shutdown() 101 { 102 backgroundExecutor.shutdownAfterProcessingCurrentlyQueuedTasks(); 103 scheduler.cancel(); 104 } 105 106 public void release() 107 { 108 } 109 } | Popular Tags |