Quartz scheduler not working in Spring batch admin setup

和自甴很熟 提交于 2020-01-07 06:15:09

问题


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.

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);
        }

    }
}

回答1:


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);    
    ...
}


来源:https://stackoverflow.com/questions/32356543/quartz-scheduler-not-working-in-spring-batch-admin-setup

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!