zoukankan      html  css  js  c++  java
  • quartz延迟执行一次

    package com.example.balabala;
    
    import org.quartz.*;
    import org.quartz.impl.StdSchedulerFactory;
    import org.springframework.boot.SpringApplication;
    import org.springframework.boot.autoconfigure.SpringBootApplication;
    
    import java.text.SimpleDateFormat;
    import java.util.Date;
    
    import static org.quartz.JobBuilder.newJob;
    import static org.quartz.SimpleScheduleBuilder.simpleSchedule;
    import static org.quartz.TriggerBuilder.newTrigger;
    
    @SpringBootApplication
    public class LibeiApplication {
    
        public static void main(String[] args) throws Exception {
            SpringApplication.run(LibeiApplication.class, args);
    
            Scheduler scheduler = StdSchedulerFactory.getDefaultScheduler();
    
            JobDetail job = newJob(LibeiApplication.TestJob.class)
                    .withIdentity("cronJob", "testJob")
                    .build();
    
            String startDateStr = "2019-04-08 10:11:00";
            String endDateStr = "2019-04-10 15:35:00";
    
            Date startDate = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").parse(startDateStr);
            Date endDate = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").parse(endDateStr);
    
            Trigger cronTrigger = newTrigger()
                    .withIdentity("trigger1", "testJob")
                    .startAt(startDate)
    //                .withSchedule(CalendarIntervalScheduleBuilder.calendarIntervalSchedule().withIntervalInMinutes(1))
    //                .withSchedule(SimpleScheduleBuilder.simpleSchedule().withIntervalInMinutes(1).withRepeatCount(2))
                    .endAt(endDate)
                    .build();
    
            scheduler.scheduleJob(job, cronTrigger);
            scheduler.start();
        }
    
        public static class TestJob implements Job {
            @Override
            public void execute(JobExecutionContext context) throws JobExecutionException {
                System.out.println("this is a cron scheduled test job");
                System.out.println(new Date());
            }
        }
    }
    

      

  • 相关阅读:
    创建型模式(四) 单例模式
    创建型模式(三) 原型模式
    创建型模式(二) 建造者模式
    创建型模式(一) 简单工厂模式、工厂模式与抽象工厂模式
    Django15-分页功能
    Django14-Ajax删除按钮动态效果
    网络day04-配置备份、清除、密码恢复、IOS更新
    网络day03-NTP配置和SMTP配置
    网络day02-设备配置远程登录
    HTML注释
  • 原文地址:https://www.cnblogs.com/shenwenkai/p/10674941.html
Copyright © 2011-2022 走看看