所以,我在不同的 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
AnullPointerException
正在这条特定线上被抛出:
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.
和石英配置。
@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);
}
}
我错过了什么?如何正确注入应该安排其方法的类?
我相信这是发生了,因为石英JobBuilder
创建了你的新实例,SampleJob
而不是使用创建的具有自动装配字段的实例。因为它使用默认构造函数作为结果,所以你有空指针。
解决此问题的一种选择是将您的GetDataServiceQuartz
置于调度程序上下文中。
此处描述
因此,要放置您的数据,您需要调用:
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")
完整示例可以在这里找到。
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句