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.quartz.SchedulerConfigException;
20: import org.quartz.simpl.SimpleThreadPool;
21:
22: import org.springframework.beans.factory.DisposableBean;
23: import org.springframework.beans.factory.InitializingBean;
24: import org.springframework.scheduling.SchedulingException;
25: import org.springframework.scheduling.SchedulingTaskExecutor;
26: import org.springframework.util.Assert;
27:
28: /**
29: * Subclass of Quartz's SimpleThreadPool that implements Spring's
30: * TaskExecutor interface and listens to Spring lifecycle callbacks.
31: *
32: * <p>Can be used as a thread-pooling TaskExecutor backend, in particular
33: * on JDK <= 1.5 (where the JDK ThreadPoolExecutor isn't available yet).
34: * Can be shared between a Quartz Scheduler (specified as "taskExecutor")
35: * and other TaskExecutor users, or even used completely independent of
36: * a Quartz Scheduler (as plain TaskExecutor backend).
37: *
38: * @author Juergen Hoeller
39: * @since 2.0
40: * @see org.quartz.simpl.SimpleThreadPool
41: * @see org.springframework.core.task.TaskExecutor
42: * @see SchedulerFactoryBean#setTaskExecutor
43: */
44: public class SimpleThreadPoolTaskExecutor extends SimpleThreadPool
45: implements SchedulingTaskExecutor, InitializingBean,
46: DisposableBean {
47:
48: private boolean waitForJobsToCompleteOnShutdown = false;
49:
50: /**
51: * Set whether to wait for running jobs to complete on shutdown.
52: * Default is "false".
53: * @see org.quartz.simpl.SimpleThreadPool#shutdown(boolean)
54: */
55: public void setWaitForJobsToCompleteOnShutdown(
56: boolean waitForJobsToCompleteOnShutdown) {
57: this .waitForJobsToCompleteOnShutdown = waitForJobsToCompleteOnShutdown;
58: }
59:
60: public void afterPropertiesSet() throws SchedulerConfigException {
61: initialize();
62: }
63:
64: public void execute(Runnable task) {
65: Assert.notNull(task, "Runnable must not be null");
66: if (!runInThread(task)) {
67: throw new SchedulingException(
68: "Quartz SimpleThreadPool already shut down");
69: }
70: }
71:
72: /**
73: * This task executor prefers short-lived work units.
74: */
75: public boolean prefersShortLivedTasks() {
76: return true;
77: }
78:
79: public void destroy() {
80: shutdown(this.waitForJobsToCompleteOnShutdown);
81: }
82:
83: }
|