简体   繁体   English

在 spring 中使用 Quartz 执行作业时出错

[英]Error when executing Job with Quartz in spring

I am using quartz in spring boot project where I want to repeat every 1 minute my service methode repeat() But I have this Error:我在 spring 引导项目中使用石英,我想每 1 分钟重复一次我的服务方法 repeat() 但我有这个错误:

Scheduler class: 'org.quartz.core.QuartzScheduler' - running locally.
NOT STARTED.
Currently in standby mode.
Number of jobs executed: 0
Using thread pool 'org.quartz.simpl.SimpleThreadPool' - with 10 threads.
Using job-store 'org.quartz.simpl.RAMJobStore' - which does not support persistence. and is not 
clustered.

org.quartz.SchedulerException: Job threw an unhandled exception.
at org.quartz.core.JobRunShell.run(JobRunShell.java:213) ~[quartz-2.3.2.jar:na]
at org.quartz.simpl.SimpleThreadPool$WorkerThread.run(SimpleThreadPool.java:573) [quartz- 
2.3.2.jar:na]
Caused by: java.lang.NullPointerException: null
at com.ssm.Quartz.QuartzJob.execute(QuartzJob.java:17) ~[classes/:na]
at org.quartz.core.JobRunShell.run(JobRunShell.java:202) ~[quartz-2.3.2.jar:na]
... 1 common frames omitted

This is my main method这是我的主要方法

 public static void main(String[] args) throws SchedulerException  {
    SpringApplication.run(InventoryApplication.class, args);
    JobDetail job = JobBuilder.newJob(QuartzJob.class).build();
    Trigger t2 =  TriggerBuilder.newTrigger()
            .withIdentity("CronTrigger")
            .withSchedule(CronScheduleBuilder.cronSchedule("0 0/1 * 1/1 * ? *")).build();
     Scheduler sc = StdSchedulerFactory.getDefaultScheduler();
    sc.start();
    sc.scheduleJob(job, t2);}

QuartzJob class石英作业 class

public class QuartzJob implements Job {


@Autowired private ProduitService produitService;
@Override
public void execute(JobExecutionContext context) throws JobExecutionException {
    System.out.println("Updated");
    produitService.Repeat();
}}

This happens because Quartz creates the instance of job via constructor.发生这种情况是因为 Quartz 通过构造函数创建了作业实例。 Then injection of ProduitService does not happen and produitService is null .然后ProduitService的注入不会发生并且produitServicenull And when produitService.Repeat();produitService.Repeat(); is invoked, then NullPointerException is thrown.被调用,然后抛出NullPointerException

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM