如何在Spring Batch模块中一次读取多个文件?

时间:2018-01-30 04:19:21

标签: java spring spring-batch

我希望我的Spring Batch模块一次读取多个文件。目前,我的批处理模块一次只能读取一个文件,但我能够从批处理模块中读取多行。我该如何实现呢?

<core type="BatchCreate" class="com.batch.BatchCore" concurrency="1" start="20" stop="80" status="true">

1 个答案:

答案 0 :(得分:0)

您可以使用 MultiResourceItemReader 来读取多个文件。

以下是相同

的java配置
@Bean
  public MultiResourceItemReader<POJO> multiResourceItemReader() {
    MultiResourceItemReader<POJO> resourceItemReader = new MultiResourceItemReader<POJO>();
        ClassLoader cl = this.getClass().getClassLoader();
        ResourcePatternResolver resolver = new PathMatchingResourcePatternResolver(cl);

    Resource[] resources = resolver.getResources("file:" + filePath );
    resourceItemReader.setResources(resources);
    resourceItemReader.setDelegate(reader());
    return resourceItemReader;
  }

  @Bean
  public FlatFileItemReader<POJO> reader() {
    FlatFileItemReader<POJO> reader = new FlatFileItemReader<POJO>();
    reader.setLineMapper(new DefaultLineMapper() {{
      setLineTokenizer(new DelimitedLineTokenizer() {{
        setNames(new String[]{"id1", "POJO"});
      }});
      setFieldSetMapper(new BeanWrapperFieldSetMapper<POJO>() {{
        setTargetType(POJO.class);
      }});
    }});
    return reader;
  }