Spring-Batch Multi-line record Item Writer with variable number of lines per record

后端 未结 3 1651
旧巷少年郎
旧巷少年郎 2020-12-09 06:56

I have the below requirement but am not able to decide on the approach to take:

I need to write data to a fixed format out put file where each record spans over mult

3条回答
  •  死守一世寂寞
    2020-12-09 07:36

    there is a multiline records writer example in the official spring-batch-samples, search for multiline.xml and MultiLineTradeItemWriter

    its basically the usual delegate principle, you just need a proper domain object with supposable a list of those 1..n intermediate lines

        public class MultiLineTradeItemWriter implements ItemWriter, ItemStream {
    
        private FlatFileItemWriter delegate;
    
        public void write(List items) throws Exception {
                    List lines = new ArrayList();
                  for (Trade t : items) {
                  lines.add("BEGIN");
                  lines.add("INFO," + t.getIsin() + "," + t.getCustomer());
                  lines.add("AMNT," + t.getQuantity() + "," + t.getPrice());
                  lines.add("END");
                }
                this.delegate.write(lines);
         }
        }
    

提交回复
热议问题