好的,
我知道以前已经有人问过这个问题,但是我仍然找不到我的问题的明确答案。我的问题是:我正在使用spring batch将数据导出到SOLR搜索服务器。它需要每分钟运行一次,因此我可以导出所有更新。第一个执行通过OK,但是第二个执行抱怨:
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());
}
由于我没有提供时间参数,因此没有返回任何结果。因此,似乎我可以一次运行该作业并获得最后的执行时间,或者我可以多次运行它而没有获得最后的执行时间。我真的被卡住了:(
使用Luca Basso Ricci建议的Job Explorer。
因为您不知道作业参数,所以需要按实例查找。
这样,您仅使用Spring Batch API
//We can set count 1 because job instance are ordered by instance id descending
//so we know the first one returned is the last instance
List<JobInstance> instances = jobExplorer.getJobInstances("searchExportJob",0,1);
JobInstance lastInstance = instances.get(0);
List<JobExecution> jobExecutions = jobExplorer.getJobExecutions(lastInstance);
//JobExcectuin is ordered by execution id descending so first
//result is the last execution
JobExecution je = jobExecutions.get(0);
if (je != null && je.getEndTime() != null) {
endTime = new DateTime(je.getEndTime());
}
请注意,此代码仅适用于2.1.x中的Spring Batch 2.2.x及更高版本,API有所不同
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句