Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
@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();
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment