Spring Batch 笔记-流程控制

    此文主要参考《spring batch in action》的第10章controlling execution。在之前介绍了batch按step线性执行任务,实际上它也可以执行一定复杂的流程处理。设计一个简单流程,流程如下

 

根据以上需求,我们只需要如此配置job

    <batch:job id="etljob">
        <batch:step id="prepareEtl" next="readWriteStep">
            <batch:flow parent="beginEtlFlow" />
        </batch:step>
        <batch:step id="readWriteStep">
            <batch:tasklet>
                <batch:chunk reader="reader" writer="writer" commit-interval="1000" />
            </batch:tasklet>
        </batch:step>
    </batch:job>
   
    <!-- 开始etl,判断表是否存在,不存在就创建表 -->
    <batch:flow id="beginEtlFlow">
        <batch:decision id="tableExistsDecision" decider="tableExistsDecider">
            <batch:end on="YES" />
            <batch:next on="NO" to="createTbStep" />
        </batch:decision>
        <batch:step id="createTbStep">
            <batch:tasklet ref="createTblsTasklet" />
        </batch:step>
    </batch:flow>

在sts的视图里就是下图所示

判断表是否存在的decider

import java.util.Random;
import org.springframework.batch.core.JobExecution;
import org.springframework.batch.core.StepExecution;
import org.springframework.batch.core.job.flow.FlowExecutionStatus;
import org.springframework.batch.core.job.flow.JobExecutionDecider;
import org.springframework.stereotype.Component;

@Component("tableExistsDecider")
public class TableExistsDecider  implements JobExecutionDecider {

    @Override
    public FlowExecutionStatus decide(JobExecution jobExecution,
            StepExecution stepExecution) {
 
        if(isTblExist())
            return new FlowExecutionStatus("YES");
        else
            return new FlowExecutionStatus("NO");
    }
    
    //判断表是否存在
    private boolean isTblExist(){
        Random rd=new Random();
        return rd.nextBoolean();
    }

}

创建表的tasklet

import org.springframework.batch.core.StepContribution;
import org.springframework.batch.core.scope.context.ChunkContext;
import org.springframework.batch.core.step.tasklet.Tasklet;
import org.springframework.batch.repeat.RepeatStatus;
import org.springframework.stereotype.Component;

@Component("createTblsTasklet")
public class CreateTblsTasklet implements Tasklet{

    @Override
    public RepeatStatus execute(StepContribution contribution,
            ChunkContext chunkContext) throws Exception {

        createTable();
        return RepeatStatus.FINISHED;
    }
    
    private void createTable(){
        System.out.println("finish create table");
    }

}

reader

import org.springframework.batch.item.ItemReader;
import org.springframework.stereotype.Component;

@Component("reader")
public class Reader implements ItemReader<String> {
    
    private String[] input = {"Hello world!", null};
    
    private int index = 0;
    
    /**
     * Reads next record from input
     */
    public String read() throws Exception {
        System.out.println("first Reader");
        if (index < input.length) {
            return input[index++];
        }
        else {
            return null;
        }
        
    }
}
View Code

writer

@Component("writer")
public class Writer implements ItemWriter<Object> {

    private static final Log log = LogFactory.getLog(Writer.class);
    
    public void write(List<? extends Object> data) throws Exception {
        log.info(data);
        System.out.println("first Writer");
    }

}
View Code

 

 

 

 

转载于:https://www.cnblogs.com/treeliang/p/3347327.html

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值