问题描述
背景:我们有一些工作是由cron作业触发的,由spring batch(作为启动应用程序)管理的,我正在努力将cron替换为石英,并添加spring batch admin来管理作业.
Background :We've some jobs managed by spring batch ( as boot application) triggered by cron job, I'm working to replace cron with quartz and add spring batch admin to manage jobs.
到目前为止,我已经能够通过Spring Batch管理控制台运行作业,当石英试图触发作业执行时会发生问题. JobLauncher,JobLocator对象为null,这是自动装配的.请注意,我使用的是基于Java的配置,而不是XML.
So far I'm able to run the jobs via spring batch admin console, issue happens when quartz attempts to fire a job execution. JobLauncher, JobLocator objects are null which is autowired. Please note I use Java based configuration not XML.
@PersistJobDataAfterExecution
@DisallowConcurrentExecution
@Component
@EnableBatchProcessing
public class GatewayReconciliationQuartzJob extends QuartzJobBean {
private static final String JOB_NAME = "GatewayReconciliationJob";
@Autowired
BatchJobLauncher batchJobLauncher;
@Autowired
private JobLocator jobLocator;
@Autowired
private JobLauncher jobLauncher;
@Override
protected void executeInternal(JobExecutionContext context) {
try {
if (null == jobLauncher) {
LOG.info("JobLauncher is null ");
}
if (null == jobLocator) {
LOG.info("jobLocator is null ");
}
LOG.info(String.format("Now really Starting Batch Job : %s", JOB_NAME));
JobParametersBuilder builder = new JobParametersBuilder();
builder.addDate("date", new Date());
this.jobLauncher.run(this.jobLocator.getJob(JOB_NAME), builder.toJobParameters());
} catch (JobExecutionAlreadyRunningException | JobInstanceAlreadyCompleteException | JobParametersInvalidException | NoSuchJobException | JobRestartException e) {
LOG.error("Error executing job", e);
throw new RuntimeException(e);
}
}
}
推荐答案
从此处:将以下行添加到executeInternal
方法的开头:
From here:Add the following line to the start of the executeInternal
method:
@Override
public void executeInternal(final JobExecutionContext context) throws JobExecutionException {
// Adding this autowires everything as needed
SpringBeanAutowiringSupport.processInjectionBasedOnCurrentContext(this);
...
}
这篇关于Quartz Scheduler在Spring Batch管理员设置中不起作用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!