package com.ann.demo.job;

import org.quartz.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.scheduling.quartz.SchedulerFactoryBean;
import org.springframework.stereotype.Component;

/**
 * @Author: LeiJiaXin
 * @Date: 2019/8/16 10:08
 */
@Component
public class SchedulerAllJob {

    @Autowired
    private SchedulerFactoryBean schedulerFactoryBean;
    /*
     * 此处可以注入数据库操作，查询出所有的任务配置
     */

    /**
     * 该方法用来启动所有的定时任务
     *
     * @throws SchedulerException
     */
    public void scheduleJobs() throws SchedulerException {
        Scheduler scheduler = schedulerFactoryBean.getScheduler();
        scheduleJob3(scheduler);
    }

    private void scheduleJob3(Scheduler scheduler) throws SchedulerException {
        JobDetail jobDetail = JobBuilder.newJob(ScheduledJob3.class).withIdentity("job3", "group1").build();
        CronScheduleBuilder scheduleBuilder = CronScheduleBuilder.cronSchedule("0/15 * * * * ?");
        // 每5分钟执行一次
        CronTrigger cronTrigger = TriggerBuilder.newTrigger().withIdentity("job3", "group1").withSchedule(scheduleBuilder).build();
        scheduler.scheduleJob(jobDetail, cronTrigger);
    }
}
