Quartz 定时器 相关的builder(CronScheduleBuilder,SimpleScheduleBuilder,CalendarIntervalScheduleBuilder,DailyTimeIntervalScheduleBuilder)使用
CronScheduleBuilder :使用corn 表达式 (定时循环)
CronScheduleBuilder builder = CronScheduleBuilder.cronSchedule("*/5 * * * * ?");
SimpleScheduleBuilder:每隔一段时间执行一次(时分秒),可以设置执行总次数
- RepeatForever:指定触发器将无限期重复。
- WithRepeatCount:指定重复次数
SimpleScheduleBuilder builder = SimpleScheduleBuilder .repeatSecondlyForTotalCount(5, 3); // .simpleSchedule() // .withIntervalInSeconds(3) //每隔3秒执行一次 //// .repeatForever()) //一直执行,奔腾到老不停歇 // .withRepeatCount(5); //重复计数,重复5次 (如果没有任务开始时间,会在创建触发器时就触发一次(n+1))
CalendarIntervalScheduleBuilder : 每隔一段时间执行一次(年月日)
- OnDaysOfTheWeek:设置触发器一周中的哪几天
- OnMondayThroughFriday:从星期一到星期五
- OnSaturdayAndSunday:周六和周日
- OnEveryDay:每天
- 每天10:00到23:10.00的每一分钟执行一次
CalendarIntervalScheduleBuilder builder = CalendarIntervalScheduleBuilder .calendarIntervalSchedule() // .withIntervalInMonths(1)// 每月执行一次 .withIntervalInWeeks(1); // 每周执行一次
DailyTimeIntervalScheduleBuilder : 设置年月日中的某些固定日期,可以设置执行总次数
1、指定每天的某个时间段内,以一定的时间间隔执行任务。并且它可以支持指定星期。
2、他适合的需求类似于:一个任务每天9点开始执行,下午5点执行完毕,每个一小时执行一次,并且只有周一到周四执行。
3、它的属性有:
- startTimeOfDay 每天开始时间
- endTimeOfDay 每天结束时间
- daysOfWeek 需要执行的星期
- interval 执行间隔
- repeatCount 重复次数
- 
DailyTimeIntervalScheduleBuilder .dailyTimeIntervalSchedule() // 使用dailyTimeIntervalSchedule .startingDailyAt(TimeOfDay.hourAndMinuteOfDay(9, 0)) // 设置开始时间,从九点开始 .endingDailyAt(TimeOfDay.hourAndMinuteOfDay(17, 0)) // 设置结束时间,下午五点结束 .withIntervalInHours(1) // 每一小时执行一次 .withRepeatCount(14)// 一共执行14次(实际执行14+1次) .onDaysOfTheWeek(1, 2, 3, 4); // 周一到周四执行。不写即每天执行 
public class QuartzService implements Job { public void execute(JobExecutionContext context) throws JobExecutionException { JobDetail detail = context.getJobDetail(); String taskId = detail.getJobDataMap().getString("taskId"); SimpleDateFormat sdf = new SimpleDateFormat("HH:mm:ss"); System.out.println(taskId + ":" + sdf.format(new Date())); System.out.println(); } // public void addScheduler(String jobGroup, String jobName, String taskId, String cron) { public static void main(String[] args) { try { //创建工作 JobDetail jobDetail = JobBuilder.newJob(QuartzService.class) // 任务执行类 .withDescription("工作的描述") //工作的描述 .withIdentity("jobName", "jobGroup") //工作的名称/工作的组 .usingJobData("key", "value") //定义属性 .build(); //trigger相关的builder // 使用corn 表达式 (定时循环) // CronScheduleBuilder builder = CronScheduleBuilder.cronSchedule("*/5 * * * * ?"); // 每隔一段时间执行一次(时分秒),执行总次数 // SimpleScheduleBuilder builder = SimpleScheduleBuilder // .repeatSecondlyForTotalCount(5, 3); // .simpleSchedule() // .withIntervalInSeconds(3) //每隔3秒执行一次 //// .repeatForever()) //一直执行,奔腾到老不停歇 // .withRepeatCount(5); //重复计数,重复5次 (如果没有任务开始时间,会在创建触发器时就触发一次(n+1)) // // 每隔一段时间执行一次(日期),执行总次数 // CalendarIntervalScheduleBuilder builder = CalendarIntervalScheduleBuilder // .calendarIntervalSchedule() // .withIntervalInMonths(1)// 每月执行一次 // .withIntervalInWeeks(1); // 每周执行一次 DailyTimeIntervalScheduleBuilder builder = DailyTimeIntervalScheduleBuilder.dailyTimeIntervalSchedule(); builder.dailyTimeIntervalSchedule() // 使用dailyTimeIntervalSchedule .startingDailyAt(TimeOfDay.hourAndMinuteOfDay(9, 0)) // 设置开始时间,从九点开始 .endingDailyAt(TimeOfDay.hourAndMinuteOfDay(17, 0)) // 设置结束时间,下午五点结束 .withIntervalInHours(1) // 每一小时执行一次 .withRepeatCount(14)// 一共执行14次(实际执行14+1次) .onDaysOfTheWeek(1, 2, 3, 4); // 周一到周四执行。不写即每天执行 //创建触发器 Trigger trigger = TriggerBuilder.newTrigger() .withDescription("触发器的描述") //触发器的描述 .withIdentity("jobName", "jobGroup")//触发器的名称/触发器的组 .withSchedule(builder) //定时任务执行时间 .startAt(new Date())//任务开始时间(不设置,默认为当前时间) .build(); //创建调度器,粘合工作和触发器 SchedulerFactory schedulerFactory = new StdSchedulerFactory(); Scheduler scheduler = schedulerFactory.getScheduler(); scheduler.scheduleJob(jobDetail, trigger); //启动调度器 scheduler.start(); //运行一段时间后关闭 // Thread.sleep(10000); // scheduler.shutdown(true); } catch (Exception e) { e.printStackTrace(); } }

 
                
            
         
         浙公网安备 33010602011771号
浙公网安备 33010602011771号