Spring批处理jobtep示例

时间:2015-11-27 18:52:08

标签: java spring parallel-processing spring-batch

我试图在春季批处理中并行运行多个作业。经过google搜索后我遇到了JobStep.Has任何使用JobStep的人都可以解释如何使用它来并行运行作业或者是否有其他方法可以运行并行2个独立作业,即当我启动批处理2时,作业应该开始并行运行 我的要求就像

 <batch:job id="JobA" incrementer="runIdIncrementer">
            <batch:step id="abc">
                <batch:tasklet transaction-manager="transactionManager">
                    <batch:chunk reader="ReaderA" writer="WriterA" processor="ProcessorA" />
                </batch:tasklet>
          </batch:step>
    </batch:job>

    <batch:job id="JobB" incrementer="runIdIncrementer">
            <batch:step id="def">
                <batch:tasklet transaction-manager="transactionManager">
                    <batch:chunk reader="ReaderB" writer="WriterB" processor="ProcessorB" />
                </batch:tasklet>
          </batch:step>
    </batch:job>

当我的应用程序启动时,两个作业都应该开始运行。是否可以使用spring batch

这样做

编辑:我甚至尝试过这种方式

<batch:job id="ParallelJob" incrementer="runIdIncrementer" restartable="true">
            <batch:split id="parallelprocessing" task-executor="taskExecutor">
                <batch:flow>
                    <batch:step id="ParallelJob.step1" >
                            <batch:job ref="JobA" job-launcher="jobLauncher" job-parameters-extractor="jobParametersExtractor"/>
                    </batch:step>       
                </batch:flow>
                <batch:flow>
                    <batch:step id="ParallelJob.step2" >
                            <batch:job ref="JobB" job-launcher="jobLauncher" job-parameters-extractor="jobParametersExtractor"/>
                    </batch:step>       
                </batch:flow>
            </batch:split>
        </batch:job> 


<batch:job id="JobA" restartable="true">
        <batch:step id="abc">
            <batch:tasklet >
                <batch:chunk reader="reader" writer="writer" processor="processor"  />
            </batch:tasklet>
      </batch:step>

    </batch:job>

<batch:job id="JobB" restartable="true">
        <batch:step id="abc">
            <batch:tasklet >
                <batch:chunk reader="reader" writer="writer" processor="processor"  />
            </batch:tasklet>
      </batch:step>

    </batch:job>

我正面临异常说法 org.springframework.beans.factory.NoUniqueBeanDefinitionException:没有定义[org.springframework.batch.core.Job]类型的限定bean:期望的单个匹配bean但找到3:ParallelJob,JobA,JobB。我正在使用commandLineJobRunner启动作业传递jobId asParallelJob

1 个答案:

答案 0 :(得分:0)

定义:

<batch:job id="globalJob">
<batch:split id="StepOne">
    <batch:flow>
    <batch:step id="step1" >
        <batch:job ref="jobRef1"/>
    </batch:step>

    </batch:flow>
    <batch:flow>
    <batch:step id="step2">
        <batch:job ref="jobRef2"/>
    </batch:step>
    </batch:flow>
</batch:split>
</batch:job>

随时执行作业:

@Autowired
JobLauncher jobLauncher;

@Autowired
@Qualifier("globalJob")
Job globalJob;

 @Override
public void executeGlobalJob() throws NoSuchJobException, JobInstanceAlreadyExistsException, JobParametersInvalidException {

    try {
        jobLauncher.run(globalJob,new JobParameters());
    } catch (JobExecutionAlreadyRunningException e) {
        throw new JobInstanceAlreadyExistsException("JobExecutionAlreadyRunningException",e);
    } catch (JobRestartException e) {
        throw new JobInstanceAlreadyExistsException("JobRestartException",e);
    } catch (JobInstanceAlreadyCompleteException e) {
        throw new JobInstanceAlreadyExistsException("JobInstanceAlreadyCompleteException",e);
    }
}

}

在应用程序启动后执行作业只需设置属性:

spring.batch.job.names=globalJob