代码之家  ›  专栏  ›  技术社区  ›  yN.

MultiResourceItemReader:bean初始化时不存在资源

  •  0
  • yN.  · 技术社区  · 7 年前

    脚本: 弹簧批处理作业有两个步骤:

    1. 下载csv文件的tasklet(以前未知的文件名) 运行时)到一个目录。
    2. 带读卡器的基于块的步骤,读卡器需要读取所有csv文件

    挑战:

    由于文件名未知,我们使用 PathMatchingResourcePatternResolver.getResources() 为了得到 resources .

    返回的资源总是长度为0,因为bean初始化时目录中没有文件。

    @Bean
    Resource[] resources() throws IOException {
        final PathMatchingResourcePatternResolver resolver = new PathMatchingResourcePatternResolver();
        final Resource[] resources = resolver.getResources("file:" + destinationDir + "/*.csv");
        return resources;
    }
    

    有什么想法吗?事先谢谢!

    1 回复  |  直到 7 年前
        1
  •  3
  •   Gustavo Passini    7 年前

    您可以将文件名保存在 JobExecutionContext 在tasklet步骤中,然后使用这些名称在chunk步骤中初始化资源。

    更多详情请参见Spring批量文档: Configuring a Step .

    由于step-scope bean,可以在春季批处理中进行这种后期绑定。你可以多读一些 here .