0

I have a situation in which the deployment server doesn't allow the application to output files to its file system ... so what i'm trying to do is to configure the FlatFileItemWriter to output the result file to a static Resource property (multi Threading is not an issue here ) . the current code i have is

...
flatFileItemWriter.setResource(new FileSystemResource("outputBatch/users.csv"));
...

is there any way i can output this file to a static Resource property to be accessed later, for example ByteArrayResource or InMemoryResource . thanks in advance

toulaaaax
  • 5
  • 3

2 Answers2

1

I have similar problem - I would like to write/read GCP Cloud Storages files. I use example from https://github.com/ncelq/spring-batch-gcs/blob/master/src/main/java/com/cloud/sample/GSFileWriter.java but header/footer is not writed.

  @Bean
  @StepScope
  public GCSFileWriter<MyClass> myClassGcsWriter() throws IOException {
    final Resource outputResource = this.gcpLoader.getResource("gs://my-bucket/myfile");
    final GCSFileWriter<MyClass> writer = new GCSFileWriter<>();
    final Storage storage = StorageOptions.newBuilder().setCredentials(GoogleCredentials.getApplicationDefault()).build().getService();
    writer.setStorage(storage);
    writer.setResource(outputResource);

    final FormatterLineAggregator<MyClass> formatterLineAggregator = new FormatterLineAggregator<>();
    final BeanWrapperFieldExtractor<MyClass> beanWrapperFieldExtractor = new BeanWrapperFieldExtractor<>();
    beanWrapperFieldExtractor.setNames(FlatFileFormatConverter.getNames(MyClass.class, false, true));
    formatterLineAggregator.setFieldExtractor(beanWrapperFieldExtractor);
    formatterLineAggregator.setFormat(FlatFileFormatConverter.getFormat(MyClass.class, true));

    writer.setShouldDeleteIfExists(true);
    writer.setLineAggregator(formatterLineAggregator);
    writer.setHeaderCallback(getFileHeaderCallback(null));
    writer.setFooterCallback(getFileFooterCallback(null));

    return writer;
  }

If I use standard FlatFileItemWriter header and footer are saved

    final Resource outputResource = new FileSystemResource("somedirectory/myfile");

// @formatter:off
return new FlatFileItemWriterBuilder<MyClass>()
    .name("myClassWriter")
    .resource(outputResource)
    .formatted()
    .format(FlatFileFormatConverter.getFormat(MyClass.class, true))
    .names(FlatFileFormatConverter.getNames(MyClass.class, false, true))
    .shouldDeleteIfExists(true)
    .headerCallback(getFileHeaderCallback(null))
    .footerCallback(getFileFooterCallback(null))
    .build();
// @formatter:on

I will be grateful for help.

szopal
  • 21
  • 3
0

The FlatFileItemWriter is designed to write items to a flat file or a writable Resource that represents a file. Here is an excerpt from its Javadoc:

The location of the output file is defined by a Resource and must represent a writable file.

So in your case, you need to use a custom writer that writes items to an in-memory resource.

Mahmoud Ben Hassine
  • 28,519
  • 3
  • 32
  • 50
  • I always wait for your answers, thank you Mahmoud , i can give up on the `FlatFileItemWriter` now for a custom one . – toulaaaax Feb 15 '21 at 15:24
  • Thank you, always happy to help! If the answer helped, please accept it or upvote it. – Mahmoud Ben Hassine Feb 15 '21 at 18:40
  • @MahmoudBenHassine it would be good if you had a FlatFileItemWriter to write to ByteArrayResource for JUNIT testing. Our Jenkins build servers does not allow app to write to server. – Llewelyn Jones Aug 20 '23 at 17:25
  • @LlewelynJones Yes, and unfortunately there is no one provided by Spring Batch OOTB. You can create a custom one for tests if needed. – Mahmoud Ben Hassine Aug 29 '23 at 12:58