原生Java集成Quartz

  1. 定义Job类
package com.yang.springbootquartz.quartz.job;

import lombok.extern.slf4j.Slf4j;
import org.quartz.Job;
import org.quartz.JobExecutionContext;
import org.springframework.stereotype.Component;

import java.util.Date;

/**
 * @author: Yang
 * @date: 2018/9/12 22:41
 * @description:
 */
@Slf4j
@Component
public class NativeJob implements Job {

    @Override
    public void execute(JobExecutionContext jobExecutionContext) {
        log.info(new Date() + "------------->");
    }

}

  1. 调用
package com.yang.springbootquartz.quartz.mynative;

import org.quartz.*;
import org.quartz.impl.StdSchedulerFactory;

/**
 * @author: Yang
 * @date: 2018/9/12 22:47
 * @description:
 */
public class NativeQuartz {

    public static void main(String[] args) throws SchedulerException {
        //1.创建job对象
        JobDetail job = JobBuilder.newJob(NativeJob.class).build();

        /**2.创建trigger对象(简单型)**/
        //Trigger trigger = TriggerBuilder.newTrigger().withSchedule(SimpleScheduleBuilder.repeatSecondlyForever(5)).build();  //简单型
        Trigger trigger = TriggerBuilder.newTrigger().withSchedule(CronScheduleBuilder.cronSchedule("0/5 * * * * ?")).build();  //cron型

        //3.创建scheduler
        Scheduler scheduler = StdSchedulerFactory.getDefaultScheduler();

        //4.装载job和trigger
        scheduler.scheduleJob(job, trigger);

        //5.启动调度
        scheduler.start();
    }
}
  1. 执行main方法即可看到定时任务被调用,打印出:Sun Aug 09 22:55:25 CST 2018------------->

此Demo主要用于理解Quartz机制及核心三大件

你可能感兴趣的:(原生Java集成Quartz)