001: /*
002: * Licensed to the Apache Software Foundation (ASF) under one or more
003: * contributor license agreements. The ASF licenses this file to You
004: * under the Apache License, Version 2.0 (the "License"); you may not
005: * use this file except in compliance with the License.
006: * You may obtain a copy of the License at
007: *
008: * http://www.apache.org/licenses/LICENSE-2.0
009: *
010: * Unless required by applicable law or agreed to in writing, software
011: * distributed under the License is distributed on an "AS IS" BASIS,
012: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
013: * See the License for the specific language governing permissions and
014: * limitations under the License. For additional information regarding
015: * copyright in this work, please see the NOTICE file in the top level
016: * directory of this distribution.
017: */
018:
019: package org.apache.roller.business.runnable;
020:
021: import EDU.oswego.cs.dl.util.concurrent.BoundedBuffer;
022: import EDU.oswego.cs.dl.util.concurrent.DirectExecutor;
023: import EDU.oswego.cs.dl.util.concurrent.PooledExecutor;
024: import EDU.oswego.cs.dl.util.concurrent.ThreadFactory;
025: import java.util.Date;
026: import java.util.Timer;
027: import org.apache.commons.logging.Log;
028: import org.apache.commons.logging.LogFactory;
029:
030: /**
031: * Manage Roller's thread use.
032: */
033: public class ThreadManagerImpl implements ThreadManager {
034:
035: private static Log log = LogFactory.getLog(ThreadManagerImpl.class);
036:
037: private PooledExecutor backgroundExecutor = null;
038: private DirectExecutor nodelayExecutor = null;
039: private Timer scheduler = null;
040:
041: public ThreadManagerImpl() {
042:
043: log.info("Intializing Thread Manager");
044:
045: backgroundExecutor = new PooledExecutor(new BoundedBuffer(10),
046: 25);
047: backgroundExecutor.setMinimumPoolSize(4);
048: backgroundExecutor.setKeepAliveTime(1000 * 60 * 5);
049: backgroundExecutor.waitWhenBlocked();
050: backgroundExecutor.createThreads(9);
051:
052: backgroundExecutor.setThreadFactory(new ThreadFactory() {
053: public Thread newThread(Runnable command) {
054: Thread t = new Thread(command);
055: t.setDaemon(false);
056: t.setName("Background Execution Threads");
057: t.setPriority(Thread.NORM_PRIORITY);
058:
059: return t;
060: }
061: });
062:
063: nodelayExecutor = new DirectExecutor();
064: scheduler = new Timer(true);
065: }
066:
067: public void executeInBackground(Runnable runnable)
068: throws InterruptedException {
069: backgroundExecutor.execute(runnable);
070: }
071:
072: public void executeInForeground(Runnable runnable)
073: throws InterruptedException {
074: nodelayExecutor.execute(runnable);
075: }
076:
077: public void scheduleFixedRateTimerTask(RollerTask task,
078: Date startTime, long intervalMins) {
079:
080: if (intervalMins < MIN_RATE_INTERVAL_MINS) {
081: throw new IllegalArgumentException("Interval ("
082: + intervalMins + ") shorter than minimum allowed ("
083: + MIN_RATE_INTERVAL_MINS + ")");
084: }
085:
086: scheduler.scheduleAtFixedRate(task, startTime,
087: intervalMins * 60 * 1000);
088: }
089:
090: public void shutdown() {
091:
092: log.debug("starting shutdown sequence");
093:
094: // trigger an immediate shutdown of any backgrounded tasks
095: backgroundExecutor.shutdownNow();
096:
097: // TODO: it appears that this doesn't affect tasks which may be running
098: // when this is called and that may not be what we want. It would be
099: // nice if shutdown() meant shutdown immediately.
100: scheduler.cancel();
101: }
102:
103: public void release() {
104: }
105:
106: public boolean acquireLock(RollerTask task) {
107: return true;
108: }
109:
110: public boolean releaseLock(RollerTask task) {
111: return true;
112: }
113:
114: public boolean isLocked(RollerTask task) {
115: return false;
116: }
117:
118: }
|