Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Unfound resource for FlatFileItemReader after moving the file

I am using Spring Batch to read from a CSV file and write the lines on the screen. My job is composed of 3 parts: Part 1 : Verify if the CSV file exists in some INPUT directory on my disk, if it returns TRUE the file will be moved to another directory called PROD. Part 2 : Extract data from the CSV file using FlatFileItemReader. Part 3 : Write the all the items to the screen.

The problem is the FlatFileItemReader throws org.springframework.batch.item.ItemStreamException: Failed to initialize the reader caused by java.lang.IllegalArgumentException: Input resource must be set

Here is my code:

@Bean
public FlatFileItemReader<UniversInvestissement> reader() {
    FlatFileItemReader<UniversInvestissement> reader = new FlatFileItemReader<>();
    File csvFile = new File("C://INPUT/data.csv");
    Resource resource = resourceLoader.getResource("file:" + csvFile.getAbsolutePath());
    reader.setLinesToSkip(1);
    reader.setResource(resource);
    DefaultLineMapper lineMapper = new DefaultLineMapper();

    DelimitedLineTokenizer tokenizer = new DelimitedLineTokenizer();
    tokenizer.setNames(new String[]{"COL1", "COL2", "COL3", "COL4"});
    tokenizer.setDelimiter(";");

    FieldSetMapper fieldSetMapper = new UniversInvestissementFieldSetMapper();
    lineMapper.setLineTokenizer(tokenizer);
    lineMapper.setFieldSetMapper(fieldSetMapper);

    reader.setLineMapper(lineMapper);

    reader.setEncoding("Cp1252");
    return reader;
}

@Bean
public UniversInvestissementWriter writer() {
    return new UniversInvestissementWriter();
}

@Bean
public UniversInvestissementProcessor processor() {
    return new UniversInvestissementProcessor();
}

@Bean
public Step extractData() {
    return steps.get("extractData")
            .<UniversInvestissement, UniversInvestissementProcessorResult>chunk(1)
            .reader(reader())
            .processor(processor())
            .writer(writer())
            .build();
}

Actually the problem is that when the FlatFileItemReader is initialized it can't find the CSV file as a resource ! Is there a way to postpone the resource assignment and avoid this exception ?

like image 303
Ghassen Avatar asked Nov 17 '25 04:11

Ghassen


1 Answers

I think that problem in your resourceLoader, because such exception thrown by non-null assertion of resource instance. So you resourceLoader return null value.

Try to use FileSystemResource and without any resource loaders. For example:

reader.setResource(new FileSystemResource(csvFile));
like image 127
codeformars Avatar answered Nov 18 '25 19:11

codeformars



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!