英文:
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.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论