资源简介
通过Quartz的API,自己实现了 添加JOB、删除、修改、暂停/恢复,停复机等的多种功能,已经可以满足基本项目的使用.
代码片段和文件信息
package com.bnc.util;
import java.util.List;
import org.apache.log4j.LogManager;
import org.apache.log4j.Logger;
import org.quartz.Cronexpression;
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.Trigger;
import org.quartz.TriggerBuilder;
import org.quartz.TriggerKey;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.stereotype.Component;
@Component(“quartz“)
public class QuartzUtil {
@Autowired
@Qualifier(“scheduler“)
private Scheduler scheduler;
protected static Logger logger = LogManager.getLogger(QuartzUtil.class);
private final int DEFAULT_PRIORITY = 5;
/**
* 供外界调用的方法此方法的作业对象名 与 调度器对象名一致
*
* @param name
* @param group
* @param jobClass
* @param cronexpression
*/
public void schedJob(String name String group
Class extends Job> jobClass String cronexpression) {
schedJob(name group jobClass name group cronexpression
DEFAULT_PRIORITY);
}
/**
* 供外界调用的方法此方法的作业对象名 与 调度器对象名一致
*
* @param name
* @param group
* @param jobClass
* @param cronexpression
* @param triggerPriority
* 触发器的优先级 默认为5 整形数值越大优先级越高
*/
public void schedJob(String name String group
Class extends Job> jobClass String cronexpression
int triggerPriority) {
schedJob(name group jobClass name group cronexpression
triggerPriority);
}
/**
*
* @param jobName
* @param jobGroup
* @param jobClass
* @param trgName
* @param trgGroup
* @param cronexpression
*/
public void schedJob(String jobName String jobGroup
Class extends Job> jobClass String trgName String trgGroup
String cronexpression) {
schedJob(jobName jobGroup jobClass trgName trgGroup
cronexpression DEFAULT_PRIORITY);
}
/**
* 供外界调用的方法
*
* @param jobName
* @param jobGroup
* @param jobClass
* @param trgName
* @param trgGroup
* @param cronexpression
* @param triggerPriority
*/
public void schedJob(String jobName String jobGroup
Class extends Job> jobClass String trgName String trgGroup
String cronexpression int triggerPriority) {
JobDetail jobDetail = addJob(jobName jobGroup jobClass);
CronTrigger cronTrigger = addCronTrigger(trgName trgGroup
cronexpression triggerPriority);
scheduleJob(jobDetail cronTrigger);
}
/**
* @param name
* 作业名
* @param group
* 作业组 可为null
* @param jobClass
* 需继承 org.quartz.Job 接口
* @return JobDetail
*/
private JobDetail addJob(String name String group
Class extends Job> jobClass) {
JobKey jobKey = new JobKey(nam
评论
共有 条评论