KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > springframework > scheduling > quartz > LocalTaskExecutorThreadPool


1 /*
2  * Copyright 2002-2006 the original author or authors.
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not 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.
15  */

16
17 package org.springframework.scheduling.quartz;
18
19 import org.apache.commons.logging.Log;
20 import org.apache.commons.logging.LogFactory;
21 import org.quartz.SchedulerConfigException;
22 import org.quartz.spi.ThreadPool;
23
24 import org.springframework.core.task.TaskExecutor;
25 import org.springframework.core.task.TaskRejectedException;
26
27 /**
28  * Quartz ThreadPool adapter that delegates to a Spring-managed
29  * TaskExecutor instance, specified on SchedulerFactoryBean.
30  *
31  * @author Juergen Hoeller
32  * @since 2.0
33  * @see SchedulerFactoryBean#setTaskExecutor
34  */

35 public class LocalTaskExecutorThreadPool implements ThreadPool {
36
37     /** Logger available to subclasses */
38     protected final Log logger = LogFactory.getLog(getClass());
39
40     private TaskExecutor taskExecutor;
41
42
43     public void initialize() throws SchedulerConfigException {
44         // Absolutely needs thread-bound TaskExecutor to initialize.
45
this.taskExecutor = SchedulerFactoryBean.getConfigTimeTaskExecutor();
46         if (this.taskExecutor == null) {
47             throw new SchedulerConfigException(
48                 "No local TaskExecutor found for configuration - " +
49                 "'taskExecutor' property must be set on SchedulerFactoryBean");
50         }
51     }
52
53     public void shutdown(boolean waitForJobsToComplete) {
54     }
55
56     public int getPoolSize() {
57         return -1;
58     }
59
60
61     public boolean runInThread(Runnable JavaDoc runnable) {
62         if (runnable == null) {
63             return false;
64         }
65         try {
66             this.taskExecutor.execute(runnable);
67             return true;
68         }
69         catch (TaskRejectedException ex) {
70             logger.error("Task has been rejected by TaskExecutor", ex);
71             return false;
72         }
73     }
74
75     public int blockForAvailableThreads() {
76         // The present implementation always returns 1, making Quartz (1.6)
77
// always schedule any tasks that it feels like scheduling.
78
// This could be made smarter for specific TaskExecutors,
79
// for example calling <code>getMaximumPoolSize() - getActiveCount()</code>
80
// on a <code>java.util.concurrent.ThreadPoolExecutor</code>.
81
return 1;
82     }
83
84 }
85
Popular Tags