3

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?

Fazil Hussain
  • 425
  • 3
  • 16
  • Given that a previous execution could have failed anywhere: in a previous step, earlier in the current step (this reader is part of), you have to ask do you really want to know if the job has restarted or do you really want to know if whatever code you're about to execute in the reader has already executed? – Scott Kurz May 11 '16 at 12:03
  • 1
    I just want to know if the current execution is a restart or not, ideally in a listener's beforeStep() or beforeJob() – Fazil Hussain May 12 '16 at 04:17

2 Answers2

2

No, but there is an open issue asking for that: https://java.net/bugzilla/show_bug.cgi?id=7473

DFollis
  • 419
  • 2
  • 5
2

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;
}
Scott Kurz
  • 4,985
  • 1
  • 18
  • 40