1

I've got a Job description:

<job id="importJob" job-repository="jobRepository">
    <step id="importStep1" next="importStep2" parent="abstractImportStep">
        <tasklet ref="importJobBean" />
    </step>
    <step id="importStep2" next="importStep3" parent="abstractImportStep">
        <tasklet ref="importJobBean" />
    </step>
    <step id="importStep3" next="importStep4" parent="abstractImportStep">
        <tasklet ref="importJobBean" />
    </step>
    <step id="importStep4" next="importStepFinish" parent="abstractImportStep">
        <tasklet ref="importJobBean" />
    </step>
    <step id="importStepFinish">
        <tasklet ref="importJobBean" />
    </step>
</job>

I want to know how many steps were defined in "importJob" (5 in this case). Looks like Job and JobInstance api has nothing relevant. Is this possible at all?

emeraldjava
  • 10,894
  • 26
  • 97
  • 170

1 Answers1

2

You have options


  • JobExplorer

The cleanest way to read meta data about your Job is through JobExplorer:

public interface JobExplorer {

    List<JobInstance> getJobInstances(String jobName, int start, int count);

    JobExecution getJobExecution(Long executionId);

    StepExecution getStepExecution(Long jobExecutionId, Long stepExecutionId);

    JobInstance getJobInstance(Long instanceId);

    List<JobExecution> getJobExecutions(JobInstance jobInstance);

    Set<JobExecution> findRunningJobExecutions(String jobName);
}

  • JobExecution

But you can also get it by simply looking at JobExecution:

// Returns the step executions that were registered
public Collection<StepExecution> getStepExecutions()

JobLauncher returns you a JobExecution when you launch the job:

public interface JobLauncher {

    public JobExecution run(Job job, JobParameters jobParameters) 
                throws JobExecutionAlreadyRunningException, JobRestartException;
}

Or you can get it via JobExecutionListener

public interface JobExecutionListener {

    void beforeJob(JobExecution jobExecution);

    void afterJob(JobExecution jobExecution);
}

There are other ways to obtain it, but the above two should suffice.


EDIT to answer the comment:

In case you'd like to get a metadata regardless of whether or not the step was executed, there is a convenience method getStepNames which is defined by the AbstractJob and is implemented (e.g.) in SimpleJob as:

/**
 * Convenience method for clients to inspect the steps for this job.
 * 
 * @return the step names for this job
 */
 public Collection<String> getStepNames() {
     List<String> names = new ArrayList<String>();
     for (Step step : steps) {
         names.add(step.getName());
     }
     return names;
 }
tolitius
  • 22,149
  • 6
  • 70
  • 81
  • Hey tolitius. Thanx for answer. But I think it is not what I am looking for. I need to fetch meta information about job definition but not job execution... – Dalius Šidlauskas Oct 19 '11 at 12:23
  • what exactly are you trying to do (at which point do you need this information)? because if `getStepExecutions().size()` will give you the number of steps in a job. – tolitius Oct 19 '11 at 14:25
  • Well I am trying to implement a job progress bar. For progress tracking I need to know how many steps were configured on the Job and how many steps were executed. For this I have created a StepExecutionListener that tracks how many steps were completed, and this data is resolved by *stepExecution.getJobExecution().getStepExecutions()*. But this does not represent total configured steps in the Job.. – Dalius Šidlauskas Oct 19 '11 at 16:12
  • I see, then you can just use a `job.getStepNames()`. I updated the answer. – tolitius Oct 19 '11 at 16:35