package com.jordyn.quartz;
import org.quartz.*;
import org.quartz.impl.StdSchedulerFactory;
/**
* @description
* @date 2019-5-27
*/
public class HelloScheduler {
public static void main(String[] args) throws SchedulerException {
//创建一个jobDetail实例,将该实例与HelloJob Class绑定
JobDetail job = JobBuilder.newJob(HelloJob.class).withIdentity("myJob").build();
/*//创建一个Trigger触发器的实例,定义该job立即执行,并且每2秒执行一次,一直执行
SimpleTrigger trigger = TriggerBuilder.newTrigger().withIdentity("myTrigger").startNow()
.withSchedule(SimpleScheduleBuilder.simpleSchedule().withIntervalInSeconds(2).repeatForever()).build();
*/
//cronTrigger 每日9点40触发任务
CronTrigger cronTrigger = TriggerBuilder.newTrigger().withIdentity("cronTrigger").withSchedule(CronScheduleBuilder.cronSchedule("0 28 10 * * ?")).build();
//创建schedule实例
StdSchedulerFactory factory = new StdSchedulerFactory();
Scheduler scheduler = factory.getScheduler();
scheduler.start();
scheduler.scheduleJob(job,cronTrigger);
}
}
package com.jordyn.quartz;
import org.quartz.Job;
import org.quartz.JobExecutionContext;
import org.quartz.JobExecutionException;
import java.time.LocalDateTime;
/**
* @description
* @date 2019-5-27
*/
public class HelloJob implements Job{
public void execute(JobExecutionContext jobExecutionContext) throws JobExecutionException{
//打印当前的时间
System.out.println("现在时间是:" + LocalDateTime.now());
//具体的业务逻辑
System.out.println("开始生成任务报表");
}
}
<dependencies>
<dependency>
<groupId>org.quartz-scheduler</groupId>
<artifactId>quartz</artifactId>
<version>2.3.0</version>
</dependency>
</dependencies>
打开App,阅读手记