简介
完全由java编写的开源作业调度框架
Quartz有两大触发器:SimpleTrigger和CronTrigger
使用CronTrigger要掌握Cron表达式
"30 * * * * ?" #每半分钟触发任务
"30 10 * * * ?" #每小时的10分30秒触发任务
"30 10 1 * * ?" #每天1点10分30秒触发任务
"30 10 1 20 * ?" #每月20号1点10分30秒触发任务
.......
Maven
<dependency>
<groupId>org.quartz-scheduler</groupId>
<artifactId>quartz</artifactId>
<version>2.2.3</version>
</dependency>
<dependency>
<groupId>org.quartz-scheduler</groupId>
<artifactId>quartz-jobs</artifactId>
<version>2.2.3</version>
</dependency>
quartz.properties
org.quartz.scheduler.instanceName = MyScheduler
org.quartz.threadPool.threadCount = 3
org.quartz.jobStore.class = org.quartz.simpl.RAMJobStore
HelloJob.java
public class HelloJob implements Job {
public void execute(JobExecutionContext arg0) throws JobExecutionException {
System.out.println("HelloJob...."+new Date());
}
}
QuartzTest
public static void main(String[] args) throws InterruptedException {
Scheduler scheduler;
try {
scheduler = StdSchedulerFactory.getDefaultScheduler();
System.out.println("quartz start....");
scheduler.start();
/**********************doWork() start**************/
//每五秒执行一次
JobDetail jobDetail = JobBuilder.newJob(HelloJob.class).withIdentity("job1","group1").build();
SimpleScheduleBuilder simpleScheduleBuilder = SimpleScheduleBuilder.simpleSchedule().withIntervalInSeconds(5).repeatForever();
Trigger trigger = TriggerBuilder.newTrigger().withIdentity("trigger1", "group1").startNow().withSchedule(simpleScheduleBuilder).build();
scheduler.scheduleJob(jobDetail, trigger);
TimeUnit.MINUTES.sleep(1);
/**********************doWork() end**************/
System.out.println("quartz shutdown....");
scheduler.shutdown();
} catch (SchedulerException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
参考
官网quick-start
官网cookbook
【Quartz】Quartz的搭建、应用(单独使用Quartz)
Quartz-v1.8.6
Quartz cron表达式格式