学习黄勇smart-plugin-job的笔记
黄勇的码云地址:https://gitee.com/huangyong/smart-plugin-job
job封装了任务调度框架quartz的一些操作,可以做为插件集成在smart-framework中。
public class JobHelper {
private static final Logger logger = LoggerFactory.getLogger(JobHelper.class);
// 存放job类和对应的调用类
// map 只存放当前已经执行的调度任务
private static final Map<Class<?>, Scheduler> jobMap = new HashMap<Class<?>, Scheduler>();
// 从写JobFactory,自定义获取job类的实例
private static final JobFactory jobFactory = new SmartJobFactory();
/**
* 开启调度
*
* @param jobClass job类
* @param cron 调度规则
*/
public static void startJob(Class<?> jobClass, String cron) {
// 需要一个任务
JobDetail jobDetail = createJobDetail(jobClass);
// 需要一个调度规则
Trigger trigger = createTrigger(jobClass, cron);
// 执行调度
doStartJob(jobClass, jobDetail, trigger);
}
public static void startJob(Class<?> jobClass, int second) {
startJob(jobClass, second, 0, "", "");
}
public static void startJob(Class<?> jobClass, int second, int count) {
startJob(jobClass, second, count, "", "");
}
/**
* @param jobClass 任务类
* @param second 间隔
* @param count 次数
* @param start 开始时间
* @param end 结束时间
*/
public static void startJob(Class<?> jobClass, int second, int count, String start, String end) {
JobDetail jobDetail = createJobDetail(jobClass);
Trigger trigger = createTrigger(jobClass, second, count, start, end);
doStartJob(jobClass, jobDetail, trigger);
}
private static void doStartJob(Class<?> jobClass, JobDetail jobDetail, Trigger trigger) {
try {
// 创建调度对象
Scheduler scheduler = createScheduler(jobDetail, trigger);
scheduler.start();
// 放到调度map
jobMap.put(jobClass, scheduler);
logger.debug("[Smart] start job: " + jobClass.getName());
} catch (SchedulerException e) {
logger.error("启动 Job 出错!", e);
}
}
public static void startJobAll() {
// 获取所有任务类,继承了BaseJob就是任务类
List<Class<?>> jobClassList = ClassHelper.getClassListBySuper(BaseJob.class);
if (CollectionUtil.isNotEmpty(jobClassList)) {
for (Class<?> jobClass : jobClassList) {
if (jobClass.isAnnotationPresent(Job.class)) {
// 获取Job注解,有TIMER 和Job 两种定时规则
Job job = jobClass.getAnnotation(Job.class);
Job.Type type = job.type();
if (type == Job.Type.CRON) {
String cron = job.value();
startJob(jobClass, cron);
} else if (type == Job.Type.TIMER) {
int second = job.second();
int count = job.count();
String start = job.start();
String end = job.end();
startJob(jobClass, second, count, start, end);
}
}
}
}
}
public static void stopJob(Class<?> jobClass) {
try {
// 获取调度类
Scheduler scheduler = getScheduler(jobClass);
scheduler.shutdown(true);
jobMap.remove(jobClass); // 从 jobMap 中移除该 Job
logger.debug("[Smart] stop job: " + jobClass.getName());
} catch (SchedulerException e) {
logger.error("停止 Job 出错!", e);
}
}
public static void stopJobAll() {
for (Class<?> jobClass : jobMap.keySet()) {
stopJob(jobClass);
}
}
/**
* 暂停调度
*
* @param jobClass
*/
public static void pauseJob(Class<?> jobClass) {
try {
Scheduler scheduler = getScheduler(jobClass);
scheduler.pauseJob(new JobKey(jobClass.getName()));
logger.debug("[Smart] pause job: " + jobClass.getName());
} catch (SchedulerException e) {
logger.error("暂停 Job 出错!", e);
}
}
/**
* 继续暂停的调度
*
* @param jobClass
*/
public static void resumeJob(Class<?> jobClass) {
try {
Scheduler scheduler = getScheduler(jobClass);
scheduler.resumeJob(new JobKey(jobClass.getName()));
logger.debug("[Smart] resume job: " + jobClass.getName());
} catch (SchedulerException e) {
logger.error("恢复 Job 出错!", e);
}
}
/**
* 创建任务类
*
* @param jobClass
* @return
*/
@SuppressWarnings("unchecked")
private static JobDetail createJobDetail(Class<?> jobClass) {
return JobBuilder.newJob((Class<? extends org.quartz.Job>) jobClass)
.withIdentity(jobClass.getName())
.build();
}
/**
* 创建触发类
*
* @param jobClass
* @param cron
* @return
*/
private static CronTrigger createTrigger(Class<?> jobClass, String cron) {
return TriggerBuilder.newTrigger()
.withIdentity(jobClass.getName())
.withSchedule(CronScheduleBuilder.cronSchedule(cron))
.build();
}
/**
* 创建触发类
*
* @param jobClass
* @param second
* @return
*/
private static SimpleTrigger createTrigger(Class<?> jobClass, int second) {
return TriggerBuilder.newTrigger()
.withIdentity(jobClass.getName())
.withSchedule(SimpleScheduleBuilder.repeatSecondlyForever(second))
.build();
}
private static SimpleTrigger createTrigger(Class<?> jobClass, int second, int count) {
return createTrigger(jobClass, second, count, null, null);
}
/**
* count<=0 代表次数无穷
*
* @param jobClass
* @param second
* @param count
* @param start
* @param end
* @return
*/
private static SimpleTrigger createTrigger(Class<?> jobClass, int second, int count, String start, String end) {
TriggerBuilder<SimpleTrigger> triggerBuilder;
if (count > 0) {
triggerBuilder = TriggerBuilder.newTrigger()
.withIdentity(jobClass.getName())
.withSchedule(SimpleScheduleBuilder.repeatSecondlyForTotalCount(count, second));
} else {
triggerBuilder = TriggerBuilder.newTrigger()
.withIdentity(jobClass.getName())
.withSchedule(SimpleScheduleBuilder.repeatSecondlyForever(second));
}
return doCreateTrigger(triggerBuilder, start, end);
}
private static SimpleTrigger doCreateTrigger(TriggerBuilder<SimpleTrigger> triggerBuilder, String start, String end) {
if (StringUtil.isNotEmpty(start)) {
triggerBuilder.startAt(DateUtil.parseDatetime(start));
}
if (StringUtil.isNotEmpty(end)) {
triggerBuilder.endAt(DateUtil.parseDatetime(end));
}
return triggerBuilder.build();
}
/**
* 创建调度类
*
* @param jobDetail 任务类
* @param trigger 触发类
* @return
* @throws SchedulerException
*/
private static Scheduler createScheduler(JobDetail jobDetail, Trigger trigger) throws SchedulerException {
Scheduler scheduler = StdSchedulerFactory.getDefaultScheduler();
// 设置JobFactory ,执行调度时要获取Job实例
scheduler.setJobFactory(jobFactory); // 从 Smart IOC 容器中获取 Job 实例
// 调度的真正任务类是jobDetail
scheduler.scheduleJob(jobDetail, trigger);
return scheduler;
}
private static Scheduler getScheduler(Class<?> jobClass) {
Scheduler scheduler = null;
// zz 查询了两次map
if (jobMap.containsKey(jobClass)) {
scheduler = jobMap.get(jobClass);
}
return scheduler;
}
}
public class SmartJobFactory implements JobFactory {
@Override
public Job newJob(TriggerFiredBundle bundle, Scheduler scheduler) throws SchedulerException {
JobDetail jobDetail = bundle.getJobDetail();
Class<? extends Job> jobClass = jobDetail.getJobClass();
return BeanHelper.getBean(jobClass);
}
}