我正在通过Spring Boot使用Spring Batch。
我有两个作业,可通过以下方式执行:
private static final Logger LOGGER = LoggerFactory.getLogger(TimeWorkedJobLauncher.class);
private final Job job;
//private final Job jobDS;
private final JobLauncher jobLauncher;
@Autowired
private ResourceLoader resourceLoader;
@Autowired
TimeWorkedJobLauncher(@Qualifier("timeworkedJob") Job job, JobLauncher jobLauncher) {
System.out.println("Inside TimeWorkedJobLauncher");
this.job = job;
this.jobLauncher = jobLauncher;
//this.jobDS = jobDS;
}
@Async
@Scheduled(cron = "${reports.timeWorked.job.cron}")
public void execute() throws JobParametersInvalidException, JobExecutionAlreadyRunningException,
JobRestartException, JobInstanceAlreadyCompleteException {
LOGGER.info("Starting TimeWorked job");
JobParametersBuilder builder = new JobParametersBuilder();
builder.addDate("date", new Date());
builder.addLong("time",System.currentTimeMillis());
builder.addString(LoaderConstants.JOB_PARAMETER, resourceLoader.getTimeWorkedFileLoc());
jobLauncher.run(job, builder.toJobParameters());
LOGGER.info("Stopping TimeWorked job");
}
@Async
@Scheduled(cron = "${reports.timeWorked.job.cronds}")
public void executeDSJob() throws JobParametersInvalidException, JobExecutionAlreadyRunningException,
JobRestartException, JobInstanceAlreadyCompleteException {
JobParametersBuilder builder = new JobParametersBuilder();
builder.addDate("date", new Date());
builder.addLong("time",System.currentTimeMillis());
builder.addString(LoaderConstants.JOB_PARAMETER, resourceLoader.getTimeWorkedFileLocForDS());
jobLauncher.run(job, builder.toJobParameters());
LOGGER.info("Stopping TimeWorked job for DS");
}
唯一的区别是,当我运行executeDSJob()时,我需要作业执行与使用execute()运行作业时执行的方法不同的beforeStep()和afterStep()方法。
我配置了两个配置类,唯一的区别是一个类返回
@Bean
public StepExecutionListener timeworkedCountProcessor() {
return new TimeWorkedProcessorDS();
}
,其他返回
@Bean
public StepExecutionListener timeworkedCountProcessor() {
return new TimeWorkedProcessor();
}
但是当我运行execute()方法时,将使用TimeWorkedProcessorDS(),并使用此类的afterStep()和beforeStep()。
我该怎么做?