java - 在 spring 中使用 Quartz 执行 Job 时出错
问题描述
我在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
不会发生并且produitService
是null
。当produitService.Repeat();
被调用时,然后NullPointerException
被抛出。
推荐阅读
- php - 如何替换两个不同的多维数组中的字符串?
- google-cloud-platform - Google ML 预测:1 个大数据集预测 vs 多个较小预测
- vbscript - 经典 ASP Base64 编码和换行符
- c# - 当我单击 GroupBox 内的更改按钮时,数据网格和数据库中的名称都会更改,但人的性别没有更改
- python - 当以秒计算时,需要以小时绘制余弦函数
- android - 无法打开刚刚上传到 Play 商店的应用 java.lang.UnsatisfiedLinkError:
- javascript - 如何在chartjs图上显示指数平滑?
- ios - 如何从 Material Inspector 设置 colorBufferWriteMask
- python-2.7 - 从 Python 运行 Matlab 脚本:TypeError: 'float' object is not iterable
- macos - 如何卸载 VS 代码