如何在Spring Batch中循环中运行一个步骤:已更新

时间:2016-12-05 13:45:28

标签: java spring spring-batch step

我有春季批量工作:

<job id="MyBatchJob" job-repository="jobRepository">

  <step id="ConfigurationReadStep">
        <tasklet ref="ConfigurationReadTasklet"  transaction-manager="jobRepository-transactionManager"/>
        <next on="COMPLETED"  to="NextStep" />  
  </step>        

  <step id="NextStep">
    <tasklet transaction-manager="jobRepository-transactionManager">
        <chunk reader="myItemReader" writer="myItemWriter" commit-interval="1000"/>
    </tasklet>
  </step>

     <listeners>
        <listener ref="jobListener" />
     </listeners>     

</job>

它作为配置读取步骤的第一步,在一些业务逻辑之后,我遇到了一个查询列表。对于例如说10个查询。我知道我可以使用JobExecutionContextPromotionListener推广此列表。

但是,我希望将此查询逐个1提供给我的下一步阅读器作为阅读器查询并在循环中运行该步骤,直到所有阅读器查询都被使用。我希望将每个查询作为弹簧批处理方案运行,因为它们可能会返回大量项目。

我该怎么做?

***********************************更新*********** ***********************

这就是我想要做的事情:

<job id="MyJob" job-repository="jobRepository">

      <step id="ConfigurationReadStep">
            <tasklet ref="ConfigurationReadTasklet"  transaction-manager="jobRepository-transactionManager"/>
            <next on="COMPLETED"  to="MyNextStep" />    
             <listeners>
                <listener ref="promotionListener"/>
            </listeners>
      </step>        

    <step id="MyNextStep" next="limitDecision">
        <tasklet transaction-manager="jobRepository-transactionManager">
            <chunk reader="MyItemReader" writer="MyItemWriter" commit-interval="1000"/>
        </tasklet>
    </step>

    <decision id="limitDecision" decider="limitDecider">
        <next on="CONTINUE" to="MyNextStep" />
        <end on="COMPLETED" />
    </decision>

     <listeners>
        <listener ref="jobListener" />
     </listeners>

  </job>

<beans:bean id="jobListener" class="com.hsbc.gbm.dml.integration.batch.listener.SimpleJobListener" />

<beans:bean id="promotionListener" class="org.springframework.batch.core.listener.ExecutionContextPromotionListener">
    <beans:property name="keys" value="readerLimit,readerQueries,readerSQL" />
</beans:bean>

  <beans:bean id="ConfigurationReadTasklet" class="com.mypackage.ConfigurationReadTasklet" scope="step">
  </beans:bean>

<beans:bean id="MyItemReader" class="org.springframework.batch.item.database.JdbcCursorItemReader" scope="step">
    <beans:property name="dataSource" ref="myDataSource" />
    <beans:property name="sql" value="#{jobExecutionContext['readerSQL']}" />
    <beans:property name="rowMapper">
        <beans:bean  name="mapper" class="com.mypackage.MyRowMapper" />
    </beans:property>
</beans:bean>

<beans:bean id="MyItemWriter" class="com.mypackage.MyItemWriter" scope="step">
    <beans:constructor-arg ref="myDataSource" />
</beans:bean>

<beans:bean id="limitDecider" class="com.mypackage.StepLoopLimitDecider" scope="step"> 
    <beans:property name="readerQueries" value="#{jobExecutionContext['readerQueries']}" />
</beans:bean>     

在tasklet中,我将获得读者查询列表。我将readerQuery设置为第一步,然后转到下一步,它将作为弹簧批处理步骤正常运行。

完成此操作后,我希望我的Decider检查是否有更多读者查询,如果是,则会将readerQuery更改为下一个查询并重新运行NextStep,否则工作将完成。以下是我的决策:

public class StepLoopLimitDecider implements JobExecutionDecider {

private List<String> readerQueries;

private int count = 1;

public FlowExecutionStatus decide(JobExecution jobExecution,StepExecution stepExecution) {
    if (count >= this.readerQueries.size()) {
        return new FlowExecutionStatus("COMPLETED");
    }
    else {
        System.out.println(count + ": "+readerQueries.get(count));
        jobExecution.getExecutionContext().put("readerSQL", readerQueries.get(count));
        count = count + 1;
        return new FlowExecutionStatus("CONTINUE");
    }
}

public void setReaderQueries(List<String> readerQueries) {
    this.readerQueries = readerQueries;
}   
}

但是,这不起作用。 Step第一次正确运行。但决策失败时出现以下错误:

    2016-12-06 14:46:24 ERROR AbstractJob:306 - Encountered fatal error executing job
org.springframework.batch.core.JobExecutionException: Flow execution ended unexpectedly
    at org.springframework.batch.core.job.flow.FlowJob.doExecute(FlowJob.java:141)
    at org.springframework.batch.core.job.AbstractJob.execute(AbstractJob.java:281)
    at org.springframework.batch.core.launch.support.SimpleJobLauncher$1.run(SimpleJobLauncher.java:120)
    at org.springframework.core.task.SyncTaskExecutor.execute(SyncTaskExecutor.java:48)
    at org.springframework.batch.core.launch.support.SimpleJobLauncher.run(SimpleJobLauncher.java:114)
    at org.springframework.batch.core.launch.support.CommandLineJobRunner.start(CommandLineJobRunner.java:348)
    at org.springframework.batch.core.launch.support.CommandLineJobRunner.main(CommandLineJobRunner.java:565)
Caused by: 
org.springframework.batch.core.job.flow.FlowExecutionException: Ended flow=MyBatchJob at state=MyBatchJob.limitDecision with exception
    at org.springframework.batch.core.job.flow.support.SimpleFlow.resume(SimpleFlow.java:152)
    at org.springframework.batch.core.job.flow.support.SimpleFlow.start(SimpleFlow.java:124)
    at org.springframework.batch.core.job.flow.FlowJob.doExecute(FlowJob.java:135)
    ... 6 more
Caused by: 
org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'scopedTarget.limitDecider': Scope 'step' is not active for the current thread; consider defining a scoped proxy for this bean if you intend to refer to it from a singleton; nested exception is java.lang.IllegalStateException: No context holder available for step scope
    at org.springframework.beans.factory.support.AbstractBeanFactory.doGetBean(AbstractBeanFactory.java:339)
    at org.springframework.beans.factory.support.AbstractBeanFactory.getBean(AbstractBeanFactory.java:190)
    at org.springframework.aop.target.SimpleBeanTargetSource.getTarget(SimpleBeanTargetSource.java:33)
    at org.springframework.aop.framework.JdkDynamicAopProxy.invoke(JdkDynamicAopProxy.java:182)
    at $Proxy4.decide(Unknown Source)
    at org.springframework.batch.core.job.flow.support.state.DecisionState.handle(DecisionState.java:43)
    at org.springframework.batch.core.configuration.xml.SimpleFlowFactoryBean$DelegateState.handle(SimpleFlowFactoryBean.java:141)
    at org.springframework.batch.core.job.flow.support.SimpleFlow.resume(SimpleFlow.java:144)
    ... 8 more
Caused by: 
java.lang.IllegalStateException: No context holder available for step scope
    at org.springframework.batch.core.scope.StepScope.getContext(StepScope.java:197)
    at org.springframework.batch.core.scope.StepScope.get(StepScope.java:139)
    at org.springframework.beans.factory.support.AbstractBeanFactory.doGetBean(AbstractBeanFactory.java:325)
    ... 15 more

有人可以帮我做这项工作。

0 个答案:

没有答案