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