KickJava   Java API By Example, From Geeks To Geeks.

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


1 /*
2 * Licensed to the Apache Software Foundation (ASF) under one or more
3 * contributor license agreements. The ASF licenses this file to You
4 * under the Apache License, Version 2.0 (the "License"); you may not
5 * use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License. For additional information regarding
15 * copyright in this work, please see the NOTICE file in the top level
16 * directory of this distribution.
17 */

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 JavaDoc;
26 import java.util.Timer JavaDoc;
27 import java.util.TimerTask JavaDoc;
28
29 import org.apache.roller.model.ThreadManager;
30 import org.apache.roller.util.DateUtil;
31
32 /**
33  * Manage Roller's background thread use. Currently, Roller starts background
34  * threads for two purposes: 1) the nightly purge of referer counts and 2)
35  * following linkbacks (only occurs if linkbacks are enabled).
36  *
37  * @author aim4min
38  */

39 public class ThreadManagerImpl implements ThreadManager
40 {
41     private PooledExecutor backgroundExecutor;
42     private DirectExecutor nodelayExecutor;
43     private Timer JavaDoc 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 JavaDoc newThread(Runnable JavaDoc command)
55             {
56                 Thread JavaDoc t = new Thread JavaDoc(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 JavaDoc(true);
67     }
68
69     public void executeInBackground(Runnable JavaDoc runnable)
70             throws InterruptedException JavaDoc
71     {
72         backgroundExecutor.execute(runnable);
73     }
74
75     public void executeInForeground(Runnable JavaDoc runnable)
76             throws InterruptedException JavaDoc
77     {
78         nodelayExecutor.execute(runnable);
79     }
80
81     public void scheduleDailyTimerTask(TimerTask JavaDoc task)
82     {
83         scheduler.scheduleAtFixedRate(task,
84                 DateUtil.getEndOfDay(new Date JavaDoc()), DateUtil.millisInDay);
85     }
86
87     public void scheduleHourlyTimerTask(TimerTask JavaDoc task)
88     {
89         scheduler.scheduleAtFixedRate(task, new Date JavaDoc(), 60*60*1000);
90     }
91
92     public void scheduleFixedRateTimerTask(TimerTask JavaDoc task, long delayMins, long periodMins) {
93         if (periodMins < MIN_RATE_INTERVAL_MINS) {
94             throw new IllegalArgumentException JavaDoc("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