我如何启动我的Spring批处理应用程序后,这个重构:



之前,我有一个批处理作业,它使用这个Cron bean和以下方法来触发批处理作业:

public class CronBatchConfig {
@Autowired
private JobLauncher jobLauncher;
@Autowired
private Job batchJob;
@Autowired
private BatchType batchType;
private final String ONETIME_BATCH_NAME = "ONETIME";
private final String RECON_BATCH_NAME = "RECON";
@Scheduled(cron = "00 00 17 ? * *", zone = "CST")
public void executeDailyBatch() throws JobParametersInvalidException, JobExecutionAlreadyRunningException, JobRestartException, JobInstanceAlreadyCompleteException {
batchType.setValue(RECON_BATCH_NAME);
final JobParameters jobParameters = new JobParametersBuilder().addLong("startTime", System.nanoTime()).toJobParameters();
final JobExecution execution = jobLauncher.run(batchJob, jobParameters);
log.info("Daily batch job status: " + execution.getExitStatus().getExitCode());
}

现在我已经删除了Cronbean类(我们正在使用不同的调度机制),我不确定如何触发此作业。调度机制不需要像带有@ scheduled注释的cron bean那样的代码,所以我不确定从哪里启动应用程序。

我尝试添加execute()方法到我的应用程序类的主方法,但这是抛出一个空指针:

@SpringBootApplication
@Slf4j
@EnableBatchProcessing
public class ReconBatchApplication extends DefaultBatchConfigurer {
@Autowired
private static JobLauncher jobLauncher;
@Autowired
private static Job batchJob;
@Autowired
private static BatchType batchType;
private static final String RECON_BATCH_NAME = "RECON";
public static void main(String[] args) throws JobInstanceAlreadyCompleteException, JobExecutionAlreadyRunningException, JobParametersInvalidException, JobRestartException {
ApplicationContext context = SpringApplication.run(ReconBatchApplication.class, args);
executeDailyBatch();
System.exit(SpringApplication.exit(context));
}
public static void executeDailyBatch() throws JobParametersInvalidException, JobExecutionAlreadyRunningException, JobRestartException, JobInstanceAlreadyCompleteException {
batchType.setValue(RECON_BATCH_NAME);
final JobParameters jobParameters = new JobParametersBuilder().addLong("startTime", System.nanoTime()).toJobParameters();
final JobExecution execution = jobLauncher.run(batchJob, jobParameters);
log.info("Daily batch job status: " + execution.getExitStatus().getExitCode());
}
```
Any suggestions would be appreciated

由于使用的是Spring Boot,因此可以让它为您启动作业,如文档中所述。您只需要在应用程序上下文中将作业定义为bean。你的主方法可以是这样的:

public static void main(String[] args) throws Exception {
// add job parameters as key/value pairs to args if you want to do it programmatically and not from the command line
System.exit(SpringApplication.exit(SpringApplication.run(ReconBatchApplication.class, args)));
}

相关内容

最新更新