Spring Batch: How to find out if job is restarted

孤街浪徒 提交于 2019-12-23 01:55:22

问题


Is there any possibility to find out, If a job is restarted in Spring Batch?

We do provide some Tasklets without restart-support from spring-batch and has to implement our own proceeding, if job is restarted.

Can't find any possibility in JobRepository, JobOperator, JobExplorer, etc.


回答1:


Define a JobExplorer bean with required properties

<bean id="jobExplorer"
class="org.springframework.batch.core.explore.support.JobExplorerFactoryBean">
<property name="dataSource" ref="dataSource"/>
<property name="lobHandler" ref="lobHandler"/>
</bean>

Query it with your jobName

List<JobInstance> jobInstances= jobExplorer.getJobInstances(jobName);

for (JobInstance jobInstance : jobInstances) {
    List<JobExecution> jobExecutions = jobExplorer.getJobExecutions(jobInstance);
    for (JobExecution jobExecution : jobExecutions) {
        if (jobExecution.getExitStatus().equals(ExitStatus.COMPLETED)) {
        //You found a completed job, possible candidate for a restart
        //You may check if the job is restarted comparing jobParameters
        JobParameters jobParameters = jobInstance.getParameters();
        //Check your running job if it has the same jobParameters 
        }
     }
}

Did not compile this but I hope it gives an idea




回答2:


Another way using jobExplorer is execute the following command:

jobExplorer.getJobExecutions(jobExplorer.getJobInstance(currentJobExecution.getJobInstance().getId())).size() > 1;

This statement verifies if another execution of the the same job (same id) exists. In environments with minimum control, does not exist possibility that the other execution be not a failed or stopped execution.




回答3:


Potentially you can find this information in spring-batch's database tables, can't remeber the exact table's name, but you can figure out quickly because there are only few tables. I guess there is some information regarding restarting.



来源:https://stackoverflow.com/questions/11685013/spring-batch-how-to-find-out-if-job-is-restarted

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!