/*
* file name: ScheduleJobFactory.java
* copyright: Unis Cloud Information Technology Co., Ltd. Copyright 2015, All rights reserved
* description: <description>
* mofidy staff: zheng
* mofidy time: 2015年8月22日
*/
package com.quartz; import java.util.List; import net.sf.json.JSONObject; import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.quartz.CronScheduleBuilder;
import org.quartz.CronTrigger;
import org.quartz.Job;
import org.quartz.JobBuilder;
import org.quartz.JobDetail;
import org.quartz.JobKey;
import org.quartz.Scheduler;
import org.quartz.SchedulerException;
import org.quartz.SimpleScheduleBuilder;
import org.quartz.Trigger;
import org.quartz.TriggerBuilder; /**
* To create scheduler job
*
* @author xpz
* @version [version, Jul 18, 2015]
* @see [about class/method]
* @since [product/module version]
*/
public class ScheduleJobFactory {
private static Log log = LogFactory.getLog(ScheduleJobFactory.class); /***
* create a simpleScheduler job
*
* @param scheduler: the scheduler
* @param jobName: the job name
* @param jobGroup: the job group
* @param triggerName: the trigger name
* @param triggerGroup: the trigger group
* @param ssb: the schedBuilder (eg:SimpleScheduleBuilder ssb =
* SimpleScheduleBuilder.simpleSchedule() .withIntervalInMinutes(1)
* .repeatForever();)
* @param jobClass: the class which implement
* job/StatefulJob(@DisallowConcurrentExecution)
* @param params [explain parameter]
* @return void [explain return type]
* @exception throws [exception type] [explain exception]
* @see [class,class#method,class#member]
*/
public static void createSimpleJob(Scheduler scheduler, String jobName,
String jobGroup, String triggerName, String triggerGroup,
SimpleScheduleBuilder ssb, Class<? extends Job> jobClass,
JSONObject params) {
// build a schedule job
JobDetail jobDetail = JobBuilder.newJob(jobClass)
.withIdentity(jobName, jobGroup)
.build();
// set job name
jobDetail.getJobDataMap().put("name", jobName);
// set job params
if (params != null) {
jobDetail.getJobDataMap().put("params", params);
}
// build a schedule simpleTrigger
Trigger trigger = TriggerBuilder.newTrigger()
.withIdentity(triggerName, triggerGroup)
.startNow()
.withSchedule(ssb)
.build();
// create a new schedule job
try {
scheduler.scheduleJob(jobDetail, trigger);
}
catch (SchedulerException e) {
log.info("error with create a simple schedule job!", e);
}
} /***
* create a cronScheduler job
*
* @param scheduler: the scheduler
* @param jobName: the job name
* @param jobGroup: the job group
* @param triggerName: the trigger name
* @param triggerGroup: the trigger group
* @param cronExpression: the cron expression
* @param jobClass: the class which implements
* job/StatefulJob(@DisallowConcurrentExecution)
* @param params: the param need to trransfer
* @return void [explain return type]
* @exception throws [exception type] [explain exception]
* @see [class,class#method,class#member]
*/
public static void createCronJob(Scheduler scheduler, String jobName,
String jobGroup, String triggerName, String triggerGroup,
String cronExpression, Class<? extends Job> jobClass,
JSONObject params) {
// build a schedule job
JobDetail jobDetail = JobBuilder.newJob(jobClass)
.withIdentity(jobName, jobGroup)
.build();
// set job name
jobDetail.getJobDataMap().put("name", jobName);
// set job params
if (params != null) {
jobDetail.getJobDataMap().put("params", params);
}
// build a schedule cronTrigger
CronTrigger trigger = TriggerBuilder.newTrigger()
.withIdentity(triggerName, triggerGroup)
.withSchedule(CronScheduleBuilder.cronSchedule(cronExpression))
.build();
// create a new schedule job
try {
scheduler.scheduleJob(jobDetail, trigger);
}
catch (SchedulerException e) {
log.info("error with create a simple schedule job!", e);
}
} /***
* delete job from scheduler
* @param scheduler
* @param job
* @param jobGroup [explain parameter]
*
* @return void [explain return type]
* @exception throws [exception type] [explain exception]
* @see [class,class#method,class#member]
*/
public static void deleteJob(Scheduler scheduler, String jobName, String jobGroup){
try {
JobKey jobKey = JobKey.jobKey(jobName, jobGroup);
if(scheduler.checkExists(jobKey)){
scheduler.pauseJob(jobKey);
//Get all Triggers that are associated with the identified job
List<? extends Trigger> triggers = scheduler.getTriggersOfJob(jobKey);
for(Trigger trigger : triggers){
scheduler.pauseTrigger(trigger.getKey());
boolean unscJobFlag = scheduler.unscheduleJob(trigger.getKey());
if(!unscJobFlag)
log.error("error with unschedule job");
}
} //Interrupt all instances of the identified InterruptableJob executing in this Scheduler instance.
if(scheduler.checkExists(jobKey)){
boolean interruptFlag = scheduler.interrupt(jobKey);
if(!interruptFlag)
log.error("error with interrupt the job");
} if(scheduler.checkExists(jobKey)){
boolean delJobFlag = scheduler.deleteJob(jobKey);
if(!delJobFlag)
log.error("error with delete the job");
}
}
catch (SchedulerException e) {
e.printStackTrace();
}
}
}

