问题
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?
回答1:
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;
}
来源:https://stackoverflow.com/questions/7808867/access-spring-batch-job-definition