Spring Batch with Java Config: An Example

Reading Time: 2 minutes

Enterprises tend to be large, old institutions. They have done a lot of stuff, and they have a lot of data under their belts. When we’re working with enterprises to build apps that will help them understand their data, we need a way to process that data that takes time and system limitations into account. We can often process a lot of stuff at once more efficiently by using batch jobs. Spring Batch gives us a convenient tool to perform batch processing in Spring applications.

cookies

If you are new to Batch and would like to learn about some of the use cases for the tool, I would recommend section 1.2 of the documentation. If you would like an overview of Batch and you want to understand how it performs batch jobs at the top level, I recommend Chapter 2 of the book Pro Spring Batch

I have been working on sample configuration for a theoretical Spring Integration application, and since I have also been working with Batch it made sense to provide a sample configuration for batch, too.

The following batch configuration goes with an app that solves a business problem for a book club. The book club has been around since the early days of the internet and has all of its data on its members stored in XML files. Now, they want to add the birthdays of each of their members to the file system so they can create alerts on their members’ birthdays, offer discounts from sponsoring organizations for members on their birthdays, or collate member age data. The book club has discovered that it can fetch member birthdays from a Facebook API using member information that they already have in the system.

The book club has a giant XML file called all_members.xml. This app will take those XML records in batches of 5, make the call to get the birthdays, add the birthday to each member, and then print out each member’s birthday.


@Configuration
@EnableBatchProcessing
public class XmlParsingApplication {
@Bean
public StaxEventItemReader itemReader() throws Exception {
StaxEventItemReader reader = new StaxEventItemReader<>();
reader.setName("birthday");
reader.setResource(new ClassPathResource("all_members.xml"));
reader.setFragmentRootElementName("ClubMember");
Jaxb2Marshaller unmarshaller = new Jaxb2Marshaller();
unmarshaller.setClassesToBeBound(ClubMember.class, Membership.class);
unmarshaller.afterPropertiesSet();
reader.setUnmarshaller(unmarshaller);
return reader;
}
@Bean
public CustomProcessor customProcessor() {
return new CustomProcessor<ClubMember, ClubMember>() {
//makes a network call with the club member's information
//to get the member's birthday
//and then sets the birthday attribute on the member to that birthday
};
}
@Bean
public ItemWriter itemWriter() {
return new ItemWriter() {
@Override
public void write(List<? extends ClubMember>; items) throws Exception {
for (ClubMember item : items) {
System.out.println("item.getName() + 's birthday is " + item.getBirthdate());
}
}
};
}
@Autowired
public JobBuilderFactory jobBuilderFactory;
@Autowired
public StepBuilderFactory stepBuilderFactory;
@Bean
public Job job() throws Exception {
return jobBuilderFactory.get("job")
.start(stepBuilderFactory.get("step1")
.<ClubMember, ClubMember>chunk(5)
.reader(itemReader())
.processor(customProcessor())
.writer(itemWriter())
.build())
.build();
}
}

Leave a Reply

This site uses Akismet to reduce spam. Learn how your comment data is processed.