SpringBoot2.3整合Quartz实现排除指定日期执行任务

SpringBoot2.3整合Quartz实现排除指定日期执行任务,第1张

SpringBoot2.3整合Quartz实现排除指定日期执行任务 1. 概述

前面两篇分别讲述了SpringBoot整合Quartz实现简单和动态定时任务,在实际工作中,通常需要周末或节假日不需要执行某个定时任务,Quartz也为我们提供了相应的方法。本文主要介绍SpringBoot整合Quartz如何实现排除指定日期。
Quartz中有个Calendar接口,其实现方法有7个,如下所示:
AnnualCalendar:用于排除一年中的某一天
baseCalendar:实现了Calendar基本功能
CronCalendar:用于排除Cronexpression中某个时间,例如需要排除上午8点到下午5点,可以使用* * 0-7,18-23 ? * *
DailyCalendar:用于排除一天中的某个时间段
HolidayCalendar:用于排除指定假日(指定日期)
MonthlyCalendar:用于排除一月的某天
WeeklyCalendar:用于排除一周的某天

2. 项目整合

核心依赖和配置信息请参阅SpringBoot2.3整合Quartz实现动态定时任务
这里只贴出不同的代码

@Service
public class ScheduleServiceImpl implements ScheduleService {

    @Autowired
    private Scheduler scheduler;

    @SneakyThrows
    @Override
    public void addSchedule(String jobName, String jobGroup, String jobClassName, String triggerName, String triggerGroup, String cronexpression) {
        try {
            scheduler.addCalendar("annuals", cronCalendar(), false, false);
            Class className = Class.forName(jobClassName);
            QuartzJobBean jobBean = (QuartzJobBean) className.newInstance();
            JobDetail jobDetail = JobBuilder.newJob(jobBean.getClass())
                    .withIdentity(jobName, jobGroup)
                    .storeDurably()
                    .build();
            CronTrigger cronTrigger = TriggerBuilder.newTrigger().modifiedByCalendar("annuals")
                    .withIdentity(triggerName, triggerGroup)
                    .withSchedule(CronScheduleBuilder.cronSchedule(cronexpression))
                    .build();
            scheduler.scheduleJob(jobDetail, cronTrigger);
            scheduler.start();
        } catch (SchedulerException | ClassNotFoundException e) {
            e.printStackTrace();
        } catch (IllegalAccessException e) {
            e.printStackTrace();
        } catch (InstantiationException e) {
            e.printStackTrace();
        }
    }

    private Calendar annualCalendar() {
        AnnualCalendar annualCalendar = new AnnualCalendar();
        GregorianCalendar gregorianCalendar = new GregorianCalendar();
        gregorianCalendar.setTime(newDate().inMonthOnDay(11, 5).build());
        annualCalendar.setDayExcluded(gregorianCalendar, true);
        return annualCalendar;
    }

    private Calendar cronCalendar() throws ParseException {
        CronCalendar cronCalendar = new CronCalendar("* 0-10,20-30 * * * ?");
        return cronCalendar;
    }

    private Calendar dailyCalendar() {
        DailyCalendar dailyCalendar = new DailyCalendar("08:00:00", "10:00:00");
        return dailyCalendar;
    }

    private Calendar holidayCalendar() {
        HolidayCalendar holidayCalendar = new HolidayCalendar();
        GregorianCalendar gregorianCalendar = new GregorianCalendar();
        gregorianCalendar.setTime(newDate().inMonthOnDay(11, 4).build());
        holidayCalendar.addExcludedDate(gregorianCalendar.getTime());
        return holidayCalendar;
    }

    private Calendar monthlyCalendar() {
        MonthlyCalendar monthlyCalendar = new MonthlyCalendar();
        monthlyCalendar.setDayExcluded(4, true);
        return monthlyCalendar;
    }

    private Calendar weeklyCalendar() {
        WeeklyCalendar weeklyCalendar = new WeeklyCalendar();
        weeklyCalendar.setDayExcluded(1, true);
        return weeklyCalendar;
    }
}

欢迎分享,转载请注明来源:内存溢出

原文地址: http://outofmemory.cn/zaji/5120826.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2022-11-17
下一篇 2022-11-17

发表评论

登录后才能评论

评论列表(0条)

保存