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

  


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM