Writing multiple CSV files using MultiResourceItemWriter in a Step. After writing the various files in the Step, I want to be able to email these files to the appropriate receiver using the StepExecutionListener.
The problem, though, is how do I know which of the files should be sent to which email ? The filename or the suffix (I have a custom ResourceSuffixCreator but it only gets an index which can't help identify 1 file from the other usefully.)
Using spring boot 2.2.7.
Thanks for any help.
UPDATE I have, say, steps in this job. The success step outputs success related files for each email target using MultiResourceItemWriter.
return this.stepBuilderFactory.get("generateSuccessRecords")
.<SuccessReport, SuccessReport>chunk(1)
.reader(successReportItemReader(null, null))
.processor(itemProcessor)
.writer(successReportItemWriter(null))
.build();
successReportItemWriter is a MultiResourceItemWriter that delegates to a
return new MultiResourceItemWriterBuilder<SuccessReport>()
.name("successReportItemWriter")
.itemCountLimitPerResource(1)
.delegate(individualSuccessReportItemWriter())
.resource(new FileSystemResource(jobReportDirectory + "/successReport"))
.resourceSuffixCreator(suffixCreator)
.build();
individualSuccessReportItemWriter() is as below.
FlatFileItemWriter<SuccessReport> itemWriter = new FlatFileItemWriter<>();
itemWriter.setName("individualSuccessReportItemWriter");
itemWriter.setHeaderCallback(new SuccesssReportHeaderCallback());
itemWriter.setLineAggregator(new SuccessReportLineAggregator());
After the Success step generates the SuccessReport , the Fallout step will query from the DB and repeat the above to create the FalloutReport .csv files for each Email Target, again using MultiResourceItemWriter.
The goal is to be able to email each Email target the Success Report and the Fallout Report .csv files as attachment. Suppose, there are 25 Email Targets. There will be 25 Success .CSV files and 25 Failure .CSV files generated as a result of running the 2 Steps (Success and Fallout). Each Email target will get 1 success and 1 fallout .csv file as attachment.
The class SuccessReport and FalloutReport has the email Target while it generates the .csv file -- however unable to name the files as such because the suffix Creator does not allow for naming them accordingly.
I've come up with what I feel to be a somewhat crude hack, but hopefully it will help. From your example code, it appears that you are using a chunk size of 1 and a separate output file for each List
element (which corresponds to your description of one target email to each report). In that case, the following seems safe enough to suggest.
If your ResourceSuffixCreator
were also an ItemWriteListener
, you could extract the target email into a field variable that you could subsequently use for suffix generation. If you performed a hash of some kind (MD5) on the target email, then you could expect the same hash to be present for each of the reports destined to a given email target.
For example:
public class SuccessReportSuffixCreator
implements ItemWriteListener<SuccessReport>, ResourceSuffixCreator {
private String emailTarget = "";
@Override
public String getSuffix( int i ) {
return Md5Crypt.md5Crypt( this.emailTarget.getBytes() ) + "." + i + ".csv";
}
@Override
public void beforeWrite( List<? extends SuccessReport> list ) {
this.emailTarget = list.get( 0 ).getEmailTarget();
}
@Override
public void afterWrite( List<? extends SuccessReport> list ) {
// clear emailTarget?
}
@Override
public void onWriteError( Exception e, List<? extends SuccessReport> list ) {
// clear emailTarget?
}
}
I don't know what your class hierarchy looks like for the two report types, so you'll either need two distinct classes for each report type or a single class for a parent type that has the email target data.
You will also need to make sure and register this ItemWriteListener
on the Step
so its event handlers will be called appropriately.
I hope this works for you or gives you an alternative idea for how to solve your problem.
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