Saurabh Deshpande
Saurabh Deshpande

Reputation: 1221

Spring Batch FlatFileItemWriter does not write data to a file

I am new to Spring Batch application. I am trying to use FlatFileItemWriter to write the data into a file. Challenge is application is creating the file on a given path, but, now writing the actual content into it.

Following are details related to code:

List<String> dataFileList : This list contains the data that I want to write to a file

FlatFileItemWriter<String> writer = new FlatFileItemWriter<>();
writer.setResource(new FileSystemResource("C:\\Desktop\\test"));
writer.open(new ExecutionContext());
writer.setLineAggregator(new PassThroughLineAggregator<>());
writer.setAppendAllowed(true);
writer.write(dataFileList);
writer.close();

This is just generating the file at proper place but contents are not getting written into the file.

Am I missing something? Help is highly appreciated.

Thanks!

Upvotes: 5

Views: 7490

Answers (2)

Nikhil Pareek
Nikhil Pareek

Reputation: 764

This is not a proper way to use Spring Batch Writer and writer data. You need to declare bean of Writer first.

  • Define Job Bean
  • Define Step Bean
  • Use your Writer bean in Step

Have a look at following examples:

https://github.com/pkainulainen/spring-batch-examples/blob/master/spring-boot/src/main/java/net/petrikainulainen/springbatch/csv/in/CsvFileToDatabaseJobConfig.java

https://spring.io/guides/gs/batch-processing/

Upvotes: 3

mikep
mikep

Reputation: 3895

You probably need to force a sync to disk. From the docs at https://docs.spring.io/spring-batch/trunk/apidocs/org/springframework/batch/item/file/FlatFileItemWriter.html,

setForceSync

public void setForceSync(boolean forceSync)

Flag to indicate that changes should be force-synced to disk on flush. Defaults to false, which means that even with a local disk changes could be lost if the OS crashes in between a write and a cache flush. Setting to true may result in slower performance for usage patterns involving many frequent writes.

Parameters:
    forceSync - the flag value to set

Upvotes: 2

Related Questions