java - Quartz 产生未处理的空指针异常

标签 java spring spring-boot quartz-scheduler

所以,我在不同的 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);
    }
}

我错过了什么?如何正确注入(inject)一个应该安排其方法的类?

最佳答案

我相信这种情况正在发生,因为quartz JobBuilder 创建了 SampleJob 的新实例,而不是使用带有 Autowiring 字段的创建实例。因为它使用默认构造函数作为结果,所以你有空指针。

解决此问题的一个选项是将您的 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 找到.

关于java - Quartz 产生未处理的空指针异常,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50326557/

相关文章:

Java : Custom Timestamp Format : verify format to microsec precision

java - 交换数组列表的内容

java - docker 容器中的 Eureka 客户端

java - 从命令行打包 Spring Boot 2.0.0.M5 应用程序时出错

spring - HAL 与 Spring Hateoas 和 Mvc(无引导)

java - 带有 Thymeleaf 的 IntelliJ (2018.3) - 静态内容路径上没有自动完成?

java - 在 Spring Boot 应用程序中加载第三方 jar 的问题

java - 有没有办法在 Visual Studio 2015 上进行 Java 编程

JavaScript 被解析为 HTML

java - 找到了多个名为 [spring_web] 的片段。这对于相对顺序是不合法的