public class PrintWordsJob implements Job {
@Override
public void execute(JobExecutionContext context) {
// 组名称.job任务名
JobKey jobKey = context.getJobDetail().getKey();
/** JobDataMap */
JobDataMap dataMap = context.getJobDetail().getJobDataMap();
JobDataMap dataMap2 = context.getTrigger().getJobDataMap();
JobDataMap dataMap3 = context.getMergedJobDataMap();
System.out.println(jobKey);
System.out.println(dataMap.get("k2"));
System.out.println(dataMap2.get("k2"));
System.out.println(dataMap3.get("k1") + "---" + dataMap3.get("k2"));
String printTime = new SimpleDateFormat("yy-MM-dd HH-mm-ss").format(new Date());
System.out.println("PrintWordsJob start at:" + printTime + ", prints: Hello Job-" + new Random().nextInt(100));
}
}
public class quartz {
public static void main(String[] args) throws SchedulerException, InterruptedException {
// 1、创建调度器Scheduler
SchedulerFactory schedulerFactory = new StdSchedulerFactory();
Scheduler scheduler = schedulerFactory.getScheduler();
// 2、创建JobDetail实例,并与PrintWordsJob类绑定(Job执行内容)
JobDetail jobDetail = JobBuilder.newJob(PrintWordsJob.class)
.withIdentity("job1", "group1").build();
// 3、构建Trigger实例,每隔1s执行一次
Trigger trigger = TriggerBuilder.newTrigger().withIdentity("trigger1", "triggerGroup1")
.withSchedule(CronScheduleBuilder.cronSchedule("0 0/1 * * * ?"))
.build();
// .startNow();//立即生效
// .withSchedule(SimpleScheduleBuilder.simpleSchedule()
// .withIntervalInSeconds(1)//每隔1s执行一次
// .repeatForever()).build();//一直执行
//4、执行
scheduler.scheduleJob(jobDetail, trigger);
System.out.println("--------scheduler start ! ------------");
scheduler.start();
int i = 0;
while (i > -1) {
}
// 睡眠
TimeUnit.MINUTES.sleep(1);
scheduler.shutdown();
System.out.println("--------scheduler shutdown ! ------------");
}
}
详细点的
// 1、创建调度器Scheduler(工厂模式)
SchedulerFactory schedulerFactory = new StdSchedulerFactory();
Scheduler scheduler = schedulerFactory.getScheduler();
// JobDataMap
JobDataMap jobDataMap = new JobDataMap(); // JobData
jobDataMap.put("k1", "JobDetail:job1"); // JobData
// 2、创建JobDetail实例,并与PrintWordsJob类绑定(Job执行内容)
JobDetail jobDetail = JobBuilder
.newJob(PrintWordsJob.class)
.withIdentity("job1", "group1") // 任务名, 组名称
.setJobData(jobDataMap) // 替换JobData
.usingJobData(jobDataMap) // 增加 JobData 内容
.usingJobData("k2","JobDetail:job2") // JobData
.build();
// 3、构建Trigger实例,每隔1s执行一次(job执行方式)
Trigger trigger = TriggerBuilder
.newTrigger()
.withIdentity("trigger1", "triggerGroup1") // 触发器, 组名称
.usingJobData("k2","Trigger:job2") // JobData
.withSchedule( SimpleScheduleBuilder
.simpleSchedule()
.withIntervalInSeconds(1) // 间隔1
.withRepeatCount(5) // 执行6次
//.repeatForever() //一直执行
)
// .withSchedule(CronScheduleBuilder.cronSchedule("0 0/1 * * * ?"))
.build();
// .startNow();//立即生效
/**
* 4、执行 (执行内容 + 执行方式 放入调度器中)
* 1个 scheduler 可以放 很多对 job-trigger
*/
scheduler.scheduleJob(jobDetail, trigger);
scheduler.start();
// 计算机下一个周期时间 (minute 整点)
Date runTime = DateBuilder.evenMinuteDate(new Date());
System.out.println("--------scheduler start ! -----"+ new Date().toString() +"-------" + runTime.toString());
// 睡眠
TimeUnit.SECONDS.sleep(10);
//5、结束执行
scheduler.shutdown();
System.out.println("--------scheduler shutdown ! ------------");
// scheduler 信息
SchedulerMetaData metaData = scheduler.getMetaData();
System.out.println("Executed " + metaData.getNumberOfJobsExecuted() + " jobs.");
直接触发
// 1、创建调度器Scheduler(工厂模式)
SchedulerFactory schedulerFactory = new StdSchedulerFactory();
Scheduler scheduler = schedulerFactory.getScheduler();
// 直接触发 job
JobDetail job = newJob(PrintWordsJob.class).withIdentity("job8", "group1").storeDurably().build();
scheduler.addJob(job, true);
scheduler.triggerJob(jobKey("job8", "group1"));
scheduler.start();