package com.lz.quartz.job;
import org.quartz.Job;
import org.quartz.JobExecutionContext;
import org.quartz.JobExecutionException;
import java.time.Instant;
import java.time.LocalDateTime;
import java.time.ZoneId;
import java.time.format.DateTimeFormatter;
public class NoStateHelloJob implements Job {
/**
*无状态的JOB 每次执行JOB 都创建一个新的JobDataMap
* 可以从打印结果中看出:cout 每次都是1
*/
private Integer count;
public void setCount(Integer count) {
this.count = count;
}
public NoStateHelloJob(){
System.out.println("quartz 每次任务执行 都会创建一个新的StateHelloJob 对象实例");
}
public void execute(JobExecutionContext jobExecutionContext) throws JobExecutionException {
System.out.println("************************************进入任务************************************************");
DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyy-mm-dd hh:mm:ss");
Instant instant1 = jobExecutionContext.getFireTime().toInstant();
ZoneId zoneId1 = ZoneId.systemDefault();
LocalDateTime taskTimeNow = instant1.atZone(zoneId1).toLocalDateTime();
System.out.println("获取当前任务执行的时间:"+formatter.format(taskTimeNow));
Instant instant2 = jobExecutionContext.getNextFireTime().toInstant();
ZoneId zoneId2 = ZoneId.systemDefault();
LocalDateTime taskTimeNext = instant2.atZone(zoneId2).toLocalDateTime();
System.out.println("获取任务下次执行的时间:"+formatter.format(taskTimeNext));
++count;
System.out.println("打印count结果:"+count);
//覆盖JobDetail中的JobDataMap中的count值
jobExecutionContext.getJobDetail().getJobDataMap().put("count",count);
System.out.println("------------------------------------------------------------");
}
}
package com.lz.quartz.main;
import com.lz.quartz.job.NoStateHelloJob;
import org.quartz.*;
import org.quartz.impl.StdSchedulerFactory;
public class NoStateHelloJobMain {
public static void main(String[] args) {
try {
//1:调度器Schedule
Scheduler scheduler = StdSchedulerFactory.getDefaultScheduler();
//2:任务实例 JobDeatil hellojob任务名称 group1任务分组名称
JobDetail jobDetail = JobBuilder.newJob(NoStateHelloJob.class).
withIdentity("stateHelloJob","group2").
usingJobData("message","嘿嘿嘿").
usingJobData("count",0).
build();
//3:触发器Trigger' trigger1触发器名称 group1触发器分组名 每5S执行一次打印 startNow马上启动触发器
Trigger trigger = TriggerBuilder.newTrigger().
withIdentity("trigger2","group2").startNow().
withSchedule(SimpleScheduleBuilder.simpleSchedule().withIntervalInSeconds(5).repeatForever()).
usingJobData("message","哈哈哈").
build();
//让调度器 使触发器和任务关联
scheduler.scheduleJob(jobDetail,trigger);
//启动任务调度器
scheduler.start();
} catch (SchedulerException e) {
e.printStackTrace();
}
}
}
打印结果:
log4j:WARN Please initialize the log4j system properly.
log4j:WARN See http://logging.apache.org/log4j/1.2/faq.html#noconfig for more info.
quartz 每次任务执行 都会创建一个新的StateHelloJob 对象实例
************************************进入任务************************************************
获取当前任务执行的时间:2018-52-18 09:52:46
获取任务下次执行的时间:2018-52-18 09:52:51
打印count结果:1
------------------------------------------------------------
quartz 每次任务执行 都会创建一个新的StateHelloJob 对象实例
************************************进入任务************************************************
获取当前任务执行的时间:2018-52-18 09:52:51
获取任务下次执行的时间:2018-52-18 09:52:56
打印count结果:1
------------------------------------------------------------
Process finished with exit code -1
从打印结果可以看出 每次任务执行 都创建一个新的JobDataMap 因为每次打印出JobDataMap中的count的值都是1 一样的