SpringBatch read one database and write multiple XML files from List of List

first of all sorry for my poor english that's not my main language and i'm novice in springbatch as well.

Then, I need to read a database and extract data in differents xml files:

  1. first I need to check which Dealer need to get this xml data's extraction by executing a query
  2. then I need to loop on these Dealers to make another request and get back some data : One ArrayList<Customer> per Dealer and these arrayList are put in a List.

    public List<ArrayList<Customer>> select() {
    List<Customer> customers= new ArrayList<Customer>();
    List<ArrayList<Customer>> customersList = new ArrayList<ArrayList<Customer>>();
    List<Dealer> dealers = new ArrayList<Dealer>();
    try {
       dealers = jdbcTemplate.query(new PreparedStatementCreator(){
           public PreparedStatement createPreparedStatement(Connection connection) throws SQLException {
               return getSelectDealerPreparedStatement(connection, SELECT_DEALERS);
       },new DealerRowMapper());
    } catch (Exception e) {
        LOGGER.error("Error occured when select :  " + SELECT_DEALERS + "\n" + e.getMessage(), e);
    try {
        for(final Dealer dealer : dealers) {
           customers = jdbcTemplate.query(new PreparedStatementCreator() {
               public PreparedStatement createPreparedStatement(Connection connection) throws SQLException {
                   return getSelectPreparedStatement(connection, SELECT,dealer);
           }, new CustomerRowMapper());
           customersList.add(new ArrayList<Customer>(customers));
       }catch (Exception e) {
           LOGGER.error("Error occured when select:  " + SELECT + "\n" + e.getMessage(), e);
    return customersList;

Until this step everything is fine so I return to my reader class a List<ArrayList<Customer>> . My reader class implements ItemReader<ArrayList<Customer>> and my read method is:

public class BddDataBaseReader implements ItemReader<ArrayList<Customer>> {

private BddRepository bddRepository;
private int index;
private List<ArrayList<Customer>> inputDataBaseRowList;

BddDataBaseReader() {
    index = 0;
public ArrayList<Customer> read() throws Exception, UnexpectedInputException, ParseException, NonTransientResourceException {
    ArrayList<Customer> next = null;
    inputDataBaseRowList = getInputDataBaseRowList();
    if (index < inputDataBaseRowList.size()) {
        next = inputDataBaseRowList.get(index);
    return next;

private List<ArrayList<Customer>> getInputDataBaseRowList() {
    if (inputDataBaseRowList == null) {
        inputDataBaseRowList = bddRepository.select();
    return inputDataBaseRowList;
public void setBddRepository(BddRepository bddRepository) {
    this.bddRepository = bddRepository;

But then when I want each ArrayList<Customer> inputDataBaseRowList in a different xml file.

And here is my actual writer :

public class CustomerXMLWriter extends StaxEventItemWriter {

private int succeededRecordsCount;
private int recordsCount;
private List<Integer> failedRecords;

public void beforeStep(StepExecution stepExecution) {
    LOGGER.info("Write before step" + stepExecution);
    succeededRecordsCount = 0;
    failedRecords = new ArrayList<>();
public void write(List list) throws Exception {
   LOGGER.info("write" + list);
   for (Object row : list) {
       List item = Collections.singletonList(row);
       try {
       } catch (Exception e) {
           LOGGER.error("Error on row " + recordsCount, e);
public void afterStep(StepExecution stepExecution) throws Exception {
    LOGGER.info("Write after step" + stepExecution);
 stepExecution.getExecutionContext().put("SUCCEEDED_IMPORTED_RECORDS_COUNT", succeededRecordsCount);
    stepExecution.getExecutionContext().put("FAILED_IMPORTED_RECORDS", failedRecords);


So this code works if my read method returns a Customer instead of an ArrayList<Customer> but I need to make a file per ArrayList and I don't know how to adapt it.



0 Answers SpringBatch read one database and write multiple XML files from List of List