我正尝试在作业完成之前返回Spring Batch作业ID。
我当前的实现仅在作业完成后返回信息 完成。
我使用批处理程序控制器和批处理服务,如下所示。 谢谢,我是Spring Batch的新手,并且经过了详尽的搜索 找不到与我的问题相关的内容。有一个帖子 有人在使用Apache Camel,但我不是。
控制器
@RequestMapping(value = "/batch/{progName}", method = RequestMethod.GET)
public ResponseEntity<JobResults> process(@PathVariable("progName") String progName,
@RequestHeader("Accept") MediaType accept) throws Exception {
HttpHeaders responseHeaders = MccControllerUtils.createCacheDisabledHeaders();
responseHeaders.setContentType(MediaType.APPLICATION_XML);
if(!accept.toString().equals("*/*")) {
responseHeaders.setContentType(accept);
}
LOGGER.info("Running batch program " + progName);
JobResults response = batchService.processProgName(progName);
return new ResponseEntity<JobResults>(response, responseHeaders, HttpStatus.OK);
}
服务
@Override
public JobResults processProgName(String progName) throws Exception {
String jobName = "call" + progName.toUpperCase() + "Job";
JobExecution jobExecution = null;
String result = "";
Long jobId = null;
JobResults results = new JobResults();
BatchStatus jobStatus = null;
try {
// Launch the appropriate batch job.
Map<String, Job> jobs = applicationContext.getBeansOfType(Job.class);
LOGGER.info("BATCHSTART:Starting sync batch job:" + jobName);
JobParametersBuilder builder = new JobParametersBuilder();
// Pass in the runtime to ensure a fresh execution.
builder.addDate("Runtime", new Date());
jobExecution = jobLauncher.run(jobs.get(jobName), builder.toJobParameters());
jobId = jobExecution.getId();
jobStatus = jobExecution.getStatus();
results.setName(jobName);
results.setId(jobId);
results.setMessage("The job has finished.");
results.setStatus(jobStatus);
LOGGER.info("The job ID is " + jobId);
LOGGER.info("BATCHEND:Completed sync batch job:" + jobName);
LOGGER.info("Completion status for batch job " + jobName + " is " + jobExecution.getStatus().name());
List<Throwable> failures = jobExecution.getAllFailureExceptions();
if (failures.isEmpty()) {
result = jobExecution.getExecutionContext().getString(AbstractSetupTasklet.BATCH_PROGRAM_RESULT);
} else {
for (Throwable fail : failures) {
result += fail.getMessage() + "\n";
}
}
LOGGER.info("The job results are: " + result);
} catch (JobExecutionAlreadyRunningException | JobRestartException | JobInstanceAlreadyCompleteException
| JobParametersInvalidException e) {
throw new RuntimeException("An error occurred while attempting to execute a job. " + e.getMessage(), e);
}
return results;
}
再次感谢。
编辑
我已将此添加到我的批处理配置中
@Bean(name = "AsyncJobLauncher")
public JobLauncher simpleJobLauncher(JobRepository jobRepository){
SimpleJobLauncher jobLauncher = new SimpleJobLauncher();
jobLauncher.setJobRepository(jobRepository);
jobLauncher.setTaskExecutor(new SimpleAsyncTaskExecutor());
return jobLauncher;
}
编辑
我在Mahmoud Ben Hassine的评论的帮助下解决了问题:)
我必须从服务中删除结果变量和信息,并添加上面看到的代码。现在,当我到达终点时,我会立即收到工作信息。
答案 0 :(得分:1)
您可以做的是在您的JobLauncher
中注入一个BatchService
实现,该实现由异步任务执行器(例如SimpleAsyncTaskExecutor
或ThreadPoolTaskExecutor
)支持。这将异步运行您的作业,并且将立即返回作业执行(您可以从中获取ID),而无需等待作业完成。
因此,通过在JobLauncher
中注入BatchService
的正确实现,无需更改processProgName
方法的代码即可实现您的要求。
您可以在这里找到有关如何同步或异步运行作业的更多详细信息:https://docs.spring.io/spring-batch/4.0.x/reference/html/job.html#configuringJobLauncher