spring boot根据动态的corn表达式执行定时任务

spring boot配置定时任务本人已知的有两种方法,一种是使用注解@Scheduled,使用这个时需要在spring boot的启动类上添加@EnableScheduling注解以及在使用类中添加@Component方能使用,只需要了解corn的表达式便可以配置定时任务执行的时间。
但是如果是配置动态的corn表达式,此方法就不是很合适,这时需要用到quart定时器框架。
Quartz是纯Java实现,而且作为Spring的默认调度框架,由于Quartz的强大的调度功能、灵活的使用方式、还具有分布式集群能力,可以说Quartz出马,可以搞定一切定时任务调度!
spring boot根据动态的corn表达式执行定时任务_第1张图片
本人自己的项目中涉及到定时推送数据任务场景,corn的表达式都需要从数据库读取,所以便采用了quart框架。

@Configuration
public class ShareResAllocaJobConfig implements ApplicationListener<ContextRefreshedEvent>{
	
	@Autowired
    private ShareResQuartzScheduler quartzScheduler;
	
	@Autowired
    private SharePlanService sharePlanService;


    /**
     * 初始启动quartz
     */
    public void onApplicationEvent(ContextRefreshedEvent event) {
    	try {
        	SharePlan sharePlan = new SharePlan();
        	sharePlan.setShareStatus(-1);
        	List<SharePlan> sharePlanList = sharePlanService.selectSharePlanByStatus(sharePlan);
        	if (sharePlanList != null && sharePlanList.size() > 0) {
        		for (SharePlan sharePlan2 : sharePlanList) {
        			String executePlan = sharePlan2.getExecutePlan();
        			if (StringUtils.isNotBlank(sharePlan2.getInDataSourceId()) && StringUtils.isNotBlank(executePlan)) {
        				executePlan = QuartzCronUtils.translateStringToCronExpression(executePlan);
                        //查询所有运行中的任务   把任务添加到任务管理器,这里便从数据库中查询动态的corn表达式存入接下来的调度任务对象中
                        quartzScheduler.addJob(SharePlanJobDetail.class, sharePlan2.getShareId(), Constants.GROUP_NAME, executePlan);

        			    if (sharePlan2.getShareStatus() < 1) {
        			    	quartzScheduler.pauseJob(sharePlan2.getShareId(), Constants.GROUP_NAME);//暂停任务
        			    }
        			}
				} 
        	}
		} catch (Exception e) {
			e.printStackTrace();
		}
    	
        System.out.println("任务已经启动...");
    }

    /**
     * 初始注入scheduler
     * @return
     * @throws SchedulerException
     */
    @Bean
    public Scheduler scheduler(SchedulerFactoryBean schedulerFactoryBean) throws SchedulerException{
        return schedulerFactoryBean.getScheduler();
    }


    @Bean
    public MyJobFactory jobFactory(){
        return new MyJobFactory();
    }

    @Bean
    public SchedulerFactoryBean schedulerFactoryBean(MyJobFactory jobFactory){
        SchedulerFactoryBean schedulerFactoryBean = new SchedulerFactoryBean();
        schedulerFactoryBean.setJobFactory(jobFactory);
        return schedulerFactoryBean;
    }

}

这个类的作用是在初始化项目时配置调度器以及创建Scheduler工厂,

@Configuration
public class ShareResQuartzScheduler {
    // 任务调度
    @Autowired
    private Scheduler scheduler;

    /**
     * 开始执行所有任务
     * 
     * @throws SchedulerException
     */
    public void startJob() throws SchedulerException {
        scheduler.start();
    }

    /**
     * 获取Job信息
     * 
     * @param name
     * @param group
     * @return
     * @throws SchedulerException
     */
    public String getJobInfo(String name, String group) throws SchedulerException {
        TriggerKey triggerKey = new TriggerKey(name, group);
        CronTrigger cronTrigger = (CronTrigger) scheduler.getTrigger(triggerKey);
        return String.format("time:%s,state:%s", cronTrigger.getCronExpression(),
                scheduler.getTriggerState(triggerKey).name());
    }
    
    /**
     * 判断任务是否存在
     * @param name
     * @param group
     * @return
     * @throws SchedulerException
     */
    public JobDetail exitJobByJobname(String name,String group) throws SchedulerException {
    	JobKey jobKey = new JobKey(name, group);
    	JobDetail jobDetail = scheduler.getJobDetail(jobKey);
        return jobDetail;
    }

    /**
     * 修改某个任务的执行时间
     * 
     * @param name
     * @param group
     * @param time
     * @return
     * @throws SchedulerException
     */
    public boolean modifyJob(String name, String group, String time) throws SchedulerException {
        Date date = null;
        TriggerKey triggerKey = new TriggerKey(name, group);
        CronTrigger cronTrigger = (CronTrigger) scheduler.getTrigger(triggerKey);
        String oldTime = cronTrigger.getCronExpression();
        if (!oldTime.equalsIgnoreCase(time)) {
            CronScheduleBuilder cronScheduleBuilder = CronScheduleBuilder.cronSchedule(time);
            CronTrigger trigger = TriggerBuilder.newTrigger().withIdentity(name, group)
                    .withSchedule(cronScheduleBuilder).build();
            date = scheduler.rescheduleJob(triggerKey, trigger);
        }
        return date != null;
    }

