春季使用Quartz执行作业时出错



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

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

最新更新