共计 4723 个字符,预计需要花费 12 分钟才能阅读完成。
[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 秒钟输入一次
正文完