好的,
我知道以前有人问过这个问题,但我仍然找不到我的问题的明确答案。我的问题是:我正在使用 Spring Batch 将数据导出到 SOLR 搜索服务器。它需要每分钟运行一次,因此我可以导出所有更新。第一次执行成功,但第二次执行却提示:
2014-10-02 20:37:00,022 [defaultTaskScheduler-1] ERROR: catching
org.springframework.batch.core.repository.JobInstanceAlreadyCompleteException: A job instance already exists and is complete for parameters={catalogVersionPK=3378876823725152,
type=UPDATE}. If you want to run this job again, change the parameters.
at org.springframework.batch.core.repository.support.SimpleJobRepository.createJobExecution(SimpleJobRepository.java:126)
at
当然,我可以向作业添加日期时间参数,如下所示:
.addLong("time", System.getCurrentTimeMillis())
然后该作业可以运行多次。但是,我还想查询作业的最后执行情况,所以我有这样的代码:
DateTime endTime = new DateTime(0);
JobExecution je = jobRepository.getLastJobExecution("searchExportJob", new JobParametersBuilder().addLong("catalogVersionPK", catalogVersionPK).addString("type", type).toJobParameters());
if (je != null && je.getEndTime() != null) {
endTime = new DateTime(je.getEndTime());
}
这不会返回任何内容,因为我没有提供时间参数。所以看起来我可以运行一次作业并获取最后的执行时间,或者我可以运行它多次但无法获取最后的执行时间。我真的被困住了:(
最佳答案
假设
Spring Batch 使用一些表来存储执行的每个作业及其参数。 如果使用相同的参数运行该作业两次,则第二次会失败,因为该作业是通过 jobName 和参数来标识的。
1# 解决方案
运行新作业时可以使用 JobExecution。
JobExecution execution = jobLauncher.run(job, new JobParameters());
.....
// Use a JobExecutionDao to retrieve the JobExecution by ID
JobExecution ex = jobExecutionDao.getJobExecution(execution.getId());
2#解决方案
您可以实现自定义 JobExecutionDao 并执行自定义查询以在 BATCH_JOB_EXECUTION
表上查找您的 JobExecution
。
See here Spring的引用。
希望我的回答对您有帮助。
关于java - Spring Batch 作业实例已存在,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26166992/