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