Search code examples
jsr352java-batch

JSR 352: Is there a way to tell if a particular job execution is a restart or not from within a job?


I know how to get the Execution Id and Instance Id of a job using the Job Context. But if i restart a job, is there way to know if the job execution is the first execution or a restart within the job, for instance inside the reader?


Solution

  • This is a bit overly complicated (as the other answer noted, there's an issue opened to consider enhancement for the future Batch 1.1).

    You could do this:

    //
    // Assumes JobContext injected into 'jobCtx' field
    //
    private boolean isRestart() {
        JobOperator jo = BatchRuntime.getJobOperator();
        JobInstance jobInstance = jo.getJobInstance(jobCtx.getExecutionId());
        int numExecutions = jo.getJobExecutions(jobInstance).size();
        return numExecutions > 1;
    }