Quartz的任务的临时启动和暂停和恢复
您的评价: |
|
Quartz的任务的临时启动和暂停和恢复
在项目中需要手动启停某些服务,那么需要有一个控制这些任务的类。由于任务是有Quartz控制的,我们只需要通过Quartz的相关的API实现相关的功能即可。
001 | package com.gbsoft.rpt.qtz; |
002 |
003 | import java.util.Date; |
004 | import java.util.List; |
005 | import java.util.Map; |
006 |
007 | import org.quartz.JobDataMap; |
008 | import org.quartz.JobDetail; |
009 | import org.quartz.JobKey; |
010 | import org.quartz.Scheduler; |
011 | import org.quartz.SchedulerException; |
012 | import org.quartz.SchedulerFactory; |
013 | import org.quartz.Trigger; |
014 | import org.quartz.TriggerKey; |
015 | import org.quartz.impl.StdSchedulerFactory; |
016 | import org.quartz.impl.matchers.GroupMatcher; |
017 |
018 | /** |
019 | * 一个简单的quartz任务管理器 |
020 | * @author 123 |
021 | * |
022 | */ |
023 | public class QuartzSchedule { |
024 | private static Scheduler scheduler=getScheduler(); |
025 | /** |
026 | * 创建一个调度对象 |
027 | * @return |
028 | * @throws SchedulerException |
029 | */ |
030 | private static Scheduler getScheduler() { |
031 | SchedulerFactory sf = new StdSchedulerFactory(); |
032 | Scheduler scheduler= null ; |
033 | try { |
034 | scheduler = sf.getScheduler(); |
035 | } catch (SchedulerException e) { |
036 | e.printStackTrace(); |
037 | } |
038 | return scheduler; |
039 | } |
040 | public static Scheduler getInstanceScheduler(){ |
041 | return scheduler; |
042 | } |
043 |
044 | /** |
045 | * 启动一个调度对象 |
046 | * @throws SchedulerException |
047 | */ |
048 | public void start() throws SchedulerException |
049 | { |
050 | scheduler.start(); |
051 | } |
052 | |
053 | /** |
054 | * 检查调度是否启动 |
055 | * @return |
056 | * @throws SchedulerException |
057 | */ |
058 | public boolean isStarted() throws SchedulerException |
059 | { |
060 | return scheduler.isStarted(); |
061 | } |
062 |
063 | /** |
064 | * 关闭调度信息 |
065 | * @throws SchedulerException |
066 | */ |
067 | public void shutdown() throws SchedulerException { |
068 | scheduler.shutdown(); |
069 | } |
070 | /** |
071 | * 添加调度的job信息 |
072 | * @param jobdetail |
073 | * @param trigger |
074 | * @return |
075 | * @throws SchedulerException |
076 | */ |
077 | public Date scheduleJob(JobDetail jobdetail, Trigger trigger) |
078 | throws SchedulerException{ |
079 | return scheduler.scheduleJob(jobdetail, trigger); |
080 | } |
081 | /** |
082 | * 添加相关的触发器 |
083 | * @param trigger |
084 | * @return |
085 | * @throws SchedulerException |
086 | */ |
087 | public Date scheduleJob(Trigger trigger) throws SchedulerException{ |
088 | return scheduler.scheduleJob(trigger); |
089 | } |
090 | /** |
091 | * 添加多个job任务 |
092 | * @param triggersAndJobs |
093 | * @param replace |
094 | * @throws SchedulerException |
095 | */ |
096 | public void scheduleJobs(Map<JobDetail, List<Trigger>> triggersAndJobs, boolean replace) throws SchedulerException |
097 | { |
098 | scheduler.scheduleJobs(triggersAndJobs, replace); |
099 | } |
100 | /** |
101 | * 停止调度Job任务 |
102 | * @param triggerkey |
103 | * @return |
104 | * @throws SchedulerException |
105 | */ |
106 | public boolean unscheduleJob(TriggerKey triggerkey) |
107 | throws SchedulerException{ |
108 | return scheduler.unscheduleJob(triggerkey); |
109 | } |
110 |
111 | /** |
112 | * 停止调度多个触发器相关的job |
113 | * @param list |
114 | * @return |
115 | * @throws SchedulerException |
116 | */ |
117 | public boolean unscheduleJobs(List<TriggerKey> triggerKeylist) throws SchedulerException{ |
118 | return scheduler.unscheduleJobs(triggerKeylist); |
119 | } |
120 | /** |
121 | * 重新恢复触发器相关的job任务 |
122 | * @param triggerkey |
123 | * @param trigger |
124 | * @return |
125 | * @throws SchedulerException |
126 | */ |
127 | public Date rescheduleJob(TriggerKey triggerkey, Trigger trigger) |
128 | throws SchedulerException{ |
129 | return scheduler.rescheduleJob(triggerkey, trigger); |
130 | } |
131 | /** |
132 | * 添加相关的job任务 |
133 | * @param jobdetail |
134 | * @param flag |
135 | * @throws SchedulerException |
136 | */ |
137 | public void addJob(JobDetail jobdetail, boolean flag) |
138 | throws SchedulerException { |
139 | scheduler.addJob(jobdetail, flag); |
140 | } |
141 |
142 | /** |
143 | * 删除相关的job任务 |
144 | * @param jobkey |
145 | * @return |
146 | * @throws SchedulerException |
147 | */ |
148 | public boolean deleteJob(JobKey jobkey) throws SchedulerException{ |
149 | return scheduler.deleteJob(jobkey); |
150 | } |
151 |
152 | /** |
153 | * 删除相关的多个job任务 |
154 | * @param jobKeys |
155 | * @return |
156 | * @throws SchedulerException |
157 | */ |
158 | public boolean deleteJobs(List<JobKey> jobKeys) |
159 | throws SchedulerException{ |
160 | return scheduler.deleteJobs(jobKeys); |
161 | } |
162 | /** |
163 | * |
164 | * @param jobkey |
165 | * @throws SchedulerException |
166 | */ |
167 | public void triggerJob(JobKey jobkey) throws SchedulerException { |
168 | scheduler.triggerJob(jobkey); |
169 | } |
170 | /** |
171 | * |
172 | * @param jobkey |
173 | * @param jobdatamap |
174 | * @throws SchedulerException |
175 | */ |
176 | public void triggerJob(JobKey jobkey, JobDataMap jobdatamap) |
177 | throws SchedulerException { |
178 | scheduler.triggerJob(jobkey, jobdatamap); |
179 | } |
180 | /** |
181 | * 停止一个job任务 |
182 | * @param jobkey |
183 | * @throws SchedulerException |
184 | */ |
185 | public void pauseJob(JobKey jobkey) throws SchedulerException { |
186 | scheduler.pauseJob(jobkey); |
187 | } |
188 | /** |
189 | * 停止多个job任务 |
190 | * @param groupmatcher |
191 | * @throws SchedulerException |
192 | */ |
193 | public void pauseJobs(GroupMatcher<JobKey> groupmatcher) |
194 | throws SchedulerException { |
195 | scheduler.pauseJobs(groupmatcher); |
196 | } |
197 | /** |
198 | * 停止使用相关的触发器 |
199 | * @param triggerkey |
200 | * @throws SchedulerException |
201 | */ |
202 | public void pauseTrigger(TriggerKey triggerkey) |
203 | throws SchedulerException { |
204 | scheduler.pauseTrigger(triggerkey); |
205 | } |
206 |
207 | public void pauseTriggers(GroupMatcher<TriggerKey> groupmatcher) |
208 | throws SchedulerException { |
209 | scheduler.pauseTriggers(groupmatcher); |
210 | } |
211 | /** |
212 | * 恢复相关的job任务 |
213 | * @param jobkey |
214 | * @throws SchedulerException |
215 | */ |
216 | public void resumeJob(JobKey jobkey) throws SchedulerException { |
217 | scheduler.pauseJob(jobkey); |
218 | } |
219 | |
220 | public void resumeJobs(GroupMatcher<JobKey> matcher) |
221 | throws SchedulerException { |
222 | scheduler.resumeJobs(matcher); |
223 | } |
224 |
225 | public void resumeTrigger(TriggerKey triggerkey) |
226 | throws SchedulerException { |
227 | scheduler.resumeTrigger(triggerkey); |
228 | } |
229 | |
230 | public void resumeTriggers(GroupMatcher<TriggerKey> groupmatcher) |
231 | throws SchedulerException |
232 | { |
233 | scheduler.resumeTriggers(groupmatcher); |
234 | } |
235 | /** |
236 | * 暂停调度中所有的job任务 |
237 | * @throws SchedulerException |
238 | */ |
239 | public void pauseAll() throws SchedulerException |
240 | { |
241 | scheduler.pauseAll(); |
242 | } |
243 | /** |
244 | * 恢复调度中所有的job的任务 |
245 | * @throws SchedulerException |
246 | */ |
247 | public void resumeAll() throws SchedulerException |
248 | { |
249 | scheduler.resumeAll(); |
250 | } |
251 | } |
01 | package com.gbsoft.rpt.qrz; |
02 |
03 | import java.util.Date; |
04 |
05 | import org.slf4j.Logger; |
06 | import org.slf4j.LoggerFactory; |
07 | import org.quartz.Job; |
08 | import org.quartz.JobExecutionContext; |
09 | import org.quartz.JobExecutionException; |
10 |
11 | /** |
12 | * 一个简单的quartz调用job |
13 | * @author 123 |
14 | * |
15 | */ |
16 | public class HelloJob implements Job { |
17 |
18 | private static Logger _log = LoggerFactory.getLogger(HelloJob. class ); |
19 |
20 | public HelloJob() { |
21 | } |
22 |
23 | public void execute(JobExecutionContext context) |
24 | throws JobExecutionException { |
25 | _log.info( "Hello World! - " + new Date()); |
26 | } |
27 |
28 | } |
创建触发器和调用相关的Job
1 | package com.gbsoft.rpt.qrz; |
2 |
3 | import static org.quartz.DateBuilder.evenMinuteDate; |
4 | import static org.quartz.JobBuilder.newJob; |
5 | import static org.quartz.TriggerBuilder.newTrigger; |
6 |
7 | import java.util.Date; |
001 | import org.quartz.JobDetail; |
002 | import org.quartz.Scheduler; |
003 | import org.quartz.Trigger; |
004 | import org.slf4j.Logger; |
005 | import org.slf4j.LoggerFactory; |
006 |
007 | /** |
008 | * 一个简单的测试quartz任务管理器测试类 |
009 | * @author 123 |
010 | * |
011 | */ |
012 | public class QuartzScheduleMain { |
013 |
014 | |
015 | /** |
016 | * |
017 | * @throws Exception |
018 | */ |
019 | public void run() throws Exception { |
020 | Logger log = LoggerFactory.getLogger(QuartzScheduleMain. class ); |
021 |
022 | log.info( "------- Initializing ----------------------" ); |
023 |
024 | // First we must get a reference to a scheduler |
025 | //从调度管理器中获取调度对象 |
026 | Scheduler sched = QuartzScheduleMgr.getInstanceScheduler(); |
027 | log.info( "------- Initialization Complete -----------" ); |
028 |
029 | // computer a time that is on the next round minute |
030 | Date runTime = evenMinuteDate( new Date()); |
031 |
032 | log.info( "------- Scheduling Job -------------------" ); |
033 |
034 | // define the job and tie it to our HelloJob class |
035 | //创建相关的job信息 |
036 | JobDetail job = newJob(HelloJob. class ) |
037 | .withIdentity( "job1" , "group1" ) |
038 | .build(); |
039 | |
040 | // Trigger the job to run on the next round minute |
041 | //创建一个触发器的名称 |
042 | Trigger trigger = newTrigger() |
043 | .withIdentity( "trigger1" , "group1" ) |
044 | .startAt(runTime) |
045 | .build(); |
046 | |
047 | // Tell quartz to schedule the job using our trigger |
048 | //设置调度相关的Job |
049 | sched.scheduleJob(job, trigger); |
050 | log.info(job.getKey() + " will run at: " + runTime); |
051 |
052 | // Start up the scheduler (nothing can actually run until the |
053 | // scheduler has been started) |
054 | //启动调度任务 |
055 | sched.start(); |
056 |
057 | log.info( "------- Started Scheduler -----------------" ); |
058 |
059 | try { |
060 | Thread.sleep(25L * 1000L); |
061 | // executing... |
062 | } catch (Exception e) { |
063 | } |
064 | //暂时停止Job任务开始执行 |
065 | log.info( "-------pauseJob.. -------------" ); |
066 | sched.pauseJob(job.getKey()); |
067 | |
068 | try { |
069 | Thread.sleep(10L * 1000L); |
070 | } catch (Exception e) { |
071 | } |
072 | log.info( "------- resumeJob... -------------" ); |
073 | //恢复Job任务开始执行 |
074 | sched.resumeJob(job.getKey()); |
075 | try { |
076 | Thread.sleep(10L * 1000L); |
077 | // executing... |
078 | } catch (Exception e) { |
079 | } |
080 | |
081 | |
082 | // wait long enough so that the scheduler as an opportunity to |
083 | // run the job! |
084 | log.info( "------- Waiting 65 seconds... -------------" ); |
085 | try { |
086 | // wait 65 seconds to show job |
087 | Thread.sleep(65L * 1000L); |
088 | // executing... |
089 | } catch (Exception e) { |
090 | } |
091 |
092 | // shut down the scheduler |
093 | log.info( "------- Shutting Down ---------------------" ); |
094 | sched.shutdown( true ); |
095 | log.info( "------- Shutdown Complete -----------------" ); |
096 | } |
097 |
098 | public static void main(String[] args) throws Exception { |
099 |
100 | QuartzScheduleMain example = new QuartzScheduleMain(); |
101 | example.run(); |
102 |
103 | } |
104 |
105 | } |
转:http://www.open-open.com/lib/view/open1351324322285.html
官网地址: http://www.quartz-scheduler.org/documentation/quartz-2.1.x/quick-start
