/*
* 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. define与typedef的区别

    define: 发生在预处理阶段,也就是编译之前,仅仅文本替换,不做任何的类型检查 没有作用域的限制 typedef: 多用于简化复杂的类型声明,比如函数指针声明:typedef bool (*fun ...

  2. Android 检查手机上是否安装了指定的软件(根据包名检测)

    Android检查手机上是否安装了指定的软件(根据包名检测) /** * 检查手机上是否安装了指定的软件 * @param context * @param packageName * @return ...

  3. windows远程桌面连接

    服务器端: 1.我的电脑->管理->本地用户和组->用户->新建用户设置账号密码,隶属于administrator和remote user 2.我的电脑->属性-> ...

  4. Nrpe 插件安装教程

    Nrpe 插件安装教程  blog地址: http://www.cnblogs.com/caoguo 一.nagios plugins的安装 [root@Nrpe ~]# yum install -y ...

  5. HDU_2212_水

    DFS Time Limit: 5000/2000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)Total Submiss ...

  6. C# SetWindowsHookEx

    [DllImport("user32.dll")] static extern IntPtr SetWindowsHookEx(int idHook, keyboardHookPr ...

  7. Jenkins里jobs和workspace什么区别

    https://segmentfault.com/q/1010000012575095/a-1020000012590560 简单的说,job 中保存的是项目是在 jenkins 上的配置.日志.构建 ...

  8. lsof command not found 解决

    有些centos 没有 lsof命令,需要安装 yum install lsof -y 使用: lsof -i:端口号

  9. MVCHelper 请求检验

    public class MVCHelper { //有 两 个ModelStateDictionary类,别弄混乱了要使用 System.Web.Mvc 下的 //如果添加引用中找不到System. ...

  10. inet_XX族函数

    在网络编程中, 经常会将网络字节转为本地字节或者将本地字节转为网络字节, 但是如果每次我们都是都通过htonl, ntohl函数需要将10进制转为整数, 甚至还用将字符串转为整数, 再转为网络字节, ...