Quartz
Quartz
是一個功能滿完整的 Java Schedule 排程工具, 核心就圍繞在 Scheduler 與 Job 的操作上。
- Scheduler - the main API for interacting with the scheduler.
- Job - an interface to be implemented by components that you wish to have executed by the scheduler.
- JobDetail - used to define instances of Jobs.
- Trigger - a component that defines the schedule upon which a given Job will be executed.
- JobBuilder - used to define/build JobDetail instances, which define instances of Jobs.
- TriggerBuilder - used to define/build Trigger instances.
Job
Quartz 的 Job
被定義為需要 implement org.quartz.Job
,
需要實作 void execute(JobExecutionContext jobExecutionContext)
,
jobExecutionContext, 可以取得 Job 的 Scheduler, Trigger, JobDetail 的相關設定, 相關的說明可以參考 tutorial-lesson-02.
import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.ObjectMapper;
import org.quartz.Job;
import org.quartz.JobDataMap;
import org.quartz.JobDetail;
import org.quartz.JobExecutionContext;
import org.quartz.JobExecutionException;
/**
* Created by jerry on 2017/11/17.
*/
public class HelloJob implements Job {
/**
* Quartz Job 要求要一個 empty constructor,
* 讓 Scheduler 來 instantiate
*/
public HelloJob() {
}
/**
* Quartz Job 要執行的動作
*
* @param jobExecutionContext
* @throws JobExecutionException
*/
@Override
public void execute(JobExecutionContext jobExecutionContext) throws JobExecutionException {
ObjectMapper objectMapper = new ObjectMapper();
JobDetail jobInstant = jobExecutionContext.getJobDetail();
System.out.println("===========================================");
System.out.println("Description: " + jobInstant.getDescription());
System.out.println("Job Key: " + jobInstant.getKey());
System.out.println("is Concurrent Execution Disallowed: " + jobInstant.isConcurrentExectionDisallowed());
System.out.println("is Durable: " + jobInstant.isDurable());
System.out.println("requests Recovery: " + jobInstant.requestsRecovery());
System.out.println("===========================================");
JobDataMap jobData = jobInstant.getJobDataMap();
try {
String jobDataJson = objectMapper.writeValueAsString(jobData);
System.out.println("Job Data Map Data: " + jobDataJson);
} catch (JsonProcessingException e) {
e.printStackTrace();
}
}
}
Scheduler
定義完 Job 之後, 要觸發還需要 Scheduler
, Scheduler 可以用來分配 Job 與 Trigger,
藉由 SchedulerFactory
來取得 instance, SchedulerFactory 會載入 quartz.properties
定義的相關設定(參考)
import static org.quartz.DateBuilder.evenMinuteDate;
import static org.quartz.JobBuilder.newJob;
import static org.quartz.TriggerBuilder.newTrigger;
import com.google.common.collect.ImmutableMap;
import org.quartz.JobDataMap;
import org.quartz.JobDetail;
import org.quartz.Scheduler;
import org.quartz.SchedulerException;
import org.quartz.SchedulerFactory;
import org.quartz.Trigger;
import org.quartz.impl.StdSchedulerFactory;
import java.util.Date;
/**
* Created by jerry on 2017/11/17.
*/
public class HelloWorldSchedule {
public void run() throws SchedulerException, InterruptedException {
// init job
JobDetail job = newJob(HelloJob.class)
.withDescription("quartz job - hello world")
.withIdentity("job-name", "job-group")
.usingJobData("string-key", "value1")
.usingJobData("long-key", 1L)
.usingJobData(new JobDataMap(
ImmutableMap.builder().put("map-key", "map-value").build()))
.build();
// set trigger time
Date runTime = evenMinuteDate(new Date());
// init trigger, start at next even minute time
Trigger trigger = newTrigger()
.withIdentity("trigger-name", "trigger-group")
.startAt(runTime)
.build();
// init schedule
SchedulerFactory schedulerFactory = new StdSchedulerFactory();
Scheduler scheduler = schedulerFactory.getScheduler();
scheduler.scheduleJob(job, trigger);
// start up schedule
scheduler.start();
// sleep 10 seconds, make sure the scheduler be triggered
Thread.sleep(60L * 1000);
scheduler.shutdown(true);
}
}
Run Scheduler
@Test
public void testScheduler() throws SchedulerException, InterruptedException {
HelloWorldSchedule helloWorldSchedule = new HelloWorldSchedule();
helloWorldSchedule.run();
}
沒有留言:
張貼留言