spring batch : 在不同steps间传递数据

有两种方式:

1) 通过step_execution 或者 job_execution来在不同step中传递数据.但是如果数据量大的话,这将不是一种好的方式.因为spring batch默认会通过job repository将 setp_execution和job_execution进行持久化.


2)用自己定义的bean传递数据

创建一个data holder

@Component
public class PublicCompanyHolder {

 private List<PublicCompanyInfo> publicCompanyList;

 public List<PublicCompanyInfo> getPublicCompanyList() {
  return publicCompanyList;
 }

 public void setPublicCompanyList(List<PublicCompanyInfo> publicCompanyList) {
  this.publicCompanyList = publicCompanyList;
 }
}

在step 1中设置数据:

@Component("pubTasklet")
public class PubTasklet implements Tasklet {

 @Autowired
 private PublicCompanyHolder publicCompanyHolder;

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

  List<PublicCompanyInfo> infoContainer = new ArrayList<PublicCompanyInfo>();

  for (int i=0; i < 10; i++) {
   PublicCompanyInfo info = new PublicCompanyInfo();
   info.setPublicCompanyId("ID-" + i);
   info.setPublicCompanyName("Name*" + i);
   infoContainer.add(info);
  }
  publicCompanyHolder.setPublicCompanyList(infoContainer);

  return RepeatStatus.FINISHED;
 }

}

在step中取数据:

@Component("pubTasklet2")
public class PubTasklet2 implements Tasklet {
 @Autowired
 private PublicCompanyHolder publicCompanyHolder;

 public RepeatStatus execute(StepContribution contribution,
   ChunkContext chunkContext) throws Exception {
  System.out.println("received holder:" + publicCompanyHolder.getPublicCompanyList());
  
  return RepeatStatus.FINISHED;
 }

}

define.xml

 <job id="pubJob" restartable="true">
  <step id="step1" next="step2">
   <tasklet ref="pubTasklet" />
  </step>
  <step id="step2" next="step1">  // if you do not want to loop, remove next
   <tasklet ref="pubTasklet2" />
  </step>
  <listeners>
   <listener ref="pubListener" />
  </listeners>
 </job>
翻译自:http://wangxiangblog.blogspot.com/2013/02/spring-batch-pass-data-across-steps.html

  • 1
    点赞
  • 13
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值