访问Spring Batch作业定义

时间:2011-10-18 14:25:32

标签: spring spring-batch

我有一份职位描述:

<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>

我想知道在“importJob”中定义了多少步骤(在本例中为5)。看起来像作业 JobInstance api没有任何相关性。这有可能吗?

1 个答案:

答案 0 :(得分:1)

您有选项


  • JobExplorer

阅读有关您工作的元数据的最简洁方法是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

但你也可以通过简单地查看JobExecution来获得它:

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

JobLauncher在您启动作业时返回JobExecution

public interface JobLauncher {

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

或者您可以通过JobExecutionListener

获取
public interface JobExecutionListener {

    void beforeJob(JobExecution jobExecution);

    void afterJob(JobExecution jobExecution);
}

还有其他方法可以获得它,但上述两种方法就足够了。


编辑回答评论:

如果您想要获取元数据而不管步骤是否已执行,则有一种方便方法getStepNamesAbstractJob定义并实现(例如) 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;
 }