Skip to content

Instantly share code, notes, and snippets.

@chelseatroy
Last active February 13, 2017 18:46
Show Gist options
  • Save chelseatroy/c5d0f6dcb1c5049a5b96ea698162f9c1 to your computer and use it in GitHub Desktop.
Save chelseatroy/c5d0f6dcb1c5049a5b96ea698162f9c1 to your computer and use it in GitHub Desktop.
@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