Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/spring/13.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Spring批处理作业实例已存在_Spring_Spring Batch - Fatal编程技术网

Spring批处理作业实例已存在

Spring批处理作业实例已存在,spring,spring-batch,Spring,Spring Batch,嗯 我知道以前有人问过这个问题,但我仍然找不到我的问题的确切答案。我的问题是:我正在使用SpringBatch将数据导出到SolrSearchServer。它需要每分钟运行一次,以便我可以导出所有更新。第一次执行通过OK,但第二次执行抱怨: 2014-10-02 20:37:00,022 [defaultTaskScheduler-1] ERROR: catching org.springframework.batch.core.repository.JobInstanceAlreadyComp

我知道以前有人问过这个问题,但我仍然找不到我的问题的确切答案。我的问题是:我正在使用SpringBatch将数据导出到SolrSearchServer。它需要每分钟运行一次,以便我可以导出所有更新。第一次执行通过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());
}
然后作业可以运行多次。但是,我还想查询作业的最后一次执行,因此我有如下代码:

.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());
}

这不会返回任何结果,因为我没有提供时间参数。看起来我可以运行一次作业并得到最后一次执行时间,或者我可以运行多次但没有得到最后一次执行时间。我真的被卡住了:(

假设

SpringBatch使用一些表来存储执行的每个作业及其参数。 如果使用相同的参数运行两次作业,则第二次作业将失败,因为作业由作业名称和参数标识

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\u JOB\u EXECUTION
表中查找您的
JobExecution
。 弹簧的基准


我希望我的回答对您有所帮助。

您可以使用另一个界面:
从其javadoc:

用于浏览正在运行或历史作业的执行情况的入口点,以及 由于数据可能会从持久性存储中重新水合,因此 可能不包含在 处决是积极的


按照Luca Basso Ricci的建议使用工作浏览器

因为您不知道需要按实例查找的作业参数

  • 查找名为searchExportJob的作业的最后一个实例
  • 查找上面实例的最后一次执行
  • 这样,您只能使用Spring批处理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());
     }
    
    //我们可以设置计数1,因为作业实例是按实例id降序排列的
    //所以我们知道返回的第一个是最后一个实例
    列表实例=jobExplorer.getJobInstances(“searchExportJob”,0,1);
    JobInstance lastInstance=instances.get(0);
    List jobExecutions=jobExplorer.getJobExecutions(lastInstance);
    //JobExcecutin是按执行id降序排列的
    //结果是最后一次执行
    JobExecution je=jobExecutions.get(0);
    if(je!=null&&je.getEndTime()!=null){
    endTime=newdatetime(je.getEndTime());
    }
    

    请注意,此代码仅适用于Spring Batch 2.2.x和2.1.x中的2.2.x及以上版本。API有些不同

    我无法真正获得执行,因为此时执行已丢失。如果我没有想到更好的方法,我可能会使用您的解决方案2-创建一个自定义DAO。@PetarTahchiev请告诉我是否工作正常。为了解决此问题是主题:)我在JobExplorer中没有看到可以帮助我实现目标的方法:(