【问题标题】:Error when executing Job with Quartz in spring在 spring 中使用 Quartz 执行 Job 时出错
【发布时间】:2020-08-31 18:43:20
【问题描述】:

我在 Spring Boot 项目中使用石英,我想每 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

这是我的主要方法

 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();
}}

【问题讨论】:

    标签: java spring spring-boot quartz-scheduler


    【解决方案1】:

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

    【讨论】:

    • 那么解决办法是什么?
    猜你喜欢
    • 1970-01-01
    • 2016-04-28
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-07-27
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多