How to skip batch step when condition is false

不羁的心 提交于 2019-12-06 08:01:07

问题


I have one basis job with one basic step. This jobs is executing every x second (I am using quartz for this). Then in my config class I also have variable "runStep". Where should I add this attribute and run my step only if runStep is true ?

<batch:job id="export1" parent="baseJob">
    <batch:step id="registruj" parent="baseStep">
        <tasklet>
            <chunk reader="registrujReader" processor="registrujProcessor" writer="registrujWriter"
                commit-interval="1" />
        </tasklet>
    </batch:step>
</batch:job>

<bean id="baseJob" class="org.springframework.batch.core.job.SimpleJob" abstract="true">
    <property name="jobRepository" ref="jobRepository" />
</bean>

<bean id="baseStep" class="org.springframework.batch.core.step.factory.SimpleStepFactoryBean" abstract="true">
    <property name="transactionManager" ref="transactionManager" />
    <property name="jobRepository" ref="jobRepository" />
    <property name="startLimit" value="100" />
    <property name="commitInterval" value="1" />
</bean>

<bean id="jobRepository" class="org.springframework.batch.core.repository.support.JobRepositoryFactoryBean"
    p:dataSource-ref="expDataSource" p:transactionManager-ref="transactionManager" />

<bean id="jobLauncher" class="org.springframework.batch.core.launch.support.SimpleJobLauncher">
    <property name="jobRepository" ref="jobRepository" />
</bean>

<bean class="org.springframework.batch.core.configuration.support.JobRegistryBeanPostProcessor">
    <property name="jobRegistry" ref="jobRegistry" />
</bean>

<bean id="jobRegistry" class="org.springframework.batch.core.configuration.support.MapJobRegistry" />

<bean id="registrujWriter" class="cz.isvs.reg.rob.util.export.batch.RegistrujItemWriter" scope="step" />
<bean id="registrujReader" class="cz.isvs.reg.rob.util.export.batch.RegistrujItemReader" scope="step" />
<bean id="registrujProcessor" class="cz.isvs.reg.rob.util.export.batch.RegistrujItemProcessor" scope="step" />

<!-- run every 10 seconds -->
<bean class="org.springframework.scheduling.quartz.SchedulerFactoryBean">
    <property name="triggers">
        <bean id="cronTrigger" class="org.springframework.scheduling.quartz.CronTriggerBean">
            <property name="jobDetail" ref="jobDetail" />
            <property name="cronExpression" value="*/10 * * * * ?" />
        </bean>
    </property>
</bean>

<bean id="jobDetail" class="org.springframework.scheduling.quartz.JobDetailBean">
    <property name="jobClass" value="cz.isvs.reg.rob.util.export.batch.JobLauncherDetails" />
    <property name="group" value="quartz-batch" />
    <property name="jobDataAsMap">
        <map>
            <entry key="jobName" value="export1" />
            <entry key="jobLocator" value-ref="jobRegistry" />
            <entry key="jobLauncher" value-ref="jobLauncher" />
        </map>
    </property>
</bean>

回答1:


Use a JobExecutionDecider

public class RunStepDecider implements JobExecutionDecider {
  public FlowExecutionStatus decide(JobExecution jobExecution, StepExecution stepExecution) {
    final String runStep = jobExecution.getJobParameters().getString("runStep");
    //Depending on the above condition you can return Completed or Failed.
    return new FlowExecutionStatus.COMPLETED;
  }
}
<batch:job id="export1" parent="baseJob">
  <decision id="decision" decider="decider">
    <next on="COMPLETED" to="registruj" />
  </decision>
  <batch:step id="registruj" parent="baseStep">
    <tasklet>
      <chunk reader="registrujReader" processor="registrujProcessor" writer="registrujWriter" commit-interval="1" />
    </tasklet>
  </batch:step>
</batch:job>


<bean id="decider" class="RunStepDecider" />

and pass runStep as JobParameter.

Hope can help to solve your problem.



来源:https://stackoverflow.com/questions/18374899/how-to-skip-batch-step-when-condition-is-false

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!