1. 程式人生 > >quartz定時任務_演示版01

quartz定時任務_演示版01

print 核心 日程表 鏈式 定時 bst 模式 new hid

預置:

quartz共4個設計模式:Builder模式,Factory模式,組件模式,鏈式模式

體系結果:核心3個類JobDetail , trigger , scheduler

重要組成部分:Job接口、JobBuilder、JobDetail、JobStore

       TriggerBuilder、Trigger

Scheduler

接聽器:JobListener、TriggerListener、SchedulerListener

ThreadPool (線程池,解決並發問題)

Calendar (排除日程表中某些時間點)

=====================================================================

1.新建maven項目

2.在http://www.mvnrepository.com/中找到quartz的jar包依賴代碼引入pom.xml文件

<dependency>
<groupId>org.quartz-scheduler</groupId>
<artifactId>quartz</artifactId>
<version>2.2.1</version>
</dependency>

3.實現job接口的execute方法:

import java.text.SimpleDateFormat;
import java.util.Date;
import org.quartz.Job;
import org.quartz.JobExecutionContext;
import org.quartz.JobExecutionException;

public class helloJob implements Job {
//實現job接口execute方法具體內容
public void execute(JobExecutionContext arg0) throws JobExecutionException{
//打印當前執行時間 格式:2017-01-01 00:00:00
Date date = new Date();
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
System.out.println("current exec time is :"+sdf.format(date));

//編寫具體業務邏輯
System.out.println("helloJob類 實現了 Job接口的execute方法");
}
}

4.main方法中實現任務的創建運行

import java.text.SimpleDateFormat;
import java.util.Date;
import org.quartz.JobBuilder;
import org.quartz.JobDetail;
import org.quartz.Scheduler;
import org.quartz.SchedulerException;
import org.quartz.SchedulerFactory;
import org.quartz.SimpleScheduleBuilder;
import org.quartz.Trigger;
import org.quartz.TriggerBuilder;
import org.quartz.impl.StdSchedulerFactory;

/*
* 20180103 ver1.0
*/
public class helloSchedule {

public static void main(String[] args) throws SchedulerException {
// TODO Auto-generated method stub

// 創建一個JobDetail實例 將HelloJob類添加到JobDetail中
JobDetail jobDetail = JobBuilder.newJob(helloJob.class)
.withIdentity("myJob", "jobDetailGroup")
.build();
//創建一個Trigger實例,定義該job立即執行,每隔兩秒鐘重復執行一次,一直不停
Trigger trigger = TriggerBuilder.newTrigger()
.withIdentity("myTrigger","triggerGroup")
.startNow().withSchedule(
SimpleScheduleBuilder.simpleSchedule()
.withIntervalInSeconds(2)
.repeatForever())
.build();
//創建Schduler實例
SchedulerFactory sfFactory = new StdSchedulerFactory();
Scheduler scheduler = sfFactory.getScheduler();
scheduler.start();

//打印當前時間
Date date = new Date();
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
System.out.println("current scheduler time is :"+sdf.format(date));
scheduler.scheduleJob(jobDetail, trigger);

}

}

5.運行結果:

current scheduler time is :2018-01-03 15:08:54
current exec time is :2018-01-03 15:08:54
helloJob類 實現了 Job接口的execute方法
current exec time is :2018-01-03 15:08:56
helloJob類 實現了 Job接口的execute方法
current exec time is :2018-01-03 15:08:58
helloJob類 實現了 Job接口的execute方法
current exec time is :2018-01-03 15:09:00
helloJob類 實現了 Job接口的execute方法
current exec time is :2018-01-03 15:09:02
helloJob類 實現了 Job接口的execute方法
current exec time is :2018-01-03 15:09:04
helloJob類 實現了 Job接口的execute方法
current exec time is :2018-01-03 15:09:06
helloJob類 實現了 Job接口的execute方法

quartz定時任務_演示版01