01: /*
02: * Copyright 2002-2006 the original author or authors.
03: *
04: * Licensed under the Apache License, Version 2.0 (the "License");
05: * you may not use this file except in compliance with the License.
06: * You may obtain a copy of the License at
07: *
08: * http://www.apache.org/licenses/LICENSE-2.0
09: *
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: public void initialize() throws SchedulerConfigException {
43: // Absolutely needs thread-bound TaskExecutor to initialize.
44: this .taskExecutor = SchedulerFactoryBean
45: .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: public boolean runInThread(Runnable runnable) {
61: if (runnable == null) {
62: return false;
63: }
64: try {
65: this .taskExecutor.execute(runnable);
66: return true;
67: } catch (TaskRejectedException ex) {
68: logger.error("Task has been rejected by TaskExecutor", ex);
69: return false;
70: }
71: }
72:
73: public int blockForAvailableThreads() {
74: // The present implementation always returns 1, making Quartz (1.6)
75: // always schedule any tasks that it feels like scheduling.
76: // This could be made smarter for specific TaskExecutors,
77: // for example calling <code>getMaximumPoolSize() - getActiveCount()</code>
78: // on a <code>java.util.concurrent.ThreadPoolExecutor</code>.
79: return 1;
80: }
81:
82: }
|