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());
        }
    }
}

  

猜你喜欢

转载自www.cnblogs.com/shenwenkai/p/10674941.html