我发送post请求到run()方法类名是ScheduleGame。这个类创建了基于Quartz RAM的作业和触发器。
@Component
public class ScheduleGame {
Logger log = LoggerFactory.getLogger(ScheduleGame.class);
public String run(String jobName, String jobGroup, Class jobClass, String jobTrigger, String cronExp) throws Exception {
try {
isJobExist(jobGroup, jobName);
log.info("------- Initializing -------------------");
// First we must get a reference to a scheduler
SchedulerFactory sf = new StdSchedulerFactory();
Scheduler sched = sf.getScheduler();
log.info("------- Initialization Complete --------");
log.info("------- Scheduling Jobs ----------------");
// job1 will only fire once at date/time "ts"
JobDetail job = newJob(jobClass).withIdentity(jobName, jobGroup).build();
CronTrigger trigger = newTrigger().withIdentity(jobTrigger, jobGroup).withSchedule(
cronSchedule(cronExp).withMisfireHandlingInstructionFireAndProceed()).build();
// schedule it to run!
Date ft = sched.scheduleJob(job, trigger);
log.info(job.getKey() + " will run at: " + ft + " and repeat: " + trigger.getNextFireTime() + " times, every "
+ trigger.getExpressionSummary() + " seconds");
sched.start();
log.info(sched.getSchedulerName());
return "Game Created and Scheduled, it will run at: " + ft;
} catch (CustomException e) {
throw new Exception(e.getMessage());
}
}
}
我的工作班是
@Component
public class Job implements InterruptableJob {
private static Logger _log = LoggerFactory.getLogger(Job.class);
private JobKey _jobKey = null;
@Autowired
private Results results;
/*public void setResults(Results results) {
this.results = results;
}*/
/**
* Empty constructor for job initialization
*/
public Job() {
}
/**
* <p>
* Called by the <code>{@link org.quartz.Scheduler}</code> when a
* <code>{@link org.quartz.Trigger}</code> fires that is associated with
* the <code>Job</code>.
* </p>
*
* @throws JobExecutionException if there is an exception while executing the job.
*/
public void execute(JobExecutionContext context)
throws JobExecutionException {
SpringBeanAutowiringSupport.processInjectionBasedOnCurrentContext(this);
// This job simply prints out its job name and the
// date and time that it is running
_jobKey = context.getJobDetail().getKey();
_log.info("Job Name: "+ _jobKey.getName() +" Job Group: "+ _jobKey.getGroup()+" SimpleJob says: " + _jobKey + " executing at " + new Date());
results.lotteryResults(_jobKey.getName(),_jobKey.getGroup());
}
@Override
public void interrupt() throws UnableToInterruptJobException {
_log.info("---" + _jobKey + " -- INTERRUPTING --");
}
}
例外是:
java.lang.NullPointerException: null
at agaming.casino.lottery.handler.Job.execute(Job.java:54)
at org.quartz.core.JobRunShell.run(JobRunShell.java:202)
at org.quartz.simpl.SimpleThreadPool$WorkerThread.run(SimpleThreadPool.java:573)
.19:24:00.001 [DefaultQuartzScheduler_Worker-10] ERROR org.quartz.core.ErrorLogger - Job (SampleJob.Sample Job threw an exception.
org.quartz.SchedulerException: Job threw an unhandled exception.
at org.quartz.core.JobRunShell.run(JobRunShell.java:213)
at org.quartz.simpl.SimpleThreadPool$WorkerThread.run(SimpleThreadPool.java:573)
Caused by: java.lang.NullPointerException: null
at agaming.casino.lottery.handler.Job.execute(Job.java:54)
at org.quartz.core.JobRunShell.run(JobRunShell.java:202)
... 1 common frames omitted
当在预定时间触发时,如果没有@Autowired后跟我的服务类,则作业正常运行。但我已经@Autowired我的服务,在工作岗位上做其他业务职能。
如何在Quartz作业类中@Autowired我的服务
我使用的是Spring Boot