Job實現類
package quartz.helloQuartz;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import org.quartz.Job;
import org.quartz.JobDetail;
import org.quartz.JobExecutionContext;
import org.quartz.JobExecutionException;
import org.quartz.Scheduler;
import org.quartz.Trigger;
/**
* Created by penelope on 2017/7/3.
*/
public class FirstJob implements Job {
private String jobMsg;
private String trMsg;
private Double trDouble;
public String getJobMsg() {
return jobMsg;
}
public void setJobMsg(String jobMsg) {
this.jobMsg = jobMsg;
}
public String getTrMsg() {
return trMsg;
}
public void setTrMsg(String trMsg) {
this.trMsg = trMsg;
}
public Double getTrDouble() {
return trDouble;
}
public void setTrDouble(Double trDouble) {
this.trDouble = trDouble;
}
public void execute(JobExecutionContext jobExecutionContext) throws JobExecutionException {
Calendar calendar = java.util.Calendar.getInstance();
SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
System.out.println("Current time is: " + dateFormat.format(calendar.getTime()));
/**
* 當 Job 觸發器觸發時(在某個時刻),execute(..)就被 scheduler所調用。
* JobExecutionContext 對象被傳遞給這個方法,它爲 Job 實例提供了它的「運行時」環境:
* 1, 一個指向執行這個 Job 實例的 Scheduler 句柄,
* 2, 一個指向觸發該次執行的觸發器的句柄,
* 3, Job 的 JobDetail 對象以及一些其餘的條目。
*/
// 從jobExecutionContext中獲取Scheduler.
@SuppressWarnings("unused")
Scheduler scheduler = jobExecutionContext.getScheduler();
//從jobExecutionContext中獲取JobDetail和Trigger
JobDetail jobDetail = jobExecutionContext.getJobDetail();
System.out.println("job name and group are: " + jobDetail.getKey().getName() + ": " + jobDetail.getKey().getGroup());
Trigger trigger = jobExecutionContext.getTrigger();
System.out.println("trigger name and group are:" + trigger.getKey().getName() + ":" + trigger.getKey().getGroup());
/**
* 方法一:從JobDataMap中獲取JobDetail和Trigger的data
*/
/*
JobDataMap jobDataMap = jobExecutionContext.getJobDetail().getJobDataMap();
JobDataMap trDataMap = jobExecutionContext.getTrigger().getJobDataMap();
String jobMsg = jobDataMap.getString("jobMsg");
String trMsg = trDataMap.getString("trMsg");
Double tDouble = trDataMap.getDouble("trDouble");
System.out.println("jobMsg------- : " + jobMsg );
System.out.println("trMsg------- : " + trMsg );
System.out.println("tDouble------- : " + trDouble );
*/
/**
* 方法二:Job類中添加JobDataMap中對應的屬性和setter方法。Job類中的屬性名稱要和JobDataMap中的key值一致。
*/
// JobDataMap jobDataMap = jobExecutionContext.getMergedJobDataMap();
System.out.println("jobMsg------- : " + jobMsg );
System.out.println("trMsg------- : " + trMsg );
System.out.println("trDouble------- : " + trDouble );
System.out.println("==========================================================");
}
}
Quartz客戶端代碼:
package quartz.helloQuartz;
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;
/**
* Created by penelope on 2017/7/3.
*/
public class FirstScheduler {
public static void main(String[] args) throws SchedulerException {
JobDetail jobDetail = JobBuilder.newJob(FirstJob.class)//獲取JobDetail實例,並綁定FisrtJob類
.withIdentity("myJob", "group1")//定義Job的名稱和組別
.usingJobData("jobMsg", "this is jobDetail message.")//定義jobDataMap中的數據,傳遞給Job實現類
.build();
System.out.println("jobDetail's name: " + jobDetail.getKey().getName());
System.out.println("jobDetail's group:" + jobDetail.getKey().getGroup());
System.out.println("jobDetail's jobClass : " + jobDetail.getJobClass().getName());
// 建立一個Trigger實例,定義該Job當即執行,而且每隔2秒重複執行
Trigger trigger = TriggerBuilder.newTrigger()//過去trigger實例
.withIdentity("myTrigger", "group1")//定義trigger的名稱和組別
.usingJobData("trMsg", "this is trigger message.")//定義jobDataMap中的數據,傳遞給Job實現類
.usingJobData("trDouble", 3.0)//定義jobDataMap中的數據,傳遞給Job實現類
.startNow()
.withSchedule(SimpleScheduleBuilder
.simpleSchedule()
.withIntervalInSeconds(2)
.repeatForever())
.build();
// 建立Scheduler實例
SchedulerFactory schedulerFactory = new StdSchedulerFactory();
Scheduler scheduler = schedulerFactory.getScheduler();
scheduler.start();
// 執行
scheduler.scheduleJob(jobDetail, trigger);//將JobDetail實例傳遞給scheduler,實際上是把job和trigger告知scheduler.
// scheduler.shutdown();
}
}