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

来源:互联网 发布:阿里云业务 编辑:程序博客网 时间:2024/05/20 19:29

有两种方式:

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


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

创建一个data holder

@Componentpublic 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

0 0
原创粉丝点击