Quartz是一个彻底由java编写的开源做业调度框架,说人话就是你能够建立一些任务,规定这些任务何时执行、执行几回等。本文记录项目过程当中Quartz的经常使用方法。
官方下载地址 http://www.quartz-scheduler.org/downloads/
官网比较慢,能够在CSDN下载 https://download.csdn.net/download/leytton/10346005
经测试只须要引入quartz-2.2.3.jar、quartz-jobs-2.2.3.jar和slf4j-api-1.7.7.jar就好了
或者使用Maven:css
<dependency>
<groupId>org.quartz-scheduler</groupId>
<artifactId>quartz</artifactId>
<version>2.2.1</version>
</dependency>
<dependency>
<groupId>org.quartz-scheduler</groupId>
<artifactId>quartz-jobs</artifactId>
<version>2.2.1</version>
</dependency>
参考http://www.javashuo.com/article/p-rlnohofy-h.htmlhtml
JobHelloQuartz.java
java
public class JobHelloQuartz implements Job {
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd hh:mm:ss");
public void execute(JobExecutionContext context) throws JobExecutionException {
JobDetail detail = context.getJobDetail();
String name = detail.getJobDataMap().getString("name");
System.out.println(detail.getKey()+" say hello to " + name + " at " + sdf.format(new Date()));
}
}
Test01.java
api
public class Test01 {
public static void main(String[] args) {
try {
// 建立scheduler
Scheduler scheduler = StdSchedulerFactory.getDefaultScheduler();
// 定义一个Trigger
Trigger trigger = TriggerBuilder.newTrigger().withIdentity("Trigger_HelloQuartz", "group1") // 定义name/group
// .startNow()//一旦加入scheduler,当即执行
.startAt(new Date(System.currentTimeMillis() + 5000))// 5秒后执行
.withSchedule(SimpleScheduleBuilder.simpleSchedule() // 使用SimpleTrigger
.withIntervalInSeconds(2) // 每隔2秒执行一次
.withRepeatCount(1))// 重复1次,共计2次
// .repeatForever()) //一直执行,奔腾到老不停歇
.build();
// 定义一个JobDetail
JobDetail job = JobBuilder.newJob(JobHelloQuartz.class) // 定义Job类为JobHelloQuartz
.withIdentity("helloJob", "group1") // 定义name/group
.usingJobData("name", "Leytton") // 定义属性
.build();
// 加入这个调度
scheduler.scheduleJob(job, trigger);
// 启动之
scheduler.start();
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd hh:mm:ss");
System.out.println("Quartz start at " + sdf.format(new Date()));
// 运行一段时间后关闭
// Thread.sleep(10000);
// scheduler.shutdown(true);
} catch (SchedulerException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
日志输出
markdown
Quartz start at 2018-04-13 11:38:26
group1.helloJob say hello to Leytton at 2018-04-13 11:38:31
group1.helloJob say hello to Leytton at 2018-04-13 11:38:33
若是定义.startAt(new Date(System.currentTimeMillis() - 5000))
app
// 5秒前执行,2秒执行一次,共执行2次,那么程序启动的时候会一次性执行完2次
Quartz start at 2018-04-13 11:50:08
group1.helloJob say hello to Leytton at 2018-04-13 11:50:08
group1.helloJob say hello to Leytton at 2018-04-13 11:50:08
如果4秒执行一次,那么程序启动的时候会执行2次
框架
.withIntervalInSeconds(4)
如果6秒执行一次,那么程序启动的时候会执行1次,1秒后再执行1次
测试
.withIntervalInSeconds(6)
Quartz start at 2018-04-13 11:53:21
group1.helloJob say hello to Leytton at 2018-04-13 11:53:21
group1.helloJob say hello to Leytton at 2018-04-13 11:53:22
这种状况应该是最多见的了,目前用到的定时任务绝大部分是这种类型的
如下为天天凌晨4点执行一次
ui
Scheduler scheduler = StdSchedulerFactory.getDefaultScheduler();
//天天4点执行BEGIN################################
Calendar cal = Calendar.getInstance();
cal.setTime(new Date());
cal.set(Calendar.HOUR_OF_DAY, 4);
cal.set(Calendar.MINUTE, 0);
cal.set(Calendar.SECOND, 0);
//若是当天时间已过,则安排次日开始,不然会当即执行一次
//if(System.currentTimeMillis()>cal.getTimeInMillis()) {
// cal.add(Calendar.DAY_OF_YEAR, 1);
//}
Date startTime=cal.getTime();
Trigger trigger_xx = TriggerBuilder.newTrigger().withIdentity("trigger_xx", "group1")
.startAt(startTime)
.withSchedule(CalendarIntervalScheduleBuilder.calendarIntervalSchedule()
.withIntervalInDays(1))
.build();
JobDetail job_xx = JobBuilder.newJob(Job_xx.class)
.withIdentity("job_xx", "job_group1")
.build();
scheduler.scheduleJob(job_xx, trigger_xx);
scheduler.start();
N天执行一次
.withIntervalInDays(N))
spa