Quartz的任务的临时启动和暂停和恢复
在项目中需要手动启停某些服务,那么需要有一个控制这些任务的类。由于任务是有Quartz控制的,我们只需要通过Quartz的相关的API实现相关的功能即可。
- package com.easyway.app.quartz.mgr;
- import java.util.Date;
- import java.util.List;
- import java.util.Map;
- import org.quartz.JobDataMap;
- import org.quartz.JobDetail;
- import org.quartz.JobKey;
- import org.quartz.Scheduler;
- import org.quartz.SchedulerException;
- import org.quartz.SchedulerFactory;
- import org.quartz.Trigger;
- import org.quartz.TriggerKey;
- import org.quartz.impl.StdSchedulerFactory;
- import org.quartz.impl.matchers.GroupMatcher;
- /**
- * 一个简单的quartz任务管理器
- * @author longgangbai
- *
- */
- public class QuartzScheduleMgr {
- private static Scheduler scheduler=getScheduler();
- /**
- * 创建一个调度对象
- * @return
- * @throws SchedulerException
- */
- private static Scheduler getScheduler() {
- SchedulerFactory sf = new StdSchedulerFactory();
- Scheduler scheduler=null;
- try {
- scheduler = sf.getScheduler();
- } catch (SchedulerException e) {
- e.printStackTrace();
- }
- return scheduler;
- }
- public static Scheduler getInstanceScheduler(){
- return scheduler;
- }
- /**
- * 启动一个调度对象
- * @throws SchedulerException
- */
- public void start() throws SchedulerException
- {
- scheduler.start();
- }
- /**
- * 检查调度是否启动
- * @return
- * @throws SchedulerException
- */
- public boolean isStarted() throws SchedulerException
- {
- return scheduler.isStarted();
- }
- /**
- * 关闭调度信息
- * @throws SchedulerException
- */
- public void shutdown() throws SchedulerException {
- scheduler.shutdown();
- }
- /**
- * 添加调度的job信息
- * @param jobdetail
- * @param trigger
- * @return
- * @throws SchedulerException
- */
- public Date scheduleJob(JobDetail jobdetail, Trigger trigger)
- throws SchedulerException{
- return scheduler.scheduleJob(jobdetail, trigger);
- }
- /**
- * 添加相关的触发器
- * @param trigger
- * @return
- * @throws SchedulerException
- */
- public Date scheduleJob(Trigger trigger) throws SchedulerException{
- return scheduler.scheduleJob(trigger);
- }
- /**
- * 添加多个job任务
- * @param triggersAndJobs
- * @param replace
- * @throws SchedulerException
- */
- public void scheduleJobs(Map<JobDetail, List<Trigger>> triggersAndJobs, boolean replace) throws SchedulerException
- {
- scheduler.scheduleJobs(triggersAndJobs, replace);
- }
- /**
- * 停止调度Job任务
- * @param triggerkey
- * @return
- * @throws SchedulerException
- */
- public boolean unscheduleJob(TriggerKey triggerkey)
- throws SchedulerException{
- return scheduler.unscheduleJob(triggerkey);
- }
- /**
- * 停止调度多个触发器相关的job
- * @param list
- * @return
- * @throws SchedulerException
- */
- public boolean unscheduleJobs(List<TriggerKey> triggerKeylist) throws SchedulerException{
- return scheduler.unscheduleJobs(triggerKeylist);
- }
- /**
- * 重新恢复触发器相关的job任务
- * @param triggerkey
- * @param trigger
- * @return
- * @throws SchedulerException
- */
- public Date rescheduleJob(TriggerKey triggerkey, Trigger trigger)
- throws SchedulerException{
- return scheduler.rescheduleJob(triggerkey, trigger);
- }
- /**
- * 添加相关的job任务
- * @param jobdetail
- * @param flag
- * @throws SchedulerException
- */
- public void addJob(JobDetail jobdetail, boolean flag)
- throws SchedulerException {
- scheduler.addJob(jobdetail, flag);
- }
- /**
- * 删除相关的job任务
- * @param jobkey
- * @return
- * @throws SchedulerException
- */
- public boolean deleteJob(JobKey jobkey) throws SchedulerException{
- return scheduler.deleteJob(jobkey);
- }
- /**
- * 删除相关的多个job任务
- * @param jobKeys
- * @return
- * @throws SchedulerException
- */
- public boolean deleteJobs(List<JobKey> jobKeys)
- throws SchedulerException{
- return scheduler.deleteJobs(jobKeys);
- }
- /**
- *
- * @param jobkey
- * @throws SchedulerException
- */
- public void triggerJob(JobKey jobkey) throws SchedulerException {
- scheduler.triggerJob(jobkey);
- }
- /**
- *
- * @param jobkey
- * @param jobdatamap
- * @throws SchedulerException
- */
- public void triggerJob(JobKey jobkey, JobDataMap jobdatamap)
- throws SchedulerException {
- scheduler.triggerJob(jobkey, jobdatamap);
- }
- /**
- * 停止一个job任务
- * @param jobkey
- * @throws SchedulerException
- */
- public void pauseJob(JobKey jobkey) throws SchedulerException {
- scheduler.pauseJob(jobkey);
- }
- /**
- * 停止多个job任务
- * @param groupmatcher
- * @throws SchedulerException
- */
- public void pauseJobs(GroupMatcher<JobKey> groupmatcher)
- throws SchedulerException {
- scheduler.pauseJobs(groupmatcher);
- }
- /**
- * 停止使用相关的触发器
- * @param triggerkey
- * @throws SchedulerException
- */
- public void pauseTrigger(TriggerKey triggerkey)
- throws SchedulerException {
- scheduler.pauseTrigger(triggerkey);
- }
- public void pauseTriggers(GroupMatcher<TriggerKey> groupmatcher)
- throws SchedulerException {
- scheduler.pauseTriggers(groupmatcher);
- }
- /**
- * 恢复相关的job任务
- * @param jobkey
- * @throws SchedulerException
- */
- public void resumeJob(JobKey jobkey) throws SchedulerException {
- scheduler.pauseJob(jobkey);
- }
- public void resumeJobs(GroupMatcher<JobKey> matcher)
- throws SchedulerException {
- scheduler.resumeJobs(matcher);
- }
- public void resumeTrigger(TriggerKey triggerkey)
- throws SchedulerException {
- scheduler.resumeTrigger(triggerkey);
- }
- public void resumeTriggers(GroupMatcher<TriggerKey> groupmatcher)
- throws SchedulerException
- {
- scheduler.resumeTriggers(groupmatcher);
- }
- /**
- * 暂停调度中所有的job任务
- * @throws SchedulerException
- */
- public void pauseAll() throws SchedulerException
- {
- scheduler.pauseAll();
- }
- /**
- * 恢复调度中所有的job的任务
- * @throws SchedulerException
- */
- public void resumeAll() throws SchedulerException
- {
- scheduler.resumeAll();
- }
- }
创建一个Job任务:
- /*
- * Copyright 2005 - 2009 Terracotta, Inc.
- *
- * Licensed under the Apache License, Version 2.0 (the "License"); you may not
- * use this file except in compliance with the License. You may obtain a copy
- * of the License at
- *
- * http://www.apache.org/licenses/LICENSE-2.0
- *
- * Unless required by applicable law or agreed to in writing, software
- * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
- * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
- * License for the specific language governing permissions and limitations
- * under the License.
- *
- */
- package com.easyway.app.quartz.mgr;
- import java.util.Date;
- import org.slf4j.Logger;
- import org.slf4j.LoggerFactory;
- import org.quartz.Job;
- import org.quartz.JobExecutionContext;
- import org.quartz.JobExecutionException;
- /**
- * 一个简单的quartz调用job
- * @author longgangbai
- *
- */
- public class HelloJob implements Job {
- private static Logger _log = LoggerFactory.getLogger(HelloJob.class);
- public HelloJob() {
- }
- public void execute(JobExecutionContext context)
- throws JobExecutionException {
- _log.info("Hello World! - " + new Date());
- }
- }
创建触发器和调用相关的Job
- /*
- * Copyright 2005 - 2009 Terracotta, Inc.
- *
- * Licensed under the Apache License, Version 2.0 (the "License"); you may not
- * use this file except in compliance with the License. You may obtain a copy
- * of the License at
- *
- * http://www.apache.org/licenses/LICENSE-2.0
- *
- * Unless required by applicable law or agreed to in writing, software
- * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
- * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
- * License for the specific language governing permissions and limitations
- * under the License.
- *
- */
- package com.easyway.app.quartz.mgr;
- import static org.quartz.DateBuilder.evenMinuteDate;
- import static org.quartz.JobBuilder.newJob;
- import static org.quartz.TriggerBuilder.newTrigger;
- import java.util.Date;
- import org.quartz.JobDetail;
- import org.quartz.Scheduler;
- import org.quartz.Trigger;
- import org.slf4j.Logger;
- import org.slf4j.LoggerFactory;
- /**
- * 一个简单的测试quartz任务管理器测试类
- * @author longgangbai
- *
- */
- public class QuartzScheduleMain {
- /**
- *
- * @throws Exception
- */
- public void run() throws Exception {
- Logger log = LoggerFactory.getLogger(QuartzScheduleMain.class);
- log.info("------- Initializing ----------------------");
- // First we must get a reference to a scheduler
- //从调度管理器中获取调度对象
- Scheduler sched = QuartzScheduleMgr.getInstanceScheduler();
- log.info("------- Initialization Complete -----------");
- // computer a time that is on the next round minute
- Date runTime = evenMinuteDate(new Date());
- log.info("------- Scheduling Job -------------------");
- // define the job and tie it to our HelloJob class
- //创建相关的job信息
- JobDetail job = newJob(HelloJob.class)
- .withIdentity("job1", "group1")
- .build();
- // Trigger the job to run on the next round minute
- //创建一个触发器的名称
- Trigger trigger = newTrigger()
- .withIdentity("trigger1", "group1")
- .startAt(runTime)
- .build();
- // Tell quartz to schedule the job using our trigger
- //设置调度相关的Job
- sched.scheduleJob(job, trigger);
- log.info(job.getKey() + " will run at: " + runTime);
- // Start up the scheduler (nothing can actually run until the
- // scheduler has been started)
- //启动调度任务
- sched.start();
- log.info("------- Started Scheduler -----------------");
- try {
- Thread.sleep(25L * 1000L);
- // executing...
- } catch (Exception e) {
- }
- //暂时停止Job任务开始执行
- log.info("-------pauseJob.. -------------");
- sched.pauseJob(job.getKey());
- try {
- Thread.sleep(10L * 1000L);
- } catch (Exception e) {
- }
- log.info("------- resumeJob... -------------");
- //恢复Job任务开始执行
- sched.resumeJob(job.getKey());
- try {
- Thread.sleep(10L * 1000L);
- // executing...
- } catch (Exception e) {
- }
- // wait long enough so that the scheduler as an opportunity to
- // run the job!
- log.info("------- Waiting 65 seconds... -------------");
- try {
- // wait 65 seconds to show job
- Thread.sleep(65L * 1000L);
- // executing...
- } catch (Exception e) {
- }
- // shut down the scheduler
- log.info("------- Shutting Down ---------------------");
- sched.shutdown(true);
- log.info("------- Shutdown Complete -----------------");
- }
- public static void main(String[] args) throws Exception {
- QuartzScheduleMain example = new QuartzScheduleMain();
- example.run();
- }
- }
Quartz的任务的临时启动和暂停和恢复的更多相关文章
- Spring 3整合Quartz 2实现定时任务三:动态暂停 恢复 修改和删除任务
前面我们已经完成了spring 3和quartz 2的整合以及动态添加定时任务,我们接着来完善它,使之能支持更多的操作,例如暂停.恢复.修改等. 在动态添加定时任务中其实已经涉及到了其中的一些代码,这 ...
- 【Android Developers Training】 16. 暂停和恢复一个Activity
注:本文翻译自Google官方的Android Developers Training文档,译者技术一般,由于喜爱安卓而产生了翻译的念头,纯属个人兴趣爱好. 原文链接:http://developer ...
- 使用 suspend 和 resume 暂停和恢复线程
suspend 和 resume 的使用 在 Thread 类中有这样两个方法:suspend 和 resume,这两个方法是成对出现的. suspend() 方法的作用是将一个线程挂起(暂停), r ...
- 利用ManualResetEvent来来控制异步调用的打印的线程的暂停和恢复(转)
利用ManualResetEvent来来控制异步调用的打印的线程的暂停和恢复 打印过程可能很长,这时候有可能需要暂停下来做一些事情,然后回来继续接着打印 打印过程中有2个线程:一个是程序运行的主线程, ...
- 暂停和恢复Activity Android
暂停和恢复Activity(Pausing and Resuming an Activity) 在正常的应用程序使用,前台activity有时会被其他可视化组件遮挡,从而 造成activity的暂停. ...
- WPF控制动画开始、停止、暂停和恢复
1.闲言 好久也没更新一博客了,自己有点发懒,同时确实这几个月来也有点忙.风机监测软件,项目中,有这样一个小需求:正常风机在旋转的时候,上位机软要做一个风机的图片,让它不停地旋转,一但检测到下面风机停 ...
- Java/Android倒计时(开始,暂停,恢复,停止)
由于要做暂停和恢复,这里我就没有使用Android的CountDownTimer,而是用了Java的Timer.所以,这个方法在java肯定是通用.我也外加了Android独有的Service,有些计 ...
- CALayer的上动画的暂停和恢复
CHENYILONG Blog CALayer上动画的暂停和恢复 #pragma mark 暂停CALayer的动画-(void)pauseLayer:(CALayer*)layer{CFTimeIn ...
- Linux暂停和恢复进程
Linux暂停和恢复进程 kill -STOP 1234 将该进程暂停. 如果要让它恢复到后台,用kill -CONT 1234 (很多在前台运行的程序这样是不行的) 如果要恢复到前台,请在当时运行该 ...
随机推荐
- ASP.NET中的TextBox下划线
看到园子里一位同行写的 http://www.cnblogs.com/xiaopeng84/archive/2007/04/10/707093.html 但是没有贴出效果图,自己练了一下,贴出代码和效 ...
- Operating Cisco Router
Operating Cisco Router consider the hardware on the ends of the serial link, in particular where the ...
- Hadoop命令摘录
一:文件操作 1.建立目录 [hadoop@hadoop1:hadoop]$bin/hadoop dfs -mkdir testdir 在HDFS中建立一个名为testdir的目录 2.上传文件到HD ...
- c++中string类的详解
,<时返回-1,==时返回0 string的子串:string substr(int pos = 0,int n = npos) const;//返回pos开始的n个字符组成的字符串strin ...
- 【jquery】javaScript中prototype的妙用 巧妙运用prototype属性原型链创建对象
prototype 可以有好多有优化实现方法 http://blog.csdn.net/liuqiwen0512/article/details/8089690 在 JavaScript 中,每个函 ...
- Ha ha lou!
忙了一个晚上,终于稍微把这个模板修缮好了一点=-=//,然而我并不知道怎么像别的大牛一样,博客跟自己做的页面一样.总之今天就先到这里啦! 我的QQ是270115270,不知道会不会有人来呢=-=. ( ...
- RUP(Rational Unified Process)
RUP Rational Unified Process 目前阶段在学习UML,怎么会写RUP呢?学习UML是为了更好的把系统搭建好,RUP也是一样,为系统服务! 软件危机 美国国家总审计局,在198 ...
- WinForm程序界面假死,寻求完美解决方案
故事的开端是这样的,小白是一个程序员,他确实也是一个小白,目前还在程序员发展的道路上,兢兢业业的小心求学. 有一天,小白接到一个任务,完成一个Winform程序,附加一个功能就是可以读IC卡. 小白终 ...
- python SendMail 发送邮件
最近在学习python 时,用到了发送邮件的操作,通过整理总结如下: 1.普通文本邮件 普通文本邮件发送的实现,关键是要将MIMEText中_subtype设置为plain,首先导入smtplib和m ...
- 高质量的javascript代码 -- 深入理解Javascript
一. 编写高质量的javascript代码基本要点a) 可维护的代码(Writing Maintainable Code)i. 可读(注释)ii. 一致(看上去是同一个人写的)iii. 已记录b) 最 ...