KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > roller > business > ThreadManagerImpl


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 JavaDoc;
9 import java.util.Timer JavaDoc;
10 import java.util.TimerTask JavaDoc;
11
12 import org.roller.model.ThreadManager;
13 import org.roller.util.DateUtil;
14
15 /**
16  * Manage Roller's background thread use. Currently, Roller starts background
17  * threads for two purposes: 1) the nightly purge of referer counts and 2)
18  * following linkbacks (only occurs if linkbacks are enabled).
19  *
20  * @author aim4min
21  */

22 public class ThreadManagerImpl implements ThreadManager
23 {
24     private PooledExecutor backgroundExecutor;
25     private DirectExecutor nodelayExecutor;
26     private Timer JavaDoc 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 JavaDoc newThread(Runnable JavaDoc command)
38             {
39                 Thread JavaDoc t = new Thread JavaDoc(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 JavaDoc(true);
50     }
51
52     public void executeInBackground(Runnable JavaDoc runnable)
53             throws InterruptedException JavaDoc
54     {
55         backgroundExecutor.execute(runnable);
56     }
57
58     public void executeInForeground(Runnable JavaDoc runnable)
59             throws InterruptedException JavaDoc
60     {
61         nodelayExecutor.execute(runnable);
62     }
63
64     public void scheduleDailyTimerTask(TimerTask JavaDoc task)
65     {
66         scheduler.scheduleAtFixedRate(task,
67                 DateUtil.getEndOfDay(new Date JavaDoc()), DateUtil.millisInDay);
68     }
69
70     public void scheduleHourlyTimerTask(TimerTask JavaDoc task)
71     {
72         scheduler.scheduleAtFixedRate(task, new Date JavaDoc(), 60*60*1000);
73     }
74
75     public void scheduleFixedRateTimerTask(TimerTask JavaDoc task, long delayMins, long periodMins) {
76         if (periodMins < MIN_RATE_INTERVAL_MINS) {
77             throw new IllegalArgumentException JavaDoc("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