简体   繁体   中英

FlatFileItemWriter write to a new (non-existent) File on each JobExecution

I am currently working on a Spring Batch CSV Export. The Chunk based reader,writer,processor is used. Reading and processing works flawless, but the FlatFileItemWriter always overwrites the output CSV file. I want the Job to check if a file is existent if it exists increment the fileName and write to a new File.

Here is my current code: My Helper class, that checks if a File is existent and returns the new String for the Writer to use.

import java.io.File;

import org.springframework.stereotype.Component;

@Component
public class OrderFileManager {

    private final String orderDesc = "order";
    private final String fileEnding = ".csv";

    public String getEmptyOrderNameString() {

        int fileNumber = 1;
        String answer = orderDesc + fileNumber + fileEnding;

        while (new File(answer).exists()) {
            fileNumber = fileNumber + 1;
            answer = orderDesc + fileNumber + fileEnding;
        }
        return answer;
    }
}

My ItemWriter :

    @Bean
    public FlatFileItemWriter<UltravisionOrder> writer(String fileName) {
        FlatFileItemWriter<UltravisionOrder> writer = new FlatFileItemWriter<UltravisionOrder>();  
        writer.setResource(new FileSystemResource(fileName));
        DelimitedLineAggregator<UltravisionOrder> delimitedLineAggregator = new DelimitedLineAggregator<UltravisionOrder>();
        delimitedLineAggregator.setDelimiter(";");
        BeanWrapperFieldExtractor<UltravisionOrder> fieldExtractor = new BeanWrapperFieldExtractor<UltravisionOrder>();
        fieldExtractor.setNames(new String[] { "col1", "col2", "col3", ... });
        delimitedLineAggregator.setFieldExtractor(fieldExtractor);
        writer.setLineAggregator(delimitedLineAggregator);
        return writer;
    }

Everytime the Job gets executed the FileWriter writes to the same output File. The OrderFileManager is autowired in the Job. And the writer receives the OrderFileManager. getEmptyOrderNameString() as Parameter.

If the Spring application context is not re-loaded at each job run, the file writer bean will be re-used and will always write to the same resource.

What you can do is make your writer step scoped and configure it by calling the method that generates the file name using a SpEL expression:

@Bean
@StepScope
public FlatFileItemWriter<UltravisionOrder> itemWriter(@Value("#{orderFileManager.getEmptyOrderNameString()}") String fileName) {
    // ...
}

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