淺談JobExecutionContext與JobDataMap
1.JobExecutionContext簡介
(1)當Scheduler調用一個Job,就會將JobExecutionContext傳遞給job的execute方法
quartz無法調用job的有參構造函數,所以創建job的實例的時候是運用反射機制,通過newInstance創建實例,並且通過JobDetail描述的name與group屬性然後給Job設置一些屬性。
(2)Job能通過JobExecutionContext對象訪問到Quartz運行時候的環境以及Job本身的明細數據。
2.JobDataMap簡介:
1.JobDataMap是什麽?
(1)在進行任務調度時JobDataMap存儲在JobExecutionContext
(2)JobDataMap可以用來裝載任何可以序列化的數據對象,當job實例對象被執行時這些參數對象會傳遞給它
(3)JobDataMap實現了JDK的Map接口,並且添加了一些非常方便的方法用來存取數據基本數據類型。
2.獲取JobDataMap的兩種方式:
- 第一種方式:從JobExecutionContext實例中獲取
(1)從實例中獲取JobDetail與Trigger單獨的JobDataMap的方法
例如:
HelloJob.java(JobExecutionContext獲取JobDataMap以及獲取JobDetail和Trigger的標識信息)
package cn.qlq.quartz; import java.text.SimpleDateFormat; import java.util.Date; import org.quartz.Job; import org.quartz.JobDataMap; import org.quartz.JobExecutionContext; import org.quartz.JobExecutionException; import org.quartz.JobKey; import org.quartz.TriggerKey; public classHelloJob implements Job { public void execute(JobExecutionContext context) throws JobExecutionException { //打印當前的時間 Date date = new Date(); SimpleDateFormat sf = new SimpleDateFormat("yyyy-MM-dd hh:mm:ss"); System.out.println("current time is:"+sf.format(date)); // 編寫具體的業務邏輯 JobKey key = context.getJobDetail().getKey();//獲取JobDetail的標識信息 System.out.println("JobDetail‘s name and group are "+key.getName()+":"+key.getGroup()); TriggerKey triggerKey = context.getTrigger().getKey();//獲取Trigger的標識信息 System.out.println("Trigger‘s name and group are "+triggerKey.getName()+":"+triggerKey.getGroup()); //獲取JobDetail通過JobDataMap傳遞的參數信息 JobDataMap jobDataMap = context.getJobDetail().getJobDataMap(); String jobMessage = jobDataMap.getString("message"); Float floatValue = jobDataMap.getFloat("floatValue"); System.out.println(jobMessage+"\t"+floatValue); //獲取Trigger通過JobDataMap傳遞的信息 JobDataMap triggerDataMap = context.getTrigger().getJobDataMap(); String triggerMessage = triggerDataMap.getString("message"); Double DoubleValue = triggerDataMap.getDouble("DoubleValue"); System.out.println(triggerMessage+"\t"+DoubleValue); } }
HelloScheduler.java 通過usingJobData傳遞一些參數(JobDetail和Trigger都能使用此方法傳遞參數)
package cn.qlq.quartz; import static org.quartz.DateBuilder.newDate; import static org.quartz.JobBuilder.newJob; import static org.quartz.SimpleScheduleBuilder.simpleSchedule; import static org.quartz.TriggerBuilder.newTrigger; import java.util.GregorianCalendar; import org.quartz.JobDetail; import org.quartz.Scheduler; import org.quartz.SimpleScheduleBuilder; import org.quartz.Trigger; import org.quartz.TriggerBuilder; import org.quartz.impl.StdSchedulerFactory; import org.quartz.impl.calendar.AnnualCalendar; public class HelloScheduler { public static void main(String[] args) { try { //1. 創建一個JodDetail實例 將該實例與Hello job class綁定 (鏈式寫法) JobDetail jobDetail = newJob(HelloJob.class) // 定義Job類為HelloQuartz類,這是真正的執行邏輯所在 .withIdentity("myJob") // 定義name/group .usingJobData("message","Hello myjob1")//使用JobDataMap傳遞參數給Job,傳遞一個string類型參數 .usingJobData("floatValue",8.5f)//使用JobDataMap傳遞參數給Job,傳遞一個float類型 .build(); //2. 定義一個Trigger,定義該job立即執行,並且每兩秒鐘執行一次,直到永遠 Trigger trigger = TriggerBuilder.newTrigger().withIdentity("myTrigger", "group1")// 定義名字和組 .startNow()//現在開始 .withSchedule(SimpleScheduleBuilder.simpleSchedule().withIntervalInSeconds(2).repeatForever()) .usingJobData("message","Hello trigger1") .usingJobData("DoubleValue",2.5D) .build(); //3. 創建scheduler Scheduler scheduler = StdSchedulerFactory.getDefaultScheduler(); //4. 將trigger和jobdetail加入這個調度 scheduler.scheduleJob(jobDetail, trigger); //5. 啟動scheduler scheduler.start(); } catch (Exception e) { e.printStackTrace(); } } }
結果:
current time is:2018-04-04 09:53:15
JobDetail‘s name and group are myJob:DEFAULT
Trigger‘s name and group are myTrigger:group1
Hello myjob1 8.5
Hello trigger1 2.5
(2)從實例中獲取JobDetail與Trigger合並的JobDataMap的方法
理論上是將JobDatail的JobDataMap和Trigger的JobDataMap合並,當我們獲取兩者有相同的key值的時候,優先獲取的是Trigger的值,也就是JobDatail的會被覆蓋
例如:
HelloJob.java(獲取兩者合並後的JobDataMap)
package cn.qlq.quartz; import java.text.SimpleDateFormat; import java.util.Date; import org.quartz.Job; import org.quartz.JobDataMap; import org.quartz.JobExecutionContext; import org.quartz.JobExecutionException; import org.quartz.JobKey; import org.quartz.TriggerKey; public class HelloJob implements Job { public void execute(JobExecutionContext context) throws JobExecutionException { //打印當前的時間 Date date = new Date(); SimpleDateFormat sf = new SimpleDateFormat("yyyy-MM-dd hh:mm:ss"); System.out.println("current time is:"+sf.format(date)); // 編寫具體的業務邏輯 JobKey key = context.getJobDetail().getKey();//獲取JobDetail的標識信息 System.out.println("JobDetail‘s name and group are "+key.getName()+":"+key.getGroup()); TriggerKey triggerKey = context.getTrigger().getKey();//獲取Trigger的標識信息 System.out.println("Trigger‘s name and group are "+triggerKey.getName()+":"+triggerKey.getGroup()); //獲取JobDetail通過JobDataMap傳遞的參數信息(獲取兩者合並後的JobDataMap) JobDataMap jobDataMap = context.getMergedJobDataMap(); String message = jobDataMap.getString("message"); Float floatValue = jobDataMap.getFloat("floatValue"); System.out.println("message is :"+message); System.out.println("floatValue is :"+floatValue); Double DoubleValue = jobDataMap.getDouble("DoubleValue"); System.out.println("DoubleValue is :"+DoubleValue); } }
HelloScheduler.java
package cn.qlq.quartz; import static org.quartz.DateBuilder.newDate; import static org.quartz.JobBuilder.newJob; import static org.quartz.SimpleScheduleBuilder.simpleSchedule; import static org.quartz.TriggerBuilder.newTrigger; import java.util.GregorianCalendar; import org.quartz.JobDetail; import org.quartz.Scheduler; import org.quartz.SimpleScheduleBuilder; import org.quartz.Trigger; import org.quartz.TriggerBuilder; import org.quartz.impl.StdSchedulerFactory; import org.quartz.impl.calendar.AnnualCalendar; public class HelloScheduler { public static void main(String[] args) { try { //1. 創建一個JodDetail實例 將該實例與Hello job class綁定 (鏈式寫法) JobDetail jobDetail = newJob(HelloJob.class) // 定義Job類為HelloQuartz類,這是真正的執行邏輯所在 .withIdentity("myJob") // 定義name/group .usingJobData("message","Hello myjob1")//使用JobDataMap傳遞參數給Job,傳遞一個string類型參數 .usingJobData("floatValue",8.5f)//使用JobDataMap傳遞參數給Job,傳遞一個float類型 .build(); //2. 定義一個Trigger,定義該job立即執行,並且每兩秒鐘執行一次,直到永遠 Trigger trigger = TriggerBuilder.newTrigger().withIdentity("myTrigger", "group1")// 定義名字和組 .startNow()//現在開始 .withSchedule(SimpleScheduleBuilder.simpleSchedule().withIntervalInSeconds(2).repeatForever()) .usingJobData("message","Hello trigger1") .usingJobData("DoubleValue",2.5D) .build(); //3. 創建scheduler Scheduler scheduler = StdSchedulerFactory.getDefaultScheduler(); //4. 將trigger和jobdetail加入這個調度 scheduler.scheduleJob(jobDetail, trigger); //5. 啟動scheduler scheduler.start(); } catch (Exception e) { e.printStackTrace(); } } }
運行結果:(如果JobDetail和Trigger的傳遞參數相同,使用合並後的JobDataMap獲取的時候優先獲取Trigger的參數)
- 第二種方式:Job實現類添加setter方法對應JobDataMap的鍵值(Quartz框架默認的JobFactory實現類在初始化job實例對象時會自動調用這些setter方法)
例如:
HelloJob.java(添加成員屬性,並且添加對應的get,set方法,類似於struts的屬性註入,當我們在使用JobDataMap添加數據的時候會自動映射到這個類的成員實現方法中,需要註意的是成員屬性的名字要和jobDataMap添加時候的key值相同)
package cn.qlq.quartz; import java.text.SimpleDateFormat; import java.util.Date; import org.quartz.Job; import org.quartz.JobDataMap; import org.quartz.JobExecutionContext; import org.quartz.JobExecutionException; import org.quartz.JobKey; import org.quartz.TriggerKey; public class HelloJob implements Job { private String message; private Float floatValue; private Double DoubleValue; public void execute(JobExecutionContext context) throws JobExecutionException { //打印當前的時間 Date date = new Date(); SimpleDateFormat sf = new SimpleDateFormat("yyyy-MM-dd hh:mm:ss"); System.out.println("current time is:"+sf.format(date)); // 編寫具體的業務邏輯 JobKey key = context.getJobDetail().getKey();//獲取JobDetail的標識信息 System.out.println("JobDetail‘s name and group are "+key.getName()+":"+key.getGroup()); TriggerKey triggerKey = context.getTrigger().getKey();//獲取Trigger的標識信息 System.out.println("Trigger‘s name and group are "+triggerKey.getName()+":"+triggerKey.getGroup()); System.out.println("message is :"+message); System.out.println("floatValue is :"+floatValue); System.out.println("DoubleValue is :"+DoubleValue); } public String getMessage() { return message; } public void setMessage(String message) { this.message = message; } public Float getFloatValue() { return floatValue; } public void setFloatValue(Float floatValue) { this.floatValue = floatValue; } public Double getDoubleValue() { return DoubleValue; } public void setDoubleValue(Double doubleValue) { DoubleValue = doubleValue; } }
HelloScheduler.java
package cn.qlq.quartz; import static org.quartz.DateBuilder.newDate; import static org.quartz.JobBuilder.newJob; import static org.quartz.SimpleScheduleBuilder.simpleSchedule; import static org.quartz.TriggerBuilder.newTrigger; import java.util.GregorianCalendar; import org.quartz.JobDetail; import org.quartz.Scheduler; import org.quartz.SimpleScheduleBuilder; import org.quartz.Trigger; import org.quartz.TriggerBuilder; import org.quartz.impl.StdSchedulerFactory; import org.quartz.impl.calendar.AnnualCalendar; public class HelloScheduler { public static void main(String[] args) { try { //1. 創建一個JodDetail實例 將該實例與Hello job class綁定 (鏈式寫法) JobDetail jobDetail = newJob(HelloJob.class) // 定義Job類為HelloQuartz類,這是真正的執行邏輯所在 .withIdentity("myJob") // 定義name/group .usingJobData("message","Hello myjob1")//使用JobDataMap傳遞參數給Job,傳遞一個string類型參數 .usingJobData("floatValue",8.5f)//使用JobDataMap傳遞參數給Job,傳遞一個float類型 .build(); //2. 定義一個Trigger,定義該job立即執行,並且每兩秒鐘執行一次,直到永遠 Trigger trigger = TriggerBuilder.newTrigger().withIdentity("myTrigger", "group1")// 定義名字和組 .startNow()//現在開始 .withSchedule(SimpleScheduleBuilder.simpleSchedule().withIntervalInSeconds(2).repeatForever()) .usingJobData("message","Hello trigger1") .usingJobData("DoubleValue",2.5D) .build(); //3. 創建scheduler Scheduler scheduler = StdSchedulerFactory.getDefaultScheduler(); //4. 將trigger和jobdetail加入這個調度 scheduler.scheduleJob(jobDetail, trigger); //5. 啟動scheduler scheduler.start(); } catch (Exception e) { e.printStackTrace(); } } }
結果:
current time is:2018-04-04 10:11:29
JobDetail‘s name and group are myJob:DEFAULT
Trigger‘s name and group are myTrigger:group1
message is :Hello trigger1
floatValue is :8.5
DoubleValue is :2.5
淺談JobExecutionContext與JobDataMap