0

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 Answers1

0

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);    
    ...
}
Community
  • 1
  • 1
yishaiz
  • 2,433
  • 4
  • 28
  • 49