I am exploring Spring batch and I have a problem statement which require me to read from db, transform this data into comma separated form and write to a file. I have around 50 different queries and as many number of files to create. Few of these queries return huge data which could make my file large. I was solving this with spring batch and have few queries in general about spring batch.
- Can a field extractor be used when I need to transform a particular field value.
BeanWrapperFieldExtractor<StudentDTO> extractor = new BeanWrapperFieldExtractor<>();
extractor.setNames(new String[] {"name", "emailAddress", "purchasedPackage"});
lineAggregator.setFieldExtractor(extractor);
for example, if i need to do something like studentDto.getName().replace("a",""). Should I go for a custom processor in such cases?
- Is 1 job with 50 steps and parallel processing an apt way to go about in this scenario?
- Writing header to the top of the file instead of using FlatFileHeaderCallback - Is the below way of writing to file acceptable?
@Override
public ExitStatus afterStep(StepExecution stepExecution) {
if (stepExecution.getStatus() == "COMPLETED") {
fileWriter.write("headerString");
Path path = Paths.get("encryptedTextFileThreaded.txt");
try (BufferedWriter fileWriter = Files.newBufferedWriter(path)) {
for(Line line: studentDtoLines)
{
fileWriter.write(line.getLine());
fileWriter.newLine();
}
fileWriter.write("footerString");
}
catch (Exception e) {
log.error("Fatal error: error occurred while writing {} file",path.getFileName());
}
}
- Multi threaded steps are for speeding up a single step. If I have a Job with 50 steps and none of them steps depends on the other, then parallel processing can be employed to speed up the execution of Job. True? Does this mean spring batch can create 50 threads and run all of them in paralle?