[toc]
1. Job介面
package com.niewj;
import com.google.gson.Gson;
import org.quartz.*;
public class MyJob implements Job {
Gson gson = new Gson();
@Override
public void execute(JobExecutionContext jobExecutionContext) throws JobExecutionException {
JobDetail jobDetail = jobExecutionContext.getJobDetail();
// 傳資料
JobKey jobKey = jobDetail.getKey();
String description = jobDetail.getDescription();
JobDataMap jobDataMap = jobDetail.getJobDataMap();
StringBuffer buff = new StringBuffer();
buff.append("jobKey=").append(gson.toJson(jobKey))
.append("\t description=").append(description)
.append("\t jobDataMap=").append(gson.toJson(jobDataMap));
System.out.println(buff);
}
}
2. main方法
package com.niewj;
import lombok.extern.slf4j.Slf4j;
import org.quartz.*;
import org.quartz.impl.StdSchedulerFactory;
@Slf4j
public class JobTest {
public static void main(String[] args) throws SchedulerException {
String cron = "*/5 * * * * ?";
String identity = "1";
String group = "g1";
String desc = "JobDetail任務";
// 傳給JobDetail的資料
JobDataMap map = new JobDataMap();
map.put("taskId", "1");
map.put("instanceId", "100");
// 1. 排程器
Scheduler scheduler = StdSchedulerFactory.getDefaultScheduler();
scheduler.clear();
scheduler.start();
log.info("=================任務排程:排程器已啟動===================");
// 2.定義觸發器
//定義name/group
Trigger trigger = TriggerBuilder.newTrigger().withIdentity(identity, group)
.startNow()
.withSchedule(CronScheduleBuilder.cronSchedule(cron))
.build();
// 3.定義一個任務JobDetail
//定義Job類為HelloJob類,這是真正的執行邏輯所在
JobDetail jobDetail = JobBuilder.newJob(MyJob.class)
//定義name/group
.withIdentity(identity, group)
.withDescription(desc)
.usingJobData(map)
.build();
// 4. 使用排程器排程JobDetail, 使用trigger觸發
scheduler.scheduleJob(jobDetail, trigger);
}
}
3. 輸出
15:47:10.393 [main] INFO org.quartz.impl.StdSchedulerFactory - Using default implementation for ThreadExecutor
15:47:10.405 [main] INFO org.quartz.simpl.SimpleThreadPool - Job execution threads will use class loader of thread: main
15:47:10.430 [main] INFO org.quartz.core.SchedulerSignalerImpl - Initialized Scheduler Signaller of type: class org.quartz.core.SchedulerSignalerImpl
15:47:10.430 [main] INFO org.quartz.core.QuartzScheduler - Quartz Scheduler v.2.3.2 created.
15:47:10.431 [main] INFO org.quartz.simpl.RAMJobStore - RAMJobStore initialized.
15:47:10.433 [main] INFO org.quartz.core.QuartzScheduler - Scheduler meta-data: Quartz Scheduler (v2.3.2) 'DefaultQuartzScheduler' with instanceId 'NON_CLUSTERED'
Scheduler class: 'org.quartz.core.QuartzScheduler' - running locally.
NOT STARTED.
Currently in standby mode.
Number of jobs executed: 0
Using thread pool 'org.quartz.simpl.SimpleThreadPool' - with 10 threads.
Using job-store 'org.quartz.simpl.RAMJobStore' - which does not support persistence. and is not clustered.
15:47:10.433 [main] INFO org.quartz.impl.StdSchedulerFactory - Quartz scheduler 'DefaultQuartzScheduler' initialized from default resource file in Quartz package: 'quartz.properties'
15:47:10.433 [main] INFO org.quartz.impl.StdSchedulerFactory - Quartz scheduler version: 2.3.2
15:47:10.434 [main] INFO org.quartz.core.QuartzScheduler - Scheduler DefaultQuartzScheduler_$_NON_CLUSTERED started.
15:47:10.434 [main] INFO com.niewj.JobTest - =================任務排程:排程器已啟動===================
15:47:10.434 [DefaultQuartzScheduler_QuartzSchedulerThread] DEBUG org.quartz.core.QuartzSchedulerThread - batch acquisition of 0 triggers
15:47:10.489 [DefaultQuartzScheduler_QuartzSchedulerThread] DEBUG org.quartz.core.QuartzSchedulerThread - batch acquisition of 1 triggers
15:47:10.493 [DefaultQuartzScheduler_QuartzSchedulerThread] DEBUG org.quartz.simpl.PropertySettingJobFactory - Producing instance of Job 'g1.1', class=com.niewj.MyJob
15:47:10.569 [DefaultQuartzScheduler_QuartzSchedulerThread] DEBUG org.quartz.core.QuartzSchedulerThread - batch acquisition of 1 triggers
15:47:10.569 [DefaultQuartzScheduler_Worker-1] DEBUG org.quartz.core.JobRunShell - Calling execute on job g1.1
jobKey={"name":"1","group":"g1"} description=JobDetail任務 jobDataMap={"taskId":"1","instanceId":"100"}
15:47:15.009 [DefaultQuartzScheduler_QuartzSchedulerThread] DEBUG org.quartz.simpl.PropertySettingJobFactory - Producing instance of Job 'g1.1', class=com.niewj.MyJob
15:47:15.010 [DefaultQuartzScheduler_Worker-2] DEBUG org.quartz.core.JobRunShell - Calling execute on job g1.1
15:47:15.010 [DefaultQuartzScheduler_QuartzSchedulerThread] DEBUG org.quartz.core.QuartzSchedulerThread - batch acquisition of 1 triggers
jobKey={"name":"1","group":"g1"} description=JobDetail任務 jobDataMap={"taskId":"1","instanceId":"100"}
15:47:20.013 [DefaultQuartzScheduler_QuartzSchedulerThread] DEBUG org.quartz.simpl.PropertySettingJobFactory - Producing instance of Job 'g1.1', class=com.niewj.MyJob
15:47:20.013 [DefaultQuartzScheduler_Worker-3] DEBUG org.quartz.core.JobRunShell - Calling execute on job g1.1
15:47:20.014 [DefaultQuartzScheduler_QuartzSchedulerThread] DEBUG org.quartz.core.QuartzSchedulerThread - batch acquisition of 1 triggers
jobKey={"name":"1","group":"g1"} description=JobDetail任務 jobDataMap={"taskId":"1","instanceId":"100"}
{"taskId":"1","instanceId":"100"}
5秒鐘輸出一次