问题
Situation:
I read url of file on internet from db. In itemProcessor I download this file and I want to save each row to database. Then processing continue and I want to create some new class "summary" which I want to save to db too. How should configure my job in spring batch ?
回答1:
For your use-case job can be defined using this step sequence (in this way this job is also restartable):
- Download file from URL to HDD using a Tasklet: a Tasklet is the strategy to process a single step; in your case something similar to this post can help and store local filename to
JobExecutionContext. - Process downloaded file:
2.1. With aFlatFileItemReader<S>(or your own ItemReader/ItemStream implementation) read downloaded file
2.2 With anItemProcessor<S,T>process each row
2.3 Write each object to processed in 2.2 to database using a customMyWriter<T>that do summary calculation and delegate toItemWriter<T>for T's database persistence and toItemWriter<Summary>to writeSummaryobject.
<S> is the bean contains each file row and <T> is the bean your write to db
MyWriter<T> can be used in this way:
class MyWriter extends ItemWriter<T> {
private ItemWriter<Summary> summaryWriter;
private ItemWriter<T> tWriter;
public void write(List<? super T> items) {
List<Summary> summaries = new ArrayList<>(items.size());
for(T item : items) {
final Summary summary = /* Here create summary object reading from
* database or creating new object */
/* Do summary or update summary */
summaries.add(summary);
}
/* The code above is trivial: you can group Summary object using a Map<SummaryKey,Summary> to reduce reading and use summaryWriter.write(summariesMap.values()) for example */
tWriter.write(items);
summaryWriter.write(summaries);
}
}
You need to save as stream both MyWriter.summaryWriter and MyWriter.tWriter for restartability.
回答2:
You can use a CompositeItemWriter.
But perhaps your summary processing should be in another step which reads the rows you previously inserted
来源:https://stackoverflow.com/questions/18396259/how-to-write-more-then-one-class-in-spring-batch