我使用Quartz调度程序运行作业,在这里,我正在获取Null指针异常 自动连线对象的QuartzJobBean扩展类,即DailyEmailsJob {testService.ts();}
请在下面找到代码
这是调度程序类,在这里我称为DailyEmailsJob类
@Component
public interface EmailSchedulers {
void dailyEmailTrigger();
@Service
@Transactional
@Slf4j
public class Impl implements EmailSchedulers{
@Override
public void dailyEmailTrigger() {
JobDetail job = JobBuilder.newJob(DailyEmailsJob.class)
.withIdentity("DailyEmail", "group1")
.build();
Trigger trigger = TriggerBuilder.newTrigger()
.withIdentity("DailyEmailTrigger", "group1")
.withSchedule(CronScheduleBuilder.cronSchedule("0 0 3 * * ?")) //At 03:00:00am every day
.build();
//schedule it
Scheduler scheduler;
try {
scheduler = new StdSchedulerFactory().getScheduler();
scheduler.start();
scheduler.scheduleJob(job, trigger);
} catch (SchedulerException e) {
e.printStackTrace();
log.info("erorrrrrrrrrrrrrrrrrr");
}
}
}
QuartzJobBean扩展类,在这里testService.ts();我得到了Null指针,如错误所示。
@Slf4j
@Component
public class DailyEmailsJob extends QuartzJobBean {
@Autowired
private TestService testService;
@Override
protected void executeInternal(JobExecutionContext arg0) throws JobExecutionException {
log.info("**************** DAILY SCHEDULER STARTED*****************");
testService.ts(); // Getting null pointer
log.info("**************** DAILY SCHEDULER ENDEND*****************");
}
}
这是我在DailyEmailsJob类中注入的服务类
@Component
public interface TestService {
void ts();
@Slf4j
@Service
public class Impl implements TestService{
@Override
public void ts() {
log.info("SERVICE WORKED");
}
}
}
2018-09-23 03:00:00.040 INFO 6020 --- [eduler_Worker-1] DailyEmailsJob : **************** DAILY SCHEDULER STARTED*****************
2018-09-23 03:00:00.041 ERROR 6020 --- [eduler_Worker-1] org.quartz.core.JobRunShell : Job group1.DailyEmail threw an unhandled Exception:
java.lang.NullPointerException: null
at DailyEmailsJob.executeInternal(DailyEmailsJob.java:42) ~[classes/:na]
at org.springframework.scheduling.quartz.QuartzJobBean.execute(QuartzJobBean.java:75) ~[spring-context-support-5.0.4.RELEASE.jar:5.0.4.RELEASE]
at org.quartz.core.JobRunShell.run(JobRunShell.java:202) ~[quartz-2.3.0.jar:na]
at org.quartz.simpl.SimpleThreadPool$WorkerThread.run(SimpleThreadPool.java:573) [quartz-2.3.0.jar:na]
2018-09-23 03:00:00.042 ERROR 6020 --- [eduler_Worker-1] org.quartz.core.ErrorLogger : Job (group1.DailyEmail threw an exception.
org.quartz.SchedulerException: Job threw an unhandled exception.
at org.quartz.core.JobRunShell.run(JobRunShell.java:213) ~[quartz-2.3.0.jar:na]
at org.quartz.simpl.SimpleThreadPool$WorkerThread.run(SimpleThreadPool.java:573) [quartz-2.3.0.jar:na]
Caused by: java.lang.NullPointerException: null
at DailyEmailsJob.executeInternal(DailyEmailsJob.java:42) ~[classes/:na]
at org.springframework.scheduling.quartz.QuartzJobBean.execute(QuartzJobBean.java:75) ~[spring-context-support-5.0.4.RELEASE.jar:5.0.4.RELEASE]
at org.quartz.core.JobRunShell.run(JobRunShell.java:202) ~[quartz-2.3.0.jar:na]
... 1 common frames omitted
答案 0 :(得分:0)
将以下行添加到executeInternal
类的DailyEmailsJob
方法的开头:
@Override
public void executeInternal(final JobExecutionContext context) throws JobExecutionException {
// Adding this autowires everything as needed
SpringBeanAutowiringSupport.processInjectionBasedOnCurrentContext(this);
...
}