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 multiple lines as seen below:
000120992599999990000000000000009291100000000000000000000000010000
000000000000000000000006050052570009700000050000990494920000111100
ABCDE:WXYZ 0200
descriptiongoesheredescriptiongoesheredescriptiongoesher0200
descriptiongoesheredescriptiongoesheredescriptiongoesher0200
descriptiongoesheredescriptiongoesheredescriptiongoesher0200
descriptiongoesheredescriptiongoesheredescriptiongoesher0200
000000000000000000000006050052730005700001100000090494920000221200
ABCDE:WXYZ 0200
descriptiongoesheredescriptiongoesheredescriptiongoesher0200
descriptiongoesheredescriptiongoesheredescriptiongoesher0200
000000000000000000000006050113110009700000000000000494920000311100
ABCDE:WXYZ 0200
descriptiongoesheredescriptiongoesheredescriptiongoesher0200
descriptiongoesheredescriptiongoesheredescriptiongoesher0200
descriptiongoesheredescriptiongoesheredescriptiongoesher0200
000012099259999999000000000000000929110000000000000000000000001000
This is one record from above example:
000000000000000000000006050052570009700000050000990494920000111100
ABCDE:WXYZ 0200
descriptiongoesheredescriptiongoesheredescriptiongoesher0200
descriptiongoesheredescriptiongoesheredescriptiongoesher0200
descriptiongoesheredescriptiongoesheredescriptiongoesher0200
descriptiongoesheredescriptiongoesheredescriptiongoesher0200
The first and last line are header and footer respectively. First line of each record contains several details. 2nd line have some other details with spaces.
I have a long description field which I need to divide into 56 characters sections and append those with record's 3rd line onwards.
So in some of the records, this may be just one line while in some it could be three lines as well.
I need guidance about how to design my itemwriter in above scenario.
Nik
An ExecutionContext is a set of key-value pairs containing information that is scoped to either StepExecution or JobExecution . Spring Batch persists the ExecutionContext , which helps in cases where you want to restart a batch run (e.g., when a fatal error has occurred, etc.).
ItemWriter. It is the element of the step of a batch process which writes data. An ItemWriter writes one item a time. Spring Batch provides an Interface ItemWriter. All the writers implement this interface.
Multiple jobs can be run simultaneously. There are two main types of Spring Batch Parallel Processing: Single Process, Multi-threaded, or Multi-process. These are also divided into subcategories, as follows: Multi-threaded Step (Step with many threads, single process)
ItemWriter defines the batch artifact that writes to a list of items for chunk processing.
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<Trade>, ItemStream {
private FlatFileItemWriter<String> delegate;
public void write(List<? extends Trade> items) throws Exception {
List<String> lines = new ArrayList<String>();
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);
}
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With