KickJava   Java API By Example, From Geeks To Geeks.

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


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.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, DisposableBean {
46
47     private boolean waitForJobsToCompleteOnShutdown = false;
48
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(boolean waitForJobsToCompleteOnShutdown) {
56         this.waitForJobsToCompleteOnShutdown = waitForJobsToCompleteOnShutdown;
57     }
58
59     public void afterPropertiesSet() throws SchedulerConfigException {
60         initialize();
61     }
62
63
64     public void execute(Runnable JavaDoc task) {
65         Assert.notNull(task, "Runnable must not be null");
66         if (!runInThread(task)) {
67             throw new SchedulingException("Quartz SimpleThreadPool already shut down");
68         }
69     }
70
71     /**
72      * This task executor prefers short-lived work units.
73      */

74     public boolean prefersShortLivedTasks() {
75         return true;
76     }
77
78
79     public void destroy() {
80         shutdown(this.waitForJobsToCompleteOnShutdown);
81     }
82
83 }
84
Popular Tags