Friday, February 1, 2013

Spring Batch - Pass data across steps

1) use step execution or job execution to transfer data from one step to another, but if the data is huge, it is not good as spring will store the data to be transferred to job repository if via step execution or job execution.

2) use own java beans to pass data.


  • This is data holder to be passed across steps
@Component
public class PublicCompanyHolder {

private List<PublicCompanyInfo> publicCompanyList;

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

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

}

  • set data to holder in 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;
}

}

  • get data from holder in step 2
@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 the job
<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>

15 comments:

  1. I have a similar requirement. But in my case its in multi threaded environment. Do you think this design is threadsafe?

    ReplyDelete
  2. Is it possible to share data between jobs. I want to read data from file and will store in hash map and next day whn same job will run, it will first check cache, it data is stored, i want to use that data only, dont want to read same file again, Is it possible?

    ReplyDelete
  3. I don't think your code is threadsafe: PublicCompanyHolder has singleton scope which means it is shared by multiple threads. Using @Scope("job") on PublicCompanyHolder should solve this multithread issue

    ReplyDelete
  4. Thanks for the post, I am techno savvy. I believe you hit the nail right on the head. I am highly impressed with your blog. It is very nicely explained. Your article adds best knowledge to our Java EE Training in Chennai. or learn thru Java EE Training in Chennai Students.

    ReplyDelete
  5. I’ve desired to post about something similar to this on one of my blogs and this has given me an idea. Cool Mat.
    python Training institute in Pune
    python Training institute in Chennai
    python Training institute in Bangalore

    ReplyDelete
  6. Pleasant Tips..Thanks for Sharing….We keep up hands on approach at work and in the workplace, keeping our business pragmatic, which recommends we can help you with your tree clearing and pruning in an invaluable and fit way.
    Data Science Training in Indira nagar
    Data Science training in marathahalli
    Data Science Interview questions and answers
    Data Science training in btm layout
    Data Science Training in BTM Layout
    Data science training in kalyan nagar

    ReplyDelete
  7. Thank you for allowing me to read it, welcome to the next in a recent article. And thanks for sharing the nice article, keep posting or updating news article.
    python training in chennai
    python course institute in chennai

    ReplyDelete

  8. Whoa! I’m enjoying the template/theme of this website. It’s simple, yet effective. A lot of times it’s very hard to get that “perfect balance” between superb usability and visual appeal. I must say you’ve done a very good job with this.

    AWS Training in Bangalore | Best AWS Amazon Web Services…
    Amazon Web Services (AWS) Training in Pune India
    AWS Training | AWS Training and Certification | AWS online training
    AWS Training in Bangalore cost| Aws training in Bangalore with placements

    ReplyDelete
  9. Nice post. By reading your blog, i get inspired and this provides some useful information. Thank you for posting this exclusive post for our vision. 
    AWS Training in pune

    AWS Online Training

    AWS Training in Bangalore

    ReplyDelete
  10. Your blog provided us with valuable information to work with. Each & every tips of your post are awesome. Thanks a lot for sharing. aws training in chennai | aws training in annanagar | aws training in omr | aws training in porur | aws training in tambaram | aws training in velachery

    ReplyDelete
  11. Very interesting to read this article.I would like to thank you for the efforts you had made for writing this awesome article. This article inspired me to read more. keep it up.
    java training in chennai

    java training in tambaram

    aws training in chennai

    aws training in tambaram

    python training in chennai

    python training in tambaram

    selenium training in chennai

    selenium training in tambaram

    ReplyDelete

  12. Great post. keep sharing such a worthy information.
    AWS Training institute in Chennai

    ReplyDelete