How to write more then one class in spring batch

点点圈 提交于 2019-12-12 02:23:01

问题


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):

  1. 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.
  2. Process downloaded file:
    2.1. With a FlatFileItemReader<S> (or your own ItemReader/ItemStream implementation) read downloaded file
    2.2 With an ItemProcessor<S,T> process each row
    2.3 Write each object to processed in 2.2 to database using a custom MyWriter<T> that do summary calculation and delegate to ItemWriter<T> for T's database persistence and to ItemWriter<Summary> to write Summary object.

<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

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!