• springbatch---->springbatch的使用(六)


      前面的例子step都是线性的流程,这里我们提供一个非线性的流程,也就是根据不同的状态做不同的流程step处理。万一有天悔恨变得太现实太世故太麻木,说不定能从回忆中重拾随兴飞翔。

    step非线性的流程

      A step execution listener can change the exit status of a step. The job can then use the exit status for the transition decision to the next step。我们通过job传递参数来模拟不同的退出状态,从而来验证和加强非线性流程的学习。

    一、在batch.xml中定义一个job

     <!--一个非线性流程的job-->
    <job id="noLinerJob">
        <step id="firstStep">
            <tasklet transaction-manager="transactionManager" ref="firstStepTasklet">
                <listeners>
                    <listener ref="firstStepListener"/>
                </listeners>
            </tasklet>
            <next on="COMPLETED" to="completedStep"/>
            <next on="OWN STATUS" to="ownStatusStep"/>
            <next on="*" to="allStatusStep"/>
        </step>
        <step id="completedStep">
            <tasklet ref="completedTasklet"/>
        </step>
        <step id="ownStatusStep">
            <tasklet ref="ownStatusTasklet"/>
        </step>
        <step id="allStatusStep">
            <tasklet ref="allStatusTasklet"/>
        </step>
    </job>

      在step层面上的监听器中会有不同的返回状态,根据不同的状态我们做不同的流程处理。比如如果是COMPLETED,我们就执行completedStep。如果是OWN STATUS(我们自定义的变量),就执行ownStatusStep。如果上述变量都没有,那么我们执行正则匹配所有的allStatusStep。下面我们列出firstStepListener的定义与实现。

    <bean id="firstStepListener" class="spring.batch.noLiner.FirstStepListener" scope="step">
        <property name="status" value="#{jobParameters['status']}"/>
    </bean>

    注意上述bean定义中的scope="step"是必须的,否则会报错。它的实现类代码如下

    package spring.batch.noLiner;
    
    import org.springframework.batch.core.ExitStatus;
    import org.springframework.batch.core.StepExecution;
    import org.springframework.batch.core.listener.StepExecutionListenerSupport;
    
    /**
     * @Author: huhx
     * @Date: 2017-11-02 下午 7:35
     */
    public class FirstStepListener extends StepExecutionListenerSupport {
        private String status;
    
        public void setStatus(String status) {
            this.status = status;
        }
    
        @Override
        public ExitStatus afterStep(StepExecution stepExecution) {
            if (status.equals("1")) {
                return new ExitStatus("OWN STATUS");
            } else if (status.equals("2")) {
                return new ExitStatus("NO STATUS");
            }
            return ExitStatus.COMPLETED;
        }
    }

    至于ownStatusTasklet和completedTasklet以及allStatusTasklet的实现比较简单,就是打印一句话。这里就不再列举。以下是打印的结果。

    // 当sttus= != '1' && sttus= != '2'
    first step tasklet.
    completed status.
    
    // 当sttus='1'
    first step tasklet.
    own status.
    
    // 当sttus='2'
    first step tasklet.
    all status.

     关于batch status与 exit status的区别:

        Spring Batch uses two concepts to represent the status of an execution: the batch sta- tus and the exit status. Both step execution and job execution have their own batch and exit statuses property. The batch status describes the status of the execution of a job or a step. The exit status represents the status of the job/step once the execution is finished.

    二、关于上述的next,springbatch还提供了fail、stop与end

    fail、stop与end的用法如下所示

    <step id="firstStep">
        <tasklet transaction-manager="transactionManager" ref="firstStepTasklet">
            <listeners>
                <listener ref="firstStepListener"/>
            </listeners>
        </tasklet>
        <next on="COMPLETED" to="completedStep"/>
        <end on="OWN STATUS"/>
        <fail on="*"/>
    </step>

    它们的说明如下

    友情链接

  • 相关阅读:
    python3调用js的库之execjs
    字体替换 re.sub
    asyncio和aiohttp
    微信公众号数据抓取
    celery
    Airtest 的连接安卓模拟器
    Scrapy同时启动多个爬虫
    随机IP代理插件Scrapy-Proxies
    Charles和mitmproxy代理设置
    win下删除EFI分区
  • 原文地址:https://www.cnblogs.com/huhx/p/baseusespringbatch6.html
Copyright © 2020-2023  润新知