简体   繁体   中英

Spring Batch FlatFileItemReader provide filename in future step

So I'm building a batch process that uses 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. 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. This was taking way too long. I have found that using FlatFileItemReader and JdbcBatchItemWriter is literally 1000 times faster. Anyway, my issue is that when using FlatFileItemReader I have to define my resource and provide a FileSystemResouce when that Bean is created. 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:

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

It can be retrieved if you set your 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;
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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