在 spring 中使用 Quartz 执行作业时出错
Error when executing Job with Quartz in spring
我在 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
这是我的主要方法
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();
}}
发生这种情况是因为 Quartz 通过构造函数创建了作业实例。然后 ProduitService
的注入不会发生并且 produitService
是 null
。当调用 produitService.Repeat();
时,会抛出 NullPointerException
。
我在 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
这是我的主要方法
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();
}}
发生这种情况是因为 Quartz 通过构造函数创建了作业实例。然后 ProduitService
的注入不会发生并且 produitService
是 null
。当调用 produitService.Repeat();
时,会抛出 NullPointerException
。