Jeff Cook
Jeff Cook

Reputation: 8774

Spring Batch - create new unique CSV name while writing data using FlatFileItemWriter API

I tried the solution applied in the post here : Spring Batch - create a new file each time instead of overriding it for transferring data from CSV to XML, but it didn't worked for the annotation based approached I used.

fileItemWriter.setResource(new FileSystemResource("csv/employees-#{new java.text.SimpleDateFormat("Mddyyyyhhmmss").format(new java.util.GregorianCalendar().getTime())}.csv"));

My Batch job is scheduled to run in every 1 hours, this batch jobs reads table and write data to CSV file. When data writes I need to create new file altogether..will be good if file name is unique, so I was looking to implement the date etc as per post.

Could anyone guide what's wrong going on ?

@Bean(destroyMethod="")
    public FlatFileItemWriter<Employees> employeesWriter(){
        FlatFileItemWriter<Employees> fileItemWriter = new FlatFileItemWriter<>();
        //fileItemWriter.setResource(new FileSystemResource("csv/employees.csv"));
        fileItemWriter.setResource(new FileSystemResource("csv/employees-#{new java.text.SimpleDateFormat(&quot;Mddyyyyhhmmss&quot;).format(new java.util.GregorianCalendar().getTime())}.csv"));
        fileItemWriter.setHeaderCallback(headerCallback());

        BeanWrapperFieldExtractor<Employees> fieldExtractor = new BeanWrapperFieldExtractor<>();
        fieldExtractor.setNames(new String[] {"employeeNumber", "lastName", "firstName", "extension", "email", "officeCode", "reportsTo", "jobTitle"});

        DelimitedLineAggregator<Employees> lineAggregator = new DelimitedLineAggregator<>();
        lineAggregator.setDelimiter(",");
        lineAggregator.setFieldExtractor(fieldExtractor);

        fileItemWriter.setLineAggregator(lineAggregator);
        fileItemWriter.setShouldDeleteIfEmpty(true);

        return fileItemWriter;
    } 

Upvotes: 1

Views: 1417

Answers (1)

Mahmoud Ben Hassine
Mahmoud Ben Hassine

Reputation: 31600

Could anyone guide what's wrong going on ?

Three things:

  • SpEL expressions are not interpreted when used like you do
  • The &quot; copied from the xml sample will not work in Java config
  • The / in csv/... is not a valid character in a file name

You need to declare your writer as follows:

@Bean
public FlatFileItemWriter itemWriter(@Value("employees-#{new java.text.SimpleDateFormat('Mddyyyyhhmmss').format(new java.util.GregorianCalendar().getTime())}.csv") String filename) {

   FlatFileItemWriter<Employees> fileItemWriter = new FlatFileItemWriter<>();
   fileItemWriter.setResource(new FileSystemResource(filename));
   ...
   return fileItemWriter;

}

But I would recommend using a step scoped item writer and pass the file name as a job parameter rather than using a SpEL expression.

Upvotes: 2

Related Questions