一、Job接口:實現業務邏輯的任務接口,execute方法中實現具體邏輯(相似與TimerTask的run方法),html
1.1 Job實例在Quartz中的生命週期:java
每次調度器執行Job時,它在調用execute方法前會建立一個新的job實例,當調用完成後,關聯的job對象實例被釋放,釋放的實例會被垃圾回收機制回收。框架
二、JobDetail(綁定job,又攜帶須要使用的屬性):爲Job實例提供了許多設置屬性,以及JobDataMap成員變量屬性,他用來存儲特定Job實例的狀態信息,調度器須要藉助JobDetail對象來添加Job實例。函數
2.1 重要屬性:name/group/jobClass/jobDataMapui
一、JobExecutionContext:當scheduler調用一個Job,就會將JobExecutionContext傳遞給Job的execute()方法;Job能經過JobExecutionContext對象訪問到Quartz運行時候的環境以及job自己的明細數據。this
二、JobDataMap:spa
三、獲取JobdataMap的兩種方式:插件
A--數據賦值:鏈式寫法:線程
JobDetail jobDetail = JobBuilder .newJob(HelloJob.class) .withIdentity("myJob", "group1") .usingJobData("param", "myTestJob") .usingJobData("jobDetailValue",3.14F) .build(); Trigger trigger = TriggerBuilder .newTrigger() .withIdentity("myTrigger", "group1") .startNow(). usingJobData("param","myTestTrigger"). usingJobData("TriggerValue",3.1D) .withSchedule( SimpleScheduleBuilder.simpleSchedule().withIntervalInSeconds(2).repeatForever()) .build();
B--獲取數據:code
方法一: 從Map中直接獲取:
(1)arg0.getTrigger().getJobDataMap()和arg0.getJobDetail().getJobDataMap();
public void execute(JobExecutionContext arg0) throws JobExecutionException { //打印當前的執行時間 Date date = new Date(); SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss"); System.out.println("Current Exec Time is:"+sdf.format(date)); //編寫具體的業務邏輯 //A經過Map-取數方法1 JobKey jobKey=arg0.getJobDetail().getKey(); TriggerKey triggerKey = arg0.getTrigger().getKey(); JobDataMap jobDataMap =arg0.getJobDetail().getJobDataMap(); JobDataMap triggerDataMap = arg0.getTrigger().getJobDataMap(); System.out.println("Job name:"+jobKey.getName()+"-Job Group:"+jobKey.getGroup()); System.out.println("Trigger name:"+triggerKey.getName()+"-Trigger Group:"+triggerKey.getGroup()); System.out.println("Jobparam:"+jobDataMap.getString("param")); System.out.println("jobDetailValue:"+jobDataMap.getFloat("jobDetailValue")); System.out.println("triggerparam:"+triggerDataMap.getString("param")); System.out.println("TriggerValue:"+triggerDataMap.getDouble("TriggerValue")); }
(2)getMergedJobDataMap()方法(key相同時,Trigger優先於JobDetail)
JobDataMap DataMap = arg0.getMergedJobDataMap(); System.out.println("param:"+DataMap.getString("param"));
方法二:Job實現類中添加setter方法對應JobDataMap的鍵值(Quartz框架默認的JobFactory實現類在初始化job實例對象時會自動調用這些setter方法)
添加同名私有變量,寫setter方法
public class HelloJob implements Job{ private String param; private Double TriggerValue; private Float jobDetailValue; public void setParam(String param) { this.param = param; } public void setTriggerValue(Double triggerValue) { TriggerValue = triggerValue; } public void setJobDetailValue(Float jobDetailValue) { this.jobDetailValue = jobDetailValue; } }
利用實例化時的反射機制,settet()自動賦值
//經過setter方法獲取數據 System.out.println("param:"+param); System.out.println("jobDetailValue:"+TriggerValue); System.out.println("TriggerValue:"+jobDetailValue);
運行結果:
Current Exec Time is:2018-01-18 20:13:02 param:myTestTrigger jobDetailValue:3.1 TriggerValue:3.14 Current Exec Time is:2018-01-18 20:13:03 param:myTestTrigger jobDetailValue:3.1 TriggerValue:3.14
SimpleTrigger:在一個指定的時間內執行一次做業任務,或在指定時間間隔屢次執行任務
.withShedule( SimpleScheduleBuilder.simpleSchedule().withIntervalInSeconds() )
CornTrigger:(經常使用)基於日曆的做業調度器
.withShedule( CronScheduleBuilder.cronSchedule("* * * * * ? *")//cron表達式 )
cron表達式:https://www.cnblogs.com/sunjie9606/archive/2012/03/15/2397626.html
//StdSchedulerFactory工廠類(經常使用) SchedulerFactory sfact = new StdSchedulerFactory(); Scheduler scheduler = sfact.getScheduler(); //DirectSchedulerFactory工廠類 DirectSchedulerFactory factory= DirectSchedulerFactory.getInstance(); Scheduler scheduler = factory.getScheduler();
StdShedulerFactory:
Sheduler的主要函數:
Thread.sleep(2000L); scheduler.standby();void shutdown(Boolean boolean) // true表示等待全部正在執行的Job執行完畢以後再關閉,false表示直接關閉 ,也可空。
文檔的位置和加載順序:優先讀取用戶自定義的quartz.properties,若無則讀取Jar包裏面的配置文件
組成部分:調度器屬性/線程池屬性/做業存儲設置/插件配置
調度器屬性:
線程池屬性:
做業存儲設置:Job和Trigger信息是如何被存儲的
插件配置