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 publicCompanyList;

 public List getPublicCompanyList() {
  return publicCompanyList;
 }

 public void setPublicCompanyList(List 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 infoContainer = new ArrayList();

  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

 
  
   
  
    // if you do not want to loop, remove next
   
  
  
   
  
 
翻译自:http://wangxiangblog.blogspot.com/2013/02/spring-batch-pass-data-across-steps.html

你可能感兴趣的:(Java,batch)