    /**
     * 暂停所有任务
     * 
     * @throws SchedulerException
     */
    public void pauseAllJob() throws SchedulerException {
        scheduler.pauseAll();
    }

    /**
     * 暂停某个任务
     * 
     * @param name
     * @param group
     * @throws SchedulerException
     */
    public void pauseJob(String name, String group) throws SchedulerException {
        JobKey jobKey = new JobKey(name, group);
        JobDetail jobDetail = scheduler.getJobDetail(jobKey);
        if (jobDetail == null)
            return;
        scheduler.pauseJob(jobKey);
    }

    /**
     * 恢复所有任务
     * 
     * @throws SchedulerException
     */
    public void resumeAllJob() throws SchedulerException {
        scheduler.resumeAll();
    }

    /**
     * 恢复某个任务
     * 
     * @param name
     * @param group
     * @throws SchedulerException
     */
    public void resumeJob(String name, String group) throws SchedulerException {
        JobKey jobKey = new JobKey(name, group);
        JobDetail jobDetail = scheduler.getJobDetail(jobKey);
        if (jobDetail == null)
            return;
        scheduler.resumeJob(jobKey);
    }

    /**
     * 删除某个任务
     * 
     * @param name
     * @param group
     * @throws SchedulerException
     */
    public void deleteJob(String name, String group) throws SchedulerException {
        JobKey jobKey = new JobKey(name, group);
        JobDetail jobDetail = scheduler.getJobDetail(jobKey);
        if (jobDetail == null)
            return;
        scheduler.deleteJob(jobKey);
    }

    public void addJob(Class<? extends Job> jobClass,String name, String group,String cronTime) throws SchedulerException {
        //删除任务
    	deleteJob(name, group);
    	
    	// 通过JobBuilder构建JobDetail实例,JobDetail规定只能是实现Job接口的实例
        // JobDetail 是具体Job实例
        JobDetail jobDetail = JobBuilder.newJob(jobClass).withIdentity(name, group)
        		.usingJobData("shareId", name)
        		.build();
        
        // 基于表达式构建触发器
        CronScheduleBuilder cronScheduleBuilder = CronScheduleBuilder.cronSchedule(cronTime);
        
        // CronTrigger表达式触发器 继承于Trigger
        // TriggerBuilder 用于构建触发器实例
        CronTrigger cronTrigger = TriggerBuilder.newTrigger().withIdentity(name, group)
                .withSchedule(cronScheduleBuilder).build();
        
        scheduler.scheduleJob(jobDetail, cronTrigger);
    }

    public void addOnceJob(Class<? extends Job> jobClass,String name, String group, Date startTime) throws SchedulerException {
        //删除任务
        deleteJob(name, group);

        JobDetail jobDetail = JobBuilder.newJob(jobClass).withIdentity(name, group)
                .usingJobData("shareId", name)
                .build();

        SimpleScheduleBuilder simpleScheduleBuilder = SimpleScheduleBuilder.simpleSchedule()
                .withIntervalInSeconds(0)
                .withRepeatCount(0);

        SimpleTrigger simpleTrigger = (SimpleTrigger) TriggerBuilder.newTrigger().withIdentity(name, group)
                .startAt(startTime)
                .withSchedule(simpleScheduleBuilder)
                .build();

        scheduler.scheduleJob(jobDetail, simpleTrigger);
    }
}

这个类的作用是对任务调度的处理,主要的是addJob()方法,在这个方法中进行具体任务的设置,也就是要了解JobDetail和CronTrigger。
jobdetail类:JobDetail为Job实例提供了许多设置属性,以及JobDataMap成员变量属性,它用来存储特定job实例的状态信息系,调度器需要借助Jobdetail对象来添加Job实例。

重要属性:

name:任务的名称,必须属性

group:任务所在的组,也是必须,默认值是DEFAULT,必须的

jobclass:任务的实现类,必须的

jobdatamap:用于传递传参数

而CronTrigger则是创建触发器的对象

CronScheduleBuilder cronScheduleBuilder = CronScheduleBuilder.cronSchedule(cronTime);

创建CronScheduleBuilder的对象,cronTime就是动态的corn参数,

// CronTrigger表达式触发器 继承于Trigger
        // TriggerBuilder 用于构建触发器实例
        CronTrigger cronTrigger = TriggerBuilder.newTrigger().withIdentity(name, group)
                .withSchedule(cronScheduleBuilder).build();
        
        scheduler.scheduleJob(jobDetail, cronTrigger);

创建完成后将job detail的对象和cronTrigger的对象添加到调度任务scheduler对象的scheduleJob()方法中,这样调度任务便创建完成了。

@DisallowConcurrentExecution
public class SharePlanJobDetail implements Job {
	
	private void before(){
		System.out.println("任务开始执行");
	}

	@Override
	public void execute(JobExecutionContext jobExecutionContext) throws JobExecutionException {
		
		System.out.println("开始:"+System.currentTimeMillis());
		Long startTime = System.currentTimeMillis();

			JobDataMap dataMap = jobExecutionContext.getJobDetail().getJobDataMap();
			String shareId = dataMap.getString("shareId");
}

接着需要在具体的业务逻辑类中实现Job接口,执行便会到了任务执行的时间自动执行execute()方法。
dataMap获取的就是之前放入到jobDetail的name的值。

这样,使用quart框架配置定时任务便完成了。

另附上corn表达式生成网址:(http://cron.qqe2.com/)

你可能感兴趣的:(Quart)