因此,我在不同的Spring配置文件上设置了2个调度程序。当我运行Spring调度程序时,一切正常,但他们希望我实现Quartz。
这是一个Job类:
@Profile("quartz")
@Component
public class SampleJob implements Job {
@Autowired
private GetDataServiceQuartz getDataServiceQuartz;
public SampleJob() {
}
public SampleJob(GetDataServiceQuartz getDataServiceQuartz) {
this.getDataServiceQuartz = getDataServiceQuartz;
}
@Override
public void execute(JobExecutionContext jobExecutionContext) throws JobExecutionException {
this.getDataServiceQuartz.storeData();
}
}
抛出错误:
org.quartz.SchedulerException: Job threw an unhandled exception.
at org.quartz.core.JobRunShell.run(JobRunShell.java:213) ~[quartz-2.2.1.jar:na]
at org.quartz.simpl.SimpleThreadPool$WorkerThread.run(SimpleThreadPool.java:573) [quartz-2.2.1.jar:na]
Caused by: java.lang.NullPointerException: null
at com.example.blockchaininfo.services.Quartz.SampleJob.execute(SampleJob.java:27) ~[classes/:na]
at org.quartz.core.JobRunShell.run(JobRunShell.java:202) ~[quartz-2.2.1.jar:na]
... 1 common frames omitted
此特定行上会引发nullPointerException
:
this.getDataServiceQuartz.storeData();
我尝试打印this.getDataServiceQuartz
时会打印null
。
完成所有工作的课程:
@Slf4j
@Service
@Profile("quartz")
public class GetDataServiceQuartz{
constructorHere();
public void storeData(){
try {
String hashrateFromApi = this.getNetworkHashrateFromApi("http://public.turtlenode.io:11898/getinfo");
OffsetDateTime date = OffsetDateTime.now();
this.saveNetworkHashrateNewEntity(hashrateFromApi, date);
this.storePoolDataToDB(this.getPoolsListFromJson(), retrieveNetworkIdForPoolDefinition(date));
} catch (HttpServerErrorException e){
log.info("Network Server error e1: " + e);
} catch (ResourceAccessException e2){
log.info("Network resource access exception: " + e2);
} catch (IOException e3) {
log.info("" + e3);
} catch (InterruptedException e4){
log.info("" + e4);
}
}
...all other methods to acquire stuff.
和Quartz配置。
@Override
public void onApplicationEvent(ContextRefreshedEvent contextRefreshedEvent){
this.startQuartzScheduling();
}
public void startQuartzScheduling () {
JobDetail job = JobBuilder.newJob(SampleJob.class)
.withIdentity("dummyJobName", "group1").build();
Trigger trigger = TriggerBuilder
.newTrigger()
.withIdentity("dummyTriggerName", "group1")
.withSchedule(
SimpleScheduleBuilder.simpleSchedule()
.withIntervalInSeconds(5).repeatForever())
.build();
try {
Scheduler scheduler = StdSchedulerFactory.getDefaultScheduler();
scheduler.start();
scheduler.scheduleJob(job, trigger);
} catch (SchedulerException e){
log.info("" + e);
}
}
我错过了什么?如何正确地注入一个应该安排其方法的类?
答案 0 :(得分:2)
我认为这是一个很好的因为石英JobBuilder
创建了SampleJob
的新实例,而不是使用自动装配字段创建的实例。因为它使用默认构造函数作为结果,所以你有nullpointer。
解决此问题的一个选项是将GetDataServiceQuartz
放入调度程序上下文。
描述here
因此,要输入您需要调用的数据:
scheduler.getContext().put("getDataServiceQuartz", getDataServiceQuartz);
执行任务时:
SchedulerContext schedulerContext = jobExecutionContext.getScheduler().getContext();
schedulerContext.get("getDataServiceQuartz");
其他,在我看来更方便的方法是将它放到JobDataMap
,SampleJob
:
job.getJobDataMap().put("getDataServiceQuartz", getDataServiceQuartz);
执行任务时:
context.getJobDetail().getJobDataMap().get("getDataServiceQuartz")
可以找到完整示例here。