ScheduleJobFactory的更多相关文章

随机推荐

  1. Unity通过指定摄像机截屏

    简介 介于照抄网上之前的截图教程,然后在实际应用过程中出现了一些小小的问题,修正了一下下,特此分享一下 PS:代码在后面 原理 原理很简单,就是将一个相机的内容渲染到一个贴图上,然后将贴图保存为图片 ...

  2. 在Swift中,如何像Objective-C定义可选接口?

    Objective-C中的protocol里存在@optional关键字,被这个关键字修饰的方法并非必须要被实现.我们可以通过接口定义一系列方法,然后由实现接口的类选择性地实现其中几个方法.在Coco ...

  3. 关于onActivityResult方法不执行的问题汇总

    我们不生产代码, 只是大自然的搬运工.  首先致谢: https://blog.csdn.net/sbvfhp/article/details/26858441 场景描述: 在A activity(由 ...

  4. jQuery制作顶部与左侧锚点板块定位功能带动画跳转特效

    <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/ ...

  5. [Windows Server 2008] SQL Server 2008 数据库还原方法

    ★ 欢迎来到[护卫神·V课堂],网站地址:http://v.huweishen.com★ 护卫神·V课堂 是护卫神旗下专业提供服务器教学视频的网站,每周更新视频.★ 本节我们将带领大家:还原SQL S ...

  6. C# 金钱添加逗号0000

    private void Form1_Load(object sender, EventArgs e) { decimal dd = (decimal)11234567890.01; string d ...

  7. css中有些属性的前面会加上“*”或“_(兼容IE浏览器)

    给不同浏览器识别: color{ background-color: #CC00FF; /*所有浏览器都会显示为紫色*/ background-color: #FF0000\9; /*IE6.IE7. ...

  8. CAD处理键盘被按下事件(com接口VB语言)

    主要用到函数说明: MxDrawXCustomEvent::KeyDown 键盘被按下,详细说明如下: 参数 说明 LONG lVk 是按钮码,如F8,的值为#define VK_F8 0x77 返回 ...

  9. VMware虚拟机下Ubuntu安装VMware Tools详解

    一.安装步骤 1.开启虚拟机,运行想要安装VMware Tools的系统,运行进入系统后,点击虚拟机上方菜单栏的“虚拟机(M)”->点击“安装 VMware Tools”,图片所示是因为我已经安 ...

  10. 洛谷——P1572 计算分数

    P1572 计算分数 模拟+字符串 注意有两位数的情况以及负数情况 #include<bits/stdc++.h> using namespace std; string s; ],b[] ...