数据库配置创建quartz调度任务

本文使用数据库配置的方式创建quartz调度任务,告别xml配置。可以便于管理,灵活配置

quart 核心元素

1.Scheduler:任务调度器,是实际执行任务调度的控制器。
2.Trigger:触发器,用于定义任务调度的时间规则。
3.TriggerBuilder:定义和创建Trigger实例的接口;
4.JobDetail:用来描述Job实现类及其它相关的静态信息。
5.JobBuilder:定义和创建JobDetail实例的接口;
6.Job:是一个接口,只有一个方法void execute(JobExecutionContext context),开发者实现该接口定义运行任务,JobExecutionContext类提供了调度上下文的各种信息。

代码

看代码之前,请先了解上面几个对象.

public void startJob(){
        try {
            //查询job配置列表
            List quartzTaskList = quartzTaskMapper.selectList(null);
            if (quartzTaskList.size() == 0) {
                log.info("JobStarter.startJob not Waiting for execution task");
                return;
            }

            for (QuartzTaskInformations quartzTaskInformations:quartzTaskList) {
                try {
                    //创建任务调度器
                    Scheduler scheduler = schedulerBean.getScheduler();
                    //删除调度任务
                    scheduler.deleteJob(new JobKey(quartzTaskInformations.getTaskno()));
                    //创建执行调度任务
                    this.schedule(quartzTaskInformations, scheduler);
                    log.info("taskNo={},taskName={},scheduleRule={},任务启动成功", quartzTaskInformations.getTaskno(), quartzTaskInformations.getTaskname(), quartzTaskInformations.getSchedulerrule());
                } catch (SchedulerException e) {
                    log.error("JobStarter ["+quartzTaskInformations.getTaskname()+"] start error:"+e );
                }
            }
        } catch (Exception e) {
            e.printStackTrace();
            log.error("JobStarter error:",e);
        }

    }

    public void schedule(QuartzTaskInformations quartzTaskInfo, Scheduler scheduler) throws SchedulerException {
        TriggerKey triggerKey = TriggerKey.triggerKey(quartzTaskInfo.getTaskno(), Scheduler.DEFAULT_GROUP);
        //创建job
        JobDetail jobDetail = JobBuilder.newJob(JobFactory.getJob(quartzTaskInfo.getTaskname()).getClass()).withDescription(quartzTaskInfo.getTaskname()).withIdentity(quartzTaskInfo.getTaskno(), Scheduler.DEFAULT_GROUP).build();
        //设置job参数
        JobDataMap jobDataMap = jobDetail.getJobDataMap();
        jobDataMap.put("id", quartzTaskInfo.getId());
        jobDataMap.put("taskNo", quartzTaskInfo.getTaskno());
        jobDataMap.put("executorNo", quartzTaskInfo.getExecutorno());
        jobDataMap.put("executeParameter", quartzTaskInfo.getExecuteparamter());
        //定义触发器
        CronTrigger cronTrigger = TriggerBuilder.newTrigger().withDescription(quartzTaskInfo.getTaskname()).
                withIdentity(triggerKey).withSchedule(CronScheduleBuilder.cronSchedule(quartzTaskInfo.getSchedulerrule())).build();
         //执行job
        scheduler.scheduleJob(jobDetail, cronTrigger);
        log.info("taskNo={},taskName={},scheduleRule={} load to quartz success!", quartzTaskInfo.getTaskno(), quartzTaskInfo.getTaskname(), quartzTaskInfo.getSchedulerrule());
    }


//job工厂
public class JobFactory {
    private static Logger log = LogManager.getLogger(JobFactory.class.getName());
    public static Job getJob(String taskName){
        try {
            return (Job) ApplicationContextProvider.getBean(taskName);
        } catch (Exception e) {
            log.error("JobFactory getJob error:"+e);
            return null;
        }
    }
}

public class QuartzTaskInformations implements Serializable {
    private int id;
   
    //任务编号
    private String taskno;
    //任务名称
    private String taskname;
    //corn表达式
    private String schedulerrule;
    //状态
    private String frozenstatus;
    //执行人
    private String executorno;
    
    private String createtime;

    private String lastmodifytime;
    //参数
    private String executeparamter;
    //描述
    private String remark;
}

你可能感兴趣的:(quartz)