在使用Spring中的Quartz执行作业时出现错误。

huangapple go评论60阅读模式
英文:

Error when executing Job with Quartz in spring

问题

我在Spring Boot项目中使用Quartz想要每隔1分钟重复执行我的服务方法repeat()但我遇到了以下错误

调度程序类:'org.quartz.core.QuartzScheduler' - 本地运行
尚未启动
当前处于待机模式
已执行的作业数0
使用线程池'org.quartz.simpl.SimpleThreadPool' - 共有10个线程
使用作业存储'org.quartz.simpl.RAMJobStore' - 不支持持久性并且不是集群化的

org.quartz.SchedulerException作业引发未处理的异常
	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
*这是我的主要方法*

    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类*

public class QuartzJob implements Job {

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

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 :

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

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

答案1

得分: 0

这是因为Quartz通过构造函数创建作业实例。然后,ProduitService的注入不会发生,produitService的值为null。当调用produitService.Repeat();时,会抛出NullPointerException

英文:

This happens because Quartz creates the instance of job via constructor. Then injection of ProduitService does not happen and produitService is null. And when produitService.Repeat(); is invoked, then NullPointerException is thrown.

huangapple
  • 本文由 发表于 2020年9月1日 02:43:20
  • 转载请务必保留本文链接:https://go.coder-hub.com/63676456.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定