我的工作必须在一天中的每一分钟完成,用户可以通过界面在白天启动它 .
所以我使用Quartz Scheduler将我的工作保存在JobDataMap上 .
@Service
public class MyJob {
private static final String TRIGGER_NAME = "myTriggerCronScheduleEveryMinute";
private static final String JOB_NAME = "jobRunner";
private static final String JOB_GROUPE = "groupRunnerJob";
private static final Logger LOGGER = LoggerFactory.getLogger(JobHoliday.class);
private Scheduler sched;
@EventListener(ApplicationReadyEvent.class)
public void registredJobOnTheSchedulerAfterApplicationReadyToServiceRequest() throws SchedulerException {
JobDetail job = newJob(SampleJob.class).withIdentity(JOB_NAME, JOB_GROUPE).storeDurably(true)
.requestRecovery(true).build();
Trigger trigger = newTrigger().withIdentity(TRIGGER_NAME, JOB_GROUPE)
.withSchedule(CronScheduleBuilder.cronSchedule(" 0 0/1 * 1/1 * ? *")).build();
SchedulerFactory schedFact = new org.quartz.impl.StdSchedulerFactory();
try {
sched = schedFact.getScheduler();
} catch (SchedulerException e) {
LOGGER.info("SchedulerException is thrown after get the scheduler");
throw e;
}
try {
sched.start();
} catch (SchedulerException e) {
LOGGER.info("SchedulerException is thrown after start a job");
throw e;
}
try {
sched.scheduleJob(job, trigger);
} catch (SchedulerException e) {
LOGGER.info("SchedulerException is thrown after schedule a job");
throw e;
}
}
}
然后我创建了另一个类,它还扩展了QuartzJobBean以实现executeInternal方法 .
@Override
protected void executeInternal(JobExecutionContext arg0) throws JobExecutionException {
Scheduler scheduler;
LOGGER.info("holiday job");
try {
try {
scheduler = new StdSchedulerFactory().getScheduler();
for (String groupName : scheduler.getJobGroupNames()) {
for (JobKey jobKey : scheduler.getJobKeys(GroupMatcher.jobGroupEquals(groupName))) {
if ("jobRunner".equals(jobKey.getName())) {
System.out.println("it's run");
} else {
System.out.println("it's not runing");
}
}
}
} catch (SchedulerException e) {
e.printStackTrace();
}
} catch (Exception e) {
LOGGER.info("Failed run Job holiday ", e);
throw new JobExecutionException(e);
}
LOGGER.info("Success run Job holiday");
}
当我运行应用程序时,它不会启动作业,这是令人困惑的,因为当我使用minimuim指令创建作业时:
@Override
protected void executeInternal(JobExecutionContext context) throws JobExecutionException {
System.out.println("sample job");
}
它将显示“样本作业” .
我的问题是:为什么这种行为?为什么他在一个 class 而不是另一个 class 中展示“工作实例”?