简体   繁体   English

Spring Batch FlatFileItemReader 在以后的步骤中提供文件名

[英]Spring Batch FlatFileItemReader provide filename in future step

So I'm building a batch process that uses Spring Batch.所以我正在构建一个使用 Spring Batch 的批处理。 I've defined a job that has a few steps, the first being an implementation of Tasklet which is a file watcher and checks a directory for any file that matches a particular file mask.我定义了一个有几个步骤的作业,第一个是 Tasklet 的实现,它是一个文件观察器,并检查与特定文件掩码匹配的任何文件的目录。 Once that file is found, we move forward with the next step in the process.找到该文件后,我们将继续进行该过程的下一步。 Initially this was additionally another implementation of Tasklet and we were looping through the file and for each records, loading to Oracle in batch loads.最初,这也是 Tasklet 的另一种实现,我们循环遍历文件和每条记录,批量加载到 Oracle。 This was taking way too long.这花费的时间太长了。 I have found that using FlatFileItemReader and JdbcBatchItemWriter is literally 1000 times faster.我发现使用 FlatFileItemReader 和 JdbcBatchItemWriter 确实快了 1000 倍。 Anyway, my issue is that when using FlatFileItemReader I have to define my resource and provide a FileSystemResouce when that Bean is created.无论如何,我的问题是,当使用 FlatFileItemReader 时,我必须定义我的资源并在创建该 Bean 时提供 FileSystemResouce。 I really want to supply that filename after my first step is completed because I need to run a filewatcher and figure out what the filename is that we want to process.我真的想在我的第一步完成后提供该文件名,因为我需要运行一个文件观察器并找出我们想要处理的文件名。 Is there a way of achieving this?有没有办法实现这一目标?

@Bean
public FlatFileItemReader<PartnerRelationship> partnerRelationshipReader() throws ParseException {
    FlatFileItemReader<PartnerRelationship> reader = new FlatFileItemReader<>();
    reader.setResource(new FileSystemResource("/path/to/my/file/file_20210714.dat"));
    reader.setBufferedReaderFactory(new CustomFileReaderFactory());
    reader.setStrict(false);
    reader.setLineMapper(new DefaultLineMapper<PartnerRelationship>() {{
        setLineTokenizer(new FixedLengthTokenizer() {{
            setNames(Constants.partnerRelationshipFields);
            setColumns(Constants.partnerRelationshipIndeces);
        }});
        setFieldSetMapper(new PartnerRelationshipFieldSetMapper());
    }});
    return reader;
}

You could pass the resource to the jobExecutionContext:您可以将资源传递给 jobExecutionContext:

ExecutionContext jobExecutionContext = stepExecution.getJobExecution().getExecutionContext();
jobExecutionContext.put("resource", res);

It can be retrieved if you set your bean stepScope:如果您设置了 bean stepScope,则可以检索它:

@Bean
@StepScope
public FlatFileItemReader<PartnerRelationship> partnerRelationshipReader(@Value #{jobExecutionContext['resource']} Resource res) throws ParseException {
    FlatFileItemReader<PartnerRelationship> reader = new FlatFileItemReader<>();
    reader.setResource(res);
    reader.setBufferedReaderFactory(new CustomFileReaderFactory());
    reader.setStrict(false);
    reader.setLineMapper(new DefaultLineMapper<PartnerRelationship>() {{
        setLineTokenizer(new FixedLengthTokenizer() {{
            setNames(Constants.partnerRelationshipFields);
            setColumns(Constants.partnerRelationshipIndeces);
        }});
        setFieldSetMapper(new PartnerRelationshipFieldSetMapper());
    }});
    return reader;
